The referencing functions in LaTeX are pretty powerful. In this article we want to illustrate some of those features and present packages that extend on them.
The basic functionality is easy to understand: place a \label{key}
behind a chapter, sectioning command or an image or table and assign a unique (!) key to it. Then use \ref{key}
and \pageref{key}
commands to reference the corresponding counter and the page. The following example illustrates this, but keep in mind that you need to compile twice: the first run stores the information on the counter value and page number in the .aux
file. In the second run the information is printed out to the PDF.
\documentclass{article} \begin{document} \section{Introduction}\label{sec:intro} \section{Definitions} See section \ref{sec:intro} on page \pageref{sec:intro} \end{document}
As you may have noticed, we did not simply use an arbitrary key but added “sec:” in front of it. There are two main reasons: a) it may be easier to remember and b) there are LaTeX packages which can use the information from the label to do some sophisticated automation. For the moment, we simply advise you to use the following prefixes for your labels:
fig
for figurestab
for tablessec
for sectionsch
for chapterslis
for sourcecode listings
Displaying the defined labels
Especially in longer documents, it may be difficult to track which labels have been defined. In that case, the showlabels package can be very helpful (See this previous article for more details). You can simply load the package via \usepackage{showlabels}
. Moreover, the package provides options to define the location of the label to be printed. For example, outer
or inner
for outer and inner margins. See the package documentation for more details.
\documentclass{article} \usepackage{showlabels} \begin{document} \section{Introduction}\label{sec:intro} \section{Definitions} See section \ref{sec:intro} on page \pageref{sec:intro} \end{document}
Enhanced references with varioref
The varioref package is a versatile package, enhancing LaTeX’s referencing mechanisms. For example, it provides the \vref{key}
command which combines \ref
with \pageref
to “\ref{key} on \pageref{key}”. This notation is quite common in scientific writing. The command is more powerful than it may seem. For references to the previous or following page, the package automatically switches from “on page x” to “on the previous page” or “on the following page”, respectively. In addition, the package provides the \vrefrange{key1}{key2}
command, which allows to reference a range of references on multiple pages. The following example illustrates these features (compile it twice!):
\documentclass{article} \usepackage{showlabels} \usepackage{varioref} \begin{document} \section{Introduction}\label{sec:intro} \clearpage \section{Definitions}\label{sec:defs} See section \vref{sec:conclusion} \clearpage \section{Conclusion}\label{sec:conclusion} See section \vref{sec:intro} and \vref{sec:defs}. See sections \vrefrange{sec:intro}{sec:defs} \end{document}
Intelligent referencing with prettyref
In the last example above, we still had to write “section” with the \ref{key}
. The prettyref package allows us to skip this step if we use consistent prefixes like the ones defined above. The following example illustrates this: we load the prettyref
package and define how labels of type “sec:key” are to be printed. Note: instead of the shown section \ref{#1} on page \pageref{#1}
one could simply use the \vref{#1}
, as the varioref
and prettyref
package work well together.
\documentclass{article} \usepackage{showlabels} \usepackage{varioref} \usepackage{prettyref} \newrefformat{sec}{section \ref{#1} on page \pageref{#1}} % alternatively % \newrefformat{sec}{section \vref{#1}} \begin{document} \section{Introduction}\label{sec:intro} \clearpage \section{Definitions}\label{sec:defs} See section \vref{sec:conclusion} \clearpage \section{Conclusion}\label{sec:conclusion} See section \vref{sec:intro} and \vref{sec:defs}. See sections \vrefrange{sec:intro}{sec:defs}. With prettyref we simply write \verb|\prettyref{sec:intro}| to get \prettyref{sec:intro}. \end{document}
Cleveref, a clever way to reference
The cleverref package (https://ctan.org/pkg/cleveref) was introduced in a separate article on this blog.