19

I wrote a long table with a label in a tex file and \input it into my main tex file. The reference in the main tex file to the table, however, does not show the numbering of the table but the one of the next table that are written directly in the main tex file. All long tables that are written directly in the main tex file have correct references. How to fix my problem? Must the label be defined in the same tex file as its reference?

The label is \label{tab:yy}, and the reference is \ref{tab:yy}, and every table has a distinct label.

6
  • If you have not already done so, check for spelling and coping errors in the \label's. Also for duplicate \labels. I've been stymied by that kind of thing once or twice. Commented Mar 19, 2010 at 16:01
  • The label is \label{tab:yy}, and the reference is \ref{tab:yy}, and every table has a distinct label.
    – Tim
    Commented Mar 19, 2010 at 16:34
  • 2
    I had the same error! Unless I moved the label inside the caption, I was getting all kinds of wacky errors on the referenced tables and figures! Thank goodness I found this conversation!!! SO: moral of the story, just put the \label{uniquelabelhere} inside the caption affiliated with the intended referenced figure or table!
    – user12866
    Commented Mar 22, 2012 at 23:26
  • 3
    One of the stupidities of LaTeX. I never understood why, but as a matter of fact you have to put the caption above the label, e.g. \caption{Picture} \label{pic} otherwise LaTeX will get it wrong.
    – Daniel
    Commented May 7, 2012 at 20:58
  • 6
    The \label macro always labels the last thing which increased a counter. If you put it before \caption you are labeling usually the last sectioning command. This makes perfectly sense and is not a stupidity. This fact is also usually mentioned in many beginner texts about LaTeX. Commented May 7, 2012 at 21:43

5 Answers 5

33

This may or may not help, but I've had similar problems with figure references where the figure number inserted into the text in place of the \ref command was wrong. I managed to solve it by putting the \label inside the figure's \caption, viz:

\begin{figure}
\caption{Some caption.\label{fig:some-figure}}
\includegraphics{some-figure.eps}
\end{figure}

It's difficult to say more without a more extensive example of what's going wrong, though!

3
  • Thanks! It works. I put \label just before \end when it did not work. Why is this?
    – Tim
    Commented Mar 19, 2010 at 17:06
  • @Tim: I'm not entirely sure! If anyone else could answer this I'd be interested to hear. Commented Mar 19, 2010 at 17:07
  • 3
    I think it has something to do with when the counter is generated, see this answer: tex.stackexchange.com/questions/23385/…
    – Steve Heim
    Commented Apr 27, 2015 at 0:02
7

In my case I solved it by writing \caption and \label together.

\caption{Test table}
\label{tb:testTable}
3
  • @random: Your edit made it worse instead of better. In LaTeX-code shouldn't be any HTML tags. Commented Aug 17, 2011 at 1:04
  • That's what you get for kludging through and not using the toolbar by hacking in HTML tags to display code. @pau
    – random
    Commented Aug 17, 2011 at 1:15
  • 3
    Did do the trick for me, so the order is important.
    – jmjr
    Commented Jun 8, 2016 at 12:06
1

Could it be that you need to rerun (La)TeX in order to imbed the new .aux file with the updated references?


OK, not that.

Could you post a minimal example that fails?


Sorry, don't know why @Will's answer works. Neither of my two bibles (the memoir manual and the Guide to LaTeX2e) says anything. The examples from there show \label{} coming right after \caption{}, not inside it.

???

2
  • Thanks! But no matter how many times latex command is run, the numbering is still wrong.
    – Tim
    Commented Mar 19, 2010 at 15:48
  • See the answer of Will. Do you know why?
    – Tim
    Commented Mar 19, 2010 at 18:06
0

I had this frustrating experience too. Even followed the exact syntax that worked successfully in another file. I ended up deleting .aux, .log, and .gz files, running it again, and it worked.

1
  • 1
    Welcome to tex.sx. If the wrong reference has appeared in an .aux file, it's a good idea to delete that .aux file, because that's what's going to be used in the next compilation, regardless of whether corrections have been made in the main source. Better to have a question mark in the next compilation as a reminder. Commented Jul 4 at 21:54
-1

I am using memoir and I learnt that the label has always to go after the caption.

2
  • 1
    It can also be in the caption command \caption{\label{tab:yy} here is the caption}.
    – Ludovic C.
    Commented Jul 9, 2013 at 8:24
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Commented Jul 9, 2013 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .