LaTeX examples - reference a figure or table

LaTeX table/figure FAQ - How do I reference a table or figure in a LaTeX document?

Being able to automatically reference a figure within a LaTeX document is a very cool thing. This referencing capability lets you easily give readers the exact number of a figure, or tell them what page number a figure is located on with the use of a few simple commands (\label, \ref, and \pageref). The same technique works for referencing other objects within a LaTeX document, including tables and equations.

Here’s the simple two-step process to use and reference figures in LaTeX documents.

To begin with, assume that you have a figure defined somewhere in a LaTeX document like this:

\pagebreak
\begin{figure}[here]
\includegraphics[width=0.9\textwidth]{images/JobInformationDialog.jpg}
\caption{A prototype of the Job Information dialog}
\label{fig:jobInformationDialog}
\end{figure}


(As you’ll see from the many comments below, the order of these lines is very important. Please read the comments for more details.)

Let’s further assume that as it turns out, in the current version of your document, this is Figure 11-1 in the document, and it appears on page 69.

Now, somewhere else in my document I want to make a reference to this figure. If I just want to refer to the figure number, I can get it to appear by writing LaTeX text like this:

Please see Figure ~\ref{fig:JobInformationDialog} for a prototype yada yada yada


When I create my output document, such as when I create a PDF with pdflatex, this results in the following output:

Please see Figure 11-1 for a prototype blah blah blah


Now, if I further want to refer to the page number that the image resides on, I can make a reference like this:

Please see Figure ~\ref{fig:JobInformationDialog} on page ~\pageref{fig:JobInformationDialog} for a prototype blah blah blah


If I now compile this statement with pdflatex I’ll end up with a PDF that has this output:

Please see Figure 11-1 on page 69 for a prototype blah blah blah


The \ref and \pageref commands make it very easy to refer to figures that contain labels. As you’ve seen, the text referenced by the \ref and \pageref commands must match the text used within the label command. (It seems to be a convention to begin this text with the string “fig:”. This isn’t really necessary, but because these labels must be unique within a document, it helps to organize your labels, and separate your labels for figures from your labels for tables and equations.

This way of referencing things is very powerful, because you can use the same technique to makes reference to other objects, like tables and equations.

Reference a Figure

Hello,
I've used a command \ref{fig:} to reference my figures, but I don't receive the same numbers in the text as they are in the images.
In my text I have sections and subsections. The image gets the section number (2 digits) while in the text it appears as a 3 digit number.
What should I change?

Thank you!

I don't think I've seen this

I don't think I've seen this before, but if you can provide a little more detail, I'll be glad to see if I can help.

I don't know if this will help, especially because it comes from my bad memory, but I used to see reference numbers wrong if I only ran pdflatex one time. As much as I can remember, I think I either ran a command named 'texify' to get the numbers right, or I ran pdflatex more than once ... I'm sorry, I can't remember right now, but I'll try to look at this when I get to my other computer. In the meantime, if you can share any more details, I'll be glad to help.

Reference a Figure

Hi Alvin,
Thank you very much for your reply, but I still didn't solve my problem.
I'll try to explain it better:
For example, I have 3 figures in Chapter 5. The figures will have reference numbers: 5.1, 5.2, and 5.3. But, if I refer to the first image in a subsection 5.1.3, in the text I will get the reference number 5.1.3 not 5.1. If I refer to the same figure but in a subsection 5.2.2 the text will contain the figure number 5.2.2.

Hope this will look more clear to you.

Thank you.

Sorry, just a quick apology

Sorry, just a quick apology here. Every time I start to look at this I get called away from it. I have written myself a note here, and will block off some time to look into this more tomorrow. I have a good-size document I should be able to test this on.

pdflatex test results

I'm sorry, I can't duplicate this problem, but I can share some of the code I've used to try to re-create it. First, I define my image and label like this:

\subsubsection{Post-Conditions}

FOO, and BAR.

\begin{figure}[Ht]
\includegraphics[width=.9\textwidth]{UseCases/MyImage.jpg}
\caption{The description of my image was here.}
\label{label:MyExampleLabel}
\end{figure}
\clearpage


Next, I reference this label from somewhere else in my document like this:

(Figure ~\ref{label:MyExampleLabel} on page ~\pageref{label:MyExampleLabel}) YADA YADA YADA


After I generate my PDF, the references look like this:

(Figure 4.2 on page 39) YADA YADA YADA


and the label on my image looks like this:

Figure 4.2: The description of the image was here.


These are correct, as this is the second image in this section of my document.

Depending on whether or not I use that \clearpage command, the image either shows up in Section 4.8.5 of my document, or it floats to wherever pdflatex thinks it should go.

I'm sorry I can't reproduce the problem ... thinking, thinking ... I haven't upgraded my pdflatex version in several years, so that could be a difference. I just ran pdflatex --version, and I see this output:

pdfeTeX 3.141592-1.21a-2.2 (Web2C 7.5.4)
kpathsea version 3.5.4


I guess I have the PI version of pdfeTeX. Also, I'm running this on Mac OS X 10.5.x.

One other thing I just thought of: Check your pdflatex log file and see if there are any clues in there.

Oops, one last note: here's the pdflatex message I used to see telling me my references were going to be wrong:

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.


Whenever I saw this message I knew I needed to run pdflatex again.

Thank you very much. I will

Thank you very much. I will try this later.
Hope it will help me.

Thank you!

Same Problem as Lijiljana

I have the exact problem. The label for the table seems to cause the reference to refer to the section the table is in instead of the table number itself. I am actually trying to find a way of correcting the problem when i came upon this thread. Hope somebody can help

Claudine

I figured it out....make sure

I figured it out....make sure to put your label after the caption for the table.

Claudine

thanks a lot claudine

yeah,
i came here for the same reason as you claudine, and yes you're right, that does fix it!
thx a lot

It doesn't work for me..

I have the same problem with my figures, and moving the label around doesn't seem to change anything. It's quite problematic when four figures give the same reference, which is just the name of the subsection they are placed in.

No luck for me either

Hi everyone,

I have the same problem with tables. I only have one table at the moment and its caption calls it 2.1 as it is in chapter 2. However when i refer to in in the text with \ref{table:mism} the result is table 2.2 (which is the chapter and section number - coincidence???)

This happens weather i put the text reference before or after the actual table.

I'm running
pdfTeX 3.1415926-1.40.9-2.2 (Web2C 7.5.7)
kpathsea version 3.5.7

i.e the most recent binary of TexShop for mac

Thanks

Mike

If anyone has a little code

If anyone has a little code they can share with me I'll be glad to run it on my system and see if I can get it to work. All the comments about this problem make me wonder what's up.

sample code - incorrect reference to table in text body

\documentclass[12pt]{report}
\usepackage{graphics}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{amssymb}
\usepackage{geometry}
\usepackage{mathrsfs}
\usepackage{color}
\usepackage[parfill]{parskip}

\definecolor{purp}{rgb}{0.9, 0.1, 0.9}

%40 left 30 top

\begin{document}
\setlength{\parindent}{0pt}

\setlength{\textwidth}{180mm}
\setlength{\oddsidemargin}{20mm}

\setlength{\textheight}{210mm}

\title{test doc}

\author{jony}
\date{\today}
\maketitle

\section{Abstract}
\section{Acknowledgments}

\tableofcontents
\listoffigures
\listoftables

\chapter{one}
here is some text, no tables.

\chapter{two}
Table \ref{table:mism} computes lattice mismatch for various films (horizontal) grown on different substrates and electrodes (vertical). The highlighted fields indicate trials to be undertaken.

\begin{table}[htdp]
\label{table:mism}
\begin{center}\begin{tabular}{lcccccccc} \hline & \textbf{STO} & \textbf{MAO} & \textbf{MgO} & \textbf{Fe$_3$0$_4$} & \textbf{LSMO} & \textbf{SRO} & \textbf{BFO} & \textbf{PZT} \\ \hline \textbf{STO} & -- & -3.465 & -7.841 & -7.501 & \color{green}{0.896} & \color{green}{-0.896} & -1.216 & 0 \\ \textbf{MAO} & 3.349 & -- & -4.230 & \color{green}{-3.901} & 4.215 & 2.482 & 2.173 & 0 \\ \textbf{MgO} & 7.271 & 4.058 & -- & \color{green}{0.316} & 8.102 & 6.440 & 6.143 & 0 \\ \textbf{Fe$_3$0$_4$} & 6.977 & 3.754 & -0.317 & -- & 7.811 & 6.144 & \color{blue}{5.846} & 0 \\ \textbf{LSMO} & -0.904 & -4.400 & -8.816 & -8.473 & -- & -1.809 & \color{blue}{-2.132} & 0 \\ \textbf{SRO} & 0.888 & -2.546 & -6.883 & -6.546 & 1.777 & -- & \color{blue}{-0.317} & 0 \\ \textbf{BFO} & 1.202 & -2.163 & -6.545 & -6.209 & \color{purp}{2.087} & \color{purp}{0.316} & -- & 0 \\ \textbf{PZT} & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0\end{tabular} \caption{Lattice mismatch calculations}
\end{center}
\end{table}

\chapter{Summary}

\bibliographystyle{unsrt}
\bibliography{bibliography}

\end{document}

First, thanks again for this

First, thanks again for this sample code. And second, I got this to work. The short answer is that I had to move the label to a position immediately after the caption, like this:

\caption{Lattice mismatch calculations}\label{table:mism}


The longer answer is that I dug through an old (but very good) book named The LaTeX Companion, where I found the following information (which I'm pretty much just copying and pasting, forgive me for this plagiarism). The first lines are background information, and the last line (in bold) describes the solution:

For building cross-reference labels, the "currently active" structural element of a document is determined in the following way. The sectioning commands (\chapter, \section, ...), the environments equation, figure, table, and the theorem family, as well as the various levels of the enumerate environment, and \footnote set the current reference string, which contains the number generated by LaTeX for the given element. This reference string is usually set at the beginning of an element and reset when the scope of the element is exited.

Notable exceptions to this rule are the table and figure environments, where the reference string is defined by the \caption commands. This allows several \caption and \label pairs inside one environment. As it is the \caption directive that generates the number, the corresponding \label command must follow the \caption command in question, otherwise an incorrect number will be generated.

(I don't think some of those sentences are grammatically correct, but I double-checked my typing.)

I don't know if this will help all the problems described here, but I hope it does.

Success

Hi Alvin,

Thank you so very much.

I have no idea why it works, but placing the label directly after the caption has solved the problem.

Cheers

Mike

Also worked for me!

I had the same problem with the figure being referenced by the section instead of just the number I wanted. And hooray, Alvin's solution of putting the label directly after the caption also worked for me.

caption/label

Thanks, the Post of Claudine helped me get the References straight, I also had the order of caption/label "wrong".

Fixed for me too

Thanks!!!

I can't believe changing the order to caption-label worked! I'm so happy! Thank you Claudine!!!

It solved the problem

Thnaks for the solution! It was a long standing problem for me.

Thanks!

Me too! It worked, moving the label around after the caption.

Thanks; also note the utility

Thanks to Claudine and to Alvin. Add me to the list of "me too".

Must be one of the fragile commands ... finicky about the order and
contiguousness. As it says, it is because it will actually allow
several caption-label pairs within the same table or figure environment.
That is also a feature of great utility so the fragility has to be
tolerated.

Thanks...

...for the posts! I also switched the occurence of caption and label at some point in my doc and this thread helped me quickly.

Thanks for here also

This post was a great help to me too!

thanks, Claudine

Having spent half an hour trying everything I could think of without much luck, I can't say how delighted I was to see you figured a way out of the problem.

Cheers
Mike

Thanks to Alvin

Hi Alvin,

Thank you so very much. Your suggestion indeed solved my problem

ANDY

Thank you!

I struggled with the same figure numbering issue for a couple hours until I found this post. Thanks!

It worked.

Thank you, I'm glad I found this post! Saved me some hours of debugging!

Thanks a lot!

Thanks for the help!

The form \caption{blah blah}\label{fig:num} works for me, too. I really had no idea how to get it right! Thank you once again! :)

This post helped me a lot!

This post helped me a lot! Thank you all big time!

Well done Claudine and Alvin.

Well done Claudine and Alvin.

Thanks for the help.

I just ran into the same problem with the figure references. I guess I used to blindly use the correct sequence and couldn't figure out what I was doing wrong today. Thanks for the solution.

Thanks Claudine and Alvin,

Thanks Claudine and Alvin, moving the label after the caption worked for me 2!!!

Thanks

Hi Alvin,

thanks a lot for this help. I had been struggling with incorrect numbering of a single table in my thesis and wasn't able to solve it. Today I decided to be patient and solve it. I stumbled upon your post here and to my relief it is working. thanks again,

-
Guru

Reference of Figure

Thank You so much, I had the same problem

Thanks Claudine!

Thanks so much! I have been having the same problem for a while!

figure labels

This also worked for me, move the caption to the line before the label.