Kilian Hersent
Kilian Hersent

Reputation: 103

How to fix wrong table labelling in Latex?

I am currently writing a report with many labels. All of them are working very well (section, figure, equations, ...) but not the labels of tables. Indeed, as I refer to any table, the pdf shows the label of the first table I defined...

I thought it may be due to one package I am using but I did not find out how to fix my issue.



    \documentclass[a4paper,12pt,twoside,openright]{report}
    \usepackage{array}
    \usepackage{babel}
    \usepackage{datatool}
    \usepackage{environ}
    \usepackage[T1]{fontenc}
    \usepackage{fancyhdr}
    \usepackage[top=0.5cm, bottom=0.5cm, left=2cm, right=2cm]{geometry}
    \usepackage{graphicx}
    \usepackage{enumitem}
    \usepackage[
        bookmarks         = true,%     % Signets
        bookmarksnumbered = true,%     % Signets numérotés%
        bookmarksopen     = true,%     % Signets ouverts
        colorlinks        = true,%     % Liens en couleur : true ou false
        urlcolor          = blue,%     % Couleur des liens externes
        linkcolor         = black,%    % Couleur des liens internes
        citecolor         = black,%    % Couleur des citations
        ]{hyperref}% 
    \usepackage{ifthen}
    \usepackage{indentfirst}
    \usepackage[utf8]{inputenc}
    \usepackage{lmodern}
    \usepackage{amsmath}
    \usepackage{amsfonts}
    \usepackage{amssymb}
    \usepackage{bm}
    \usepackage{makeidx}
    \usepackage{stmaryrd}
    \usepackage{tocbibind}
    \usepackage{url}
    \usepackage{pstricks}
    \usepackage{xcolor}
    \usepackage{placeins}


    \begin{document}

    \chapter{Table chapter}
    \label{ch:table}

    \section{Table section}
    \label{sec:table}

    \paragraph{}
    Let's begin with Chapter~\ref{ch:table} and Section~\ref{sec:table}.

    \paragraph{}
    Here is Table~\ref{tab:success}.

    \begin{table}[ht] \label{tab:success}
    \centering
    \begin{tabular}{|c|c|}
        \hline
        State & Success ? \\
        \hline
        Good & $\checkmark$ \\
        \hline
    \end{tabular}
    \caption{Table of success.}
    \end{table}

    \begin{table}[ht] \label{tab:defeat}
    \centering
    \begin{tabular}{|c|c|}
        \hline
        State & Success ? \\
        \hline
        Bad & $\times$ \\
        \hline
    \end{tabular}
    \caption{Table of defeat.}
    \end{table}

    \paragraph{}
    But there is also Table~\ref{tab:defeat}.

    \end{document}

The output is : The output of the previous code

enter image description here

As you can see the second label is "1.1" and should be "1.2"... What am I doing wrong?

Upvotes: 5

Views: 7178

Answers (1)

Labels must never be before the caption -- they must be either after the caption or within the caption.

Unrelated to your problem, but hyperref should be loaded after the other packages (there are only a handful exceptions of packages which go after hyperref, e.g. cleveref)

\documentclass[a4paper,12pt,twoside,openright]{report}
\usepackage{array}
\usepackage{babel}
\usepackage{datatool}
\usepackage{environ}
\usepackage[T1]{fontenc}
\usepackage[top=0.5cm, bottom=0.5cm, left=2cm, right=2cm]{geometry}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage{enumitem}

\usepackage{ifthen}
\usepackage{indentfirst}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{bm}
\usepackage{makeidx}
\usepackage{stmaryrd}
\usepackage{tocbibind}
\usepackage{url}
\usepackage{pstricks}
\usepackage{xcolor}
\usepackage{placeins}


\usepackage[
    bookmarks         = true,%     % Signets
    bookmarksnumbered = true,%     % Signets numérotés%
    bookmarksopen     = true,%     % Signets ouverts
    colorlinks        = true,%     % Liens en couleur : true ou false
    urlcolor          = blue,%     % Couleur des liens externes
    linkcolor         = black,%    % Couleur des liens internes
    citecolor         = black,%    % Couleur des citations
    ]{hyperref}% 

\begin{document}

\chapter{Table chapter}
\label{ch:table}

\section{Table section}
\label{sec:table}

\paragraph{}
Let's begin with Chapter~\ref{ch:table} and Section~\ref{sec:table}.

\paragraph{}
Here is Table~\ref{tab:success}.

\begin{table}[ht] 
\centering
\begin{tabular}{|c|c|}
    \hline
    State & Success ? \\
    \hline
    Good & $\checkmark$ \\
    \hline
\end{tabular}
\caption{Table of success.}
\label{tab:success}
\end{table}

\begin{table}[ht] 
\centering
\begin{tabular}{|c|c|}
    \hline
    State & Success ? \\
    \hline
    Bad & $\times$ \\
    \hline
\end{tabular}
\caption{Table of defeat.}
\label{tab:defeat}
\end{table}

\paragraph{}
But there is also Table~\ref{tab:defeat}.

\end{document}

enter image description here

Upvotes: 8

Related Questions