56

I have created 2 tables in my LaTeX document:

\begin{table}[hb]
\centering
\begin{tabular}{|c|c|r|}
\hline
... not important...
\end{tabular}
\label{tab:2}
\caption{Določanje prioritet mojstrov znotraj registra AHB0\_EXTPRIO \cite[str.~507]{lpc3141datasheet}.}  
\end{table}

and

\begin{landscape}
\begin{table}[ht]
\tiny{
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
...not important...
\end{tabular}
}
\label{tab:1}
\caption{Tabela kriterijev.}
\end{table}
\end{landscape}   

When I reference to this 2 tables like this:

\ref{tab:1}
\ref{tab:2}

I get different numbers in text and in caption.

  • table 1: caption nr. 3.1 , nr. in text 3.1.2 (this table is located in subsection 3.1.2)
  • table 2: caption nr. 3.2 , nr. in text 3.1.3 (this table is located in subsection 3.1.3)

It looks like my tables get their subsection numbers which don't match the caption numbers. Why is this? How can I solve this?

71GA
  • 3,573

3 Answers3

95

What the reference is actually pointing to is the \caption because this is where the relevant counter is \refstepcountered. That is, until LaTeX has read the caption, the label will point to the last thing "visible" to the label/ref mechanism. In this case, this is the subsection heading.

So put the label after the counter and it should work as expected.

How the reference/label mechanism works is like this:

  • Various things (like \section, \begin{equation} and \caption) use \refstepcounter to move a counter forward and this makes that counter's value the thing that \label notices.
  • When you \label something, it writes stuff to the .aux file to the effect of "If you see this \ref, print a suitably formatted version of the counter's current value."
  • Things like equation will tell \label to pay attention to the equation counter while you're inside the environment. This is also true of table. So labels outside the environment will point to the section again.

So when you put the \label before the \caption, \label is set to refer to the last thing that set it, which is the subsection. So the label has to go after the caption and before the end of the environment for it to properly refer to the table.

Here is a little example to highlight the points above:

\documentclass{article}
\begin{document}
\stepcounter{section} % So that the labels have different values
\section{Example}
\begin{table}
  \centering
  \begin{tabular}{cc}
    a&b\\
    c&d
  \end{tabular}
  \label{tab:wrong}
  \caption{This is a table}
  \label{tab:right}
\end{table}
\label{tab:doublewrong}
\begin{equation}
  x=y
\end{equation}
\label{eq:foo}
Here is some text and a reference to the section: \ref{eq:foo}.
Here are references to the section (\ref{tab:wrong},\ref{tab:doublewrong}) and the table (\ref{tab:right})
\end{document}
David Carlisle
  • 757,742
Seamus
  • 73,242
  • 1
    Thanky for the detailed explanation. I really understand now. – 71GA Jul 18 '11 at 13:19
  • 1
    Excellent explanation, and the solution to a problem I thought I understood, but didn't! – Wesley Burr Nov 01 '13 at 18:39
  • This just burned me. That's a sneaky thing. I wonder If it would have been better implemented differently... – magu_ Jan 13 '16 at 19:40
  • What if my table has no caption, or I don't want a caption for my table - unless can I put the caption at the top? I'm using IEEEtran.cls - so how do I use labels and refs with a table that has no caption? – Mark Mikofski Jun 05 '19 at 04:07
  • @MarkMikofski If your table has no caption, how do you expect people to know what "Table 1" refers to!? You have to at least have some signal that this table is Table 1. That text is the caption... – Seamus Jun 06 '19 at 11:39
  • 1
    Thanks @Seamus, the template I was using had \centerline{TABLE 1} \vskip5pt \centerline{\normalsize \textsc{Example Caption Here}} b/c I think they were trying to manually force the caption to the top. So following How to force table caption on top? I removed all of that and replaced it with \caption{this is my caption}, and it worked! Thanks for your help :) – Mark Mikofski Jun 06 '19 at 17:15
  • I have a figure with multiple minipages and not one has a caption. How can I ensure that each minipage label correctly points to the corresponding image? No captions involved. – Rylan Schaeffer Jun 02 '20 at 23:45
24

You should always put \label after \caption, for example

...
\end{tabular}
\caption{Določanje prioritet mojstrov znotraj registra AHB0\_EXTPRIO \cite[str.~507]{lpc3141datasheet}.}  
\label{tab:2}
egreg
  • 1,121,712
1

Try placing \label inside the caption:

\begin{table} 
  \caption{\label{t:1} Tabe caption.}
  \begin{tabular}{c c}
    a & b \\  
    c& d \\
  \end{tabular}
\end{table}

Worked for me for statsoc document class.

Marschal
  • 107
Agne
  • 11