LaTeX/Labels and Cross-referencing
From Wikibooks, the open-content textbooks collection
Another good point of LaTeX is that you can easily reference almost anything that is numbered (sections, figures, formulas), and LaTeX will take care of numbering, updating it whenever necessary. The commands to be used do not depend on what you are referencing, and they are:
\label{marker}- you give the object you want to reference a marker, you can see it like a name.
\ref{marker}- you can reference the object you have marked before. This prints the number that was assigned to the object
\pageref{marker}- It will print the number of the page where the object is.
LaTeX will calculate the right numbering for the objects in the document, the marker you have used to label the object will not be shown anywhere in the document. Then LaTeX will replace the string "\ref{marker}" with the right number that was assigned to the object. If you reference a marker that does not exist, the compilation of the document will be successful but LaTeX will return a warning:
LaTeX Warning: There were undefined references.
and it will replace "\ref{unknown-marker}" with "??" (so it will be easy to find in the document).
As you may have noticed reading how it works, it is a two-step process: first the compiler has to store the labels with the right number to be used for referencing, then it has to replace the \ref with the right number. That is why, when you use references, you have to compile your document twice to see the proper output. If you compile it only once, LaTeX will use the older information it collected in previous compilations (that might be outdated), but the compiler will inform you printing on the screen at the end of the compilation:
- LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.
Using the command \pageref{} you can help the reader to find the referenced object by providing also the page number where it can be found. You could write something like:
See figure~\ref{fig:test} on page~\pageref{fig:test}.
Since you can use exactly the same commands to reference almost anything, you might get a bit confused after you have introduced a lot of references. It is common practice among LaTeX users to add a few letters to the label to describe what you are referencing. Here is an example:
| chap: | chapter |
| sec: | section |
| fig: | figure |
| tab: | table |
| eq: | equation |
Following this convention, the label of a figure will look like \label{fig:my_figure}, etc. You are not obliged to use these prefixes. You can use any string as argument of \label{...}, but these prefixes become increasingly useful as your document grows in size.
Another suggestion: try to avoid using numbers within labels. You are better off describing what the object is about. This way, if you change the order of the objects, you will not be obliged to reference random pointless numbers.
If you want to be able to see the markers you are using in the output document as well, you can use the showkeys package; this can be very useful while developing your document. For more information see the Packages section.
Contents |
[edit] Examples
Here are some practical examples, but you will notice that they are all the same because they all use the same commands.
[edit] Sections
\section{Greetings} \label{sec:greetings} Hello! \section{Referencing} I greeted in section \ref{sec:greetings}. |
you could place the label anywhere in the section; anyway, in order to avoid confusion, it is better to place it immediately after the beginning of the section. Note how the marker starts with sec:, as suggested before. The label is then referenced in a different section.
[edit] Pictures
You can reference a picture by inserting it in the figure floating environment.
\begin{figure} \centering \includegraphics[width=0.5\textwidth]{gull} \caption{Close-up of a gull} \label{gull} \end{figure} Figure \ref{gull} shows a photograph of a gull. |
When a label is declared within a float environment, the \ref{...} will return the respective fig/table number, but it must occur after the caption. When declared outside, it will give the section number.
See the Floats, Figures and Captions section for more about the figure and related environments.
[edit] Fixing wrong labels
A label may sometimes pick up the section or list number instead of the figure number. In this case, put the label inside the caption to ensure correct numbering:
\begin{figure} \begin{center} \includegraphics[width=0.5\textwidth]{gull} \caption{Close-up of a gull \label{fig:gull} } \end{center} \end{figure}
[edit] Issues with links to tables and figures handled by hyperref
In case you use the package hyperref to create a PDF, the links to tables or figures will point to the caption of the table or figure, which is always below the table or figure itself[1]. Therefore the table or figure will not be visible, it is above the pointer and one has to scroll up in order to see it. If you want the link point to the top of the image you can use the package hypcap [1] with:
\usepackage[all]{hypcap}
Be sure to call this package after the package hyperref, which should otherwise be loaded last.
[edit] Formulas
Here is an example showing how to reference formulas:
as you can see, the label is placed soon after the beginning of the math mode. In order to reference a formula, you have to use an environment that adds numbers. Most of the times you will be using the equation environment, that is the best choice for one-line formulas, if you are using amsmath or not. Note also the eq: prefix in the label.
[edit] eqref
The amsmath package adds a new command for referencing formulas, it is \eqref{}. It works exactly like \ref{}, but it adds brackets so that, instead of printing a plain number as 5, it will print (5). This can be useful to help the reader distinguishing between formulas and other things, without the need to repeat the word "formula" before any reference. It's output can be changed as you wish, for more information see the amsmath documentation.
[edit] The varioref package
The varioref package introduces a new command called \vref{}. This command is used exactly like the basic \ref, but it has a different output according to the context. If the object to be referenced is in the same page, it works just like \ref; if the object is far away it will print something like "5 on page 25", i.e. it adds the page number automatically. If the object is close, it can use more refined sentences like "on the next page" or "on the facing page" automatically, according to the context and the document class.
This command has to be used very carefully. It outputs more than one word, so it may happen its output to stay between two different pages. In this case, the algorithm can get confused and cause a loop. Let's make an example. You label an object on page 23 and the \vref output happens to stay between page 23 and 24. If it were on page 23, it would print like the basic ref, if it were on page 24, it would print "on the previous page", but it is on both, and this may cause some strange errors at compiling time that are very hard to be fixed. You could think that this happens very rarely; unfortunately, if you write a long document you often edit with more than 100 references, situations like these are likely to happen. A possible way to avoid problems is to use the standard ref all the time, and convert it to vref when you are close to the final version of your document.
[edit] The hyperref package and \autoref{}
The hyperref package introduces another useful command; \autoref{}. This command creates a reference with additional text corresponding to the targets type, all of which will be a hyperlink. For example, the command \autoref{sec:intro} would create a hyperlink to the \label{sec:intro} command, wherever it is. Assuming that this label is pointing to a section, the hyperlink would contain the text "section 3.4", or similar (capitalization rules will be followed, which makes this very convenient). You can customize the prefixed text by redefining \typeautorefname to the prefix you want, as in:
\def\subsectionautorefname{section}
If you would like a hyperlink reference, but do not want the predefined text that \autoref{} provides, you can do this with a command such as \hyperref[sec:intro]{Appendix~\ref*{sec:intro}}. Note that you can disable the creation of hyperlinks in hyperref, and just use these commands for automatic text.
The hyperref package also automatically includes the nameref package, and similarly named command. It is similar to \autoref{}, but inserts text corresponding to the section name, for example.
[edit] The hyperref package and \phantomsection
When you define a \label outside a figure, a table, or other floating objects, the label points to the current section. In some case, this behavior is not what you'd like and you'd prefer the generated link to point to the line where the \label is defined. This can be achieved with the command \phantomsection as in this example:
%The link location will be placed on the line below. \phantomsection \label{the_label}
[edit] References
| Previous: Theorems | Index | Next: Indexing |

