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.
To begin with, let's assume that we 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:jobInfoDialog}
\end{figure}
Let's further assume that as it turns out, in the current version of
my 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 blah blah blah
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
Pretty easy, eh?
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.