LaTeX examples: How to 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.

1) Define your LaTeX figure

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: The \caption tag must come before the \label tag. 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.

2) Reference the figure

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.

Referencing a table

Here’s an example of how I reference a LaTeX table (a longtable in this case):

Table ~\ref{table:prosConsOptionalApproaches} summarizes
the benefits and drawbacks (``Pros and Cons'') of each 
approach.

\begin{longtable}[]{@{}lll@{}}
\toprule
Construct & Benefits & Drawbacks\tabularnewline
\midrule
\endhead
Option & Straightforward & Don't get error reason\tabularnewline
Try & Returns error reason & Requires exception\tabularnewline
Or & Returns error reason & Third-party library\tabularnewline
Either & Returns error reason & Works by convention\tabularnewline
\bottomrule
\caption{The pros and cons of Scala's optional classes}
\label{table:prosConsOptionalApproaches}
\end{longtable}

This image shows how that LaTeX text is converted into a table in a PDF:

How to reference a LaTeX table

Summary

In summary, if you wanted to see how to reference a figure or table in LaTeX, I hope these examples are helpful.