LaTeX: any way to autonumber a table/figure reference

I’m in the middle of writing a LaTeX document that will eventually have many tables and possibly several figures. I’m writing individual sections as I go so that I don’t have to go through a painful marathon writing session at the end. What I would like to do is to be able to refer to the tables I already have and have the numbering performed automatically, rather than having to make the text and table numbers match up myself. Is there any way to do this? Or am I just going to have to be really careful here?

It’s been a while, but I think you can do it the same way you label equations. From memory, it’s something like /label(nameofmytable) and then reference with /ref(nameofmytable). I have a bunch of latex documents on my work computer where I do this, and sadly I’ll be there later tonight, so I’ll check the exact syntax later if no one comes around and confirms it before then.

Yep, that does it. For reference, the exact syntax is \label{key} and ~\ref{key}.

Any idea why all my table references are rendered as Table 1 no matter what keys I use?

That’s weird. Not that this is it, but you can get problems if the \label{} tag isn’t in the right place. I usually put mine here:



\begin{table}
stuff
\caption{This is a table.}\label{mytable}
\end{table}

As Table~\ref{mytable} shows, the SMDB is awesome.


Although you may already know this, the ~ character isn’t part of the \ref{} command. It puts in a non-breaking space to prevent a line wrap between “Table” and “<number>”. You can use it anywhere (e.g., “Mr.~Smith”).

Is the table number correct in the caption of each table?

Have you run LaTeX on the source code twice? IIRC it takes one pass to store all the table labels in an auxiliary file, and on the next pass it uses this info to fill in the \ref.

Are you getting any error messages from LaTeX?

You must have the label after the caption command. You also need to run the LaTeX process three times, for it to generate the correct labels. Generally, the process is this:

pdflatex docname
bibtex docname
pdflatex docname
pdflatex docname

This was the problem. It kinda makes sense given what I’ve been able to dig up on how the underlying Tex looks, but I don’t think I would’ve put it together on my own for a while longer. Sure would be nice if this were documented somewhere…

Thanks for the help.

http://www.tex.ac.uk/cgi-bin/texfaq2html?label=crossref

True in this bibtex example, but if he’s not using bibtex, then running two cycles is sufficient.

At least some of the table/figure handling is dependent on the style file you’re using. For instance, the style for AAAI conference papers uses Arabic numbering (e.g., 1, 2, …), while the IEEE style uses Roman numerals (e.g., I, II, …). Some use a particular font, some abbreviate (e.g., “Fig.” vs. “Figure”).

If you have the time and the desire (and didn’t already know it existed), you might want to check out the CTAN repository. Also, again if you didn’t know this already, you can put your sections in separate files and use the \include command (see this for a quick summary).