8
Cross-Referencing

8.1 The Purpose of Cross-Referencing

Cross reference is the technical term for quoting yourself [27]. This is what you do when you say something like,

“As I said previously in the last chapter, …”

To get serious for a moment, in a written article we may often have occasion to refer the reader to something mentioned earlier 1 1 or sometimes to something yet to be said in the same document. This could be:

Such cross referencing can be done by hand, but if you revise your document and insert some new sections then changing all cross references manually is no easy task and just a labour which needs to be avoided. It is always better to automate such tedious tasks. 3 3 After all what’s a computer for, if not to do such mundane jobs?

8.2 Making LaTeX do the Work

The basic method of using cross references, see Section 8.1 for what we mean by cross reference, in LaTeX is simple. Suppose somewhere in the second section of our article, you want to refer to the first section. We assign a key to the first section using the command:

latex
\section{section name}\label{key}

and at the point in the second section where the reference is to be made , we type the command

latex
\ref{key}

Therefore the reference “see Section 8.1 …” in the first sentence of this section was produced by including the command \label{intro} in the command for the first section as follows:

latex
\section{The Purpose of Cross-Referencing}\label{intro}

and the command \ref{intro} at the place of reference in the second section as

latex
..., see Section \ref{intro} for...

The \label{key} for a section need not be given immediately after the \section{section name}. It can be given anywhere within the section.

The first time we run LaTeX on a file named, say, myfile.tex containing cross references, the reference information is written in an auxiliary file named myfile.aux and at the end of the run LaTeX prints a warning :

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

There is no need for an alarm as it is normal for this to happen. When a document is compiled for the first time, the compiler doesn’t have an idea on where the references are so in the first run it collects all the information and locates all the references, and then a second run gets the references right. The same thing happens when you’ve changed the reference information in any way, say, by adding a new section.

While the key in \label{key} can be any sequence of letters, digits or punctuation characters, it is convenient to use some mnemonic. 4 4 such as \label{limcon} for a section entitled “Limits and Continuity” rather than \label{ref98}.

Also, when you make a reference, it’s better to type ~\ref{limcon} than \ref{limcon} to prevent the possibility of the reference number falling off the edge.

8.2.1 Referencing Items

In addition to sectioning commands such as \chapter or \section, reference can also be made to an \item entry in an enumerate environment, by attaching a \label. For example the input [28]:

latex
In the classical \emph{syllogism} \begin{enumerate} \item All men are mortal.\label{pre1} \item Socrates is a man.\label{pre2} \item So Socrates is a mortal.\label{con} \end{enumerate} Statements (\ref{pre1}) and (\ref{pre2}) are the \emph{premises} and statement (\ref{con}) is the conclusion.

In the classical syllogism

1.
All men are mortal.
2.
Socrates is a man.
3.
So Socrates is a mortal.

Statements ( 1 ) and ( 2 ) are the premises and statement ( 3 ) is the conclusion.

Be careful about references to tables or figures (i.e., floats). For them, the \label command should be given after the \caption command or in its argument, as in the example below:

latex
\begin{table}[h] \begin{center} \setlength{\extrarowheight}{5pt} \begin{tabular}{|c|c|c|c|} \hline Value of $x$ & 1 & 2 & 3\\ \hline Value of $y$ & 1 & 8 & 27\\ \hline \end{tabular} \caption{Observed values of $x$ and $y$}\label{tabxy} \end{center} \end{table} Two possible relations betweeen $x$ and $y$ satisfying the data in Table\ref{tabxy} are $y=x3$ and $y=6x2-11x+6$

Which produces the following output:





Value of x 1 2 3




Value of y 1 8 27




Table 8.1: Observed values of x and y

Two possible relations betweeen x and y satisfying the data in Table 8.1 are y = x 3 and y = 6 x 2 1 1 x + 6 .

Think of a \caption command within a figure or table environment as a sort of sectioning command within the environment. Therefore, you can have several \caption and \label pairs within a single figure or table environment. You can also make forward references in exactly the same way by \ref -ing to the key of some succeeding \label.

8.2.2 Cross-Referencing in Math

Mathematical documents heavily use cross references. There are references to theorems and equations and figures and whatnot. The method of reference is exactly as before. Therefore if you’ve defined \newtheorem{theorem}[subsection], then after typing

latex
\begin{theorem}\label{diffcon} Every differentiable function is continuous \end{theorem}

we get: and you can type elsewhere in the document

latex
The converse of Theorem \ref{diffcon} is false.

to get:

The converse of Theorem ?? is false.

References can be made to equations as in the following examples:

latex
\begin{equation}\label{sumsq} (x+y)2=x2+2xy+y2 \end{equation} Changing $y$ to $-y$ in Equation (\ref{sumsq}) gives the following

gives:

( x + y ) 2 = x 2 + 2 x y + y 2 (8.1)

Changing y to y in Equation ( 8.1 ) gives the following

If you load the package amsmath , you can use the command \eqref instead of \ref to make a reference to an equation. This automatically supplies the parentheses around the equation number and provides an italic correction before the closing parenthesis, if necessary.

For example,

latex
Equation \eqref{sumsq} gives the following ..........

produces

Equation ( 8.1 ) gives the following …

References can be made to individual equations in multiline displays of equations produced by such environments as align or gather (defined in the amsmath package). The \label command can be used within such a structure for subnumbering as in the example below:

latex
\begin{align} (x+y)2&=x2+2xy+y2\label{sum}\\ (x-y)2&=x2-2xy+y2\tag{\ref{sum}a} \end{align}

( x + y ) 2 = x 2 + 2 x y + y 2 (8.2) ( x y ) 2 = x 2 2 x y + y 2 ( 8.2 a)

8.3 Pointing to a Page using the varioref Package

In making a reference to a table or an equation, it is more to convenient 5 5 Of course, the convenience here is for the reader, not for the writer. to give the page number of the reference also.

The command:

latex
\pageref{key}

typesets the number of the page where the command \label{key} was given. Therefore, for example

latex
see Table \ref{tabxy} in page \pageref{tabxy}

in this document produces

see Table 8.1 in page 265

To avoid the tedium of repeated by typing

latex
\ref{key} on page \pageref{key}

you can define the macro :

latex
\newcommand{\fullref}[1]{\ref{#1} on page \pageref{#1}}

and use \fullref for such references. However, the trouble is at times the referred object and the reference to it fall on the same page 6 6 With TeX this is unfortunately not possible until the document is compiled. so you get a reference to the page number of the very page you are reading, which looks funny.

This can be avoided by using the package varioref [29]. If you load this package by including the following code snippet, \usepackage{varioref}, in your preamble, then you can use the command:

latex
\vref{key}

to refer to an object we’ve marked with \label{key} elsewhere in the document. The action of \vref varies according to the page(s) where the referred object and the references are typeset by TeX in the final output.

1.
If the object and the reference are on the same page, \vref produces only a \ref suppressing \pageref so that only the number pointing to the object is typeset, without any reference to the page number.
2.
If the object and the reference are on different pages whose numbers differ by more than one, \vref produces both \ref and \pageref.
3.
If the object and the reference fall on pages whose numbers differ by one, 7 7 that is, on successive pages \vref produces \ref followed by the phrase “on the preceding page” or “on the following page” depending on whether the object or the reference occurs first. Moreover, in the next occurrence of \vref in a situation of the same type, the phrases are changed to “on the next page” and the “page before” respectively.

This is the default behaviour of \vref in the article class. If the article class is used with the twoside option or if the book class is used, then the behaviour in (3) above is a bit different.

1.
If the object and the reference fall on the two sides of the same leaf, the behavior of \vref is as in (3) above.
2.
If the object and the reference fall on pages forming a double spread (that is, a page of even number followed by the next page), then \vref produces \ref followed by the phrase “on the facing page”. Moreover, in the next occurence of \vref in a situation of the same type, the phrases are changed to “on the preceding page” and “on the next page” respectively.

The phrases used in the various cases considered above can be customized by redefining the commands used in generating them. For the article class without the twoside option, reference to the previous page uses the command \reftextbefore and reference to the next page uses \reftextafter.

In the case of the article class with the twoside option or the book class, the commands \reftextfaceafter and \reftextfacebefore are used in the case of reference to a page in a double spread. The default definitions of these commands are given below.

latex
\newcommand{\reftextbefore} {on the \reftextvario{preceding page}{page before}} \newcommand{\reftextafter} {on the \reftextvario{following}{next} page} \newcommand{\reftextfacebefore} {on the \reftextvario{facing}{preceding} page} \newcommand{\reftextfaceafter} {on the \reftextvario{facing}{next}{page}}

In all these, the two (2) arguments of the command \reftextvario are phrases alternatively used in the repeated use of the reference as mentioned above. You can customize the phrases generated in various situations by redefining these with phrases of your choice in the arguments of \reftextvario.

If you want to refer only to a page number using \varioref, you can use the command:

latex
\vpageref{key}

to produce the page number of the object marked with \label{key}. The phrases used in the various special cases are the same as described above, except that when the referred object and the reference fall on the same page, either the phrase “on this page” or “on the current page” is produced. The command used to generate these is \reftextcurrent whose default definition is:

latex
\newcommand{\reftextcurrent} {on \reftextvario{this}{the current} page}

You can change the phrases “this” and “the current” globally by redefining this command. You can also make some local changes by using the two (2) optional arguments that \vpageref allows. Thus you can use the command:

latex
\vpageref[same page phrase][other page phrase]{key}

to refer to the page number of the object marked with \label{key}. The same page phrase will be used if the object and the reference fall on the same page and the phrase other page phrase will be used, if they fall on different pages. Therefore for example, the command:

latex
see the \vpageref[above table][table]{tabxy}

given in this document will produce

see the table on page 265

8.4 The cleveref Package

Using standard cross-referencing in LaTeX only produces the label number, a name describing the label such as figure, chapter or equation has to be added manually. The cleveref package overcomes this limitation by automatically producing the label name and number. It further allows cross-referencing ranges of labels and multiple labels of the same or different kinds, including auto-sorting and compression of labels [30].

The package implements \cref for basic cross-referencing. The command is used in the same way as the standard \ref command. Besides the label number it also produces the reference kind.

Here is an example:

latex
\documentclass[11pt]{article} \usepackage{graphicx} \usepackage{amsmath} \usepackage{cleveref} \begin{document} \begin{align} y&=a_1x+b_1\label{eqn:1} \end{align} \noindent Standard equation reference (\textbackslash ref): \ref{eqn:1}\\ Cleveref equation reference (\textbackslash cref): \cref{eqn:1} \begin{figure}[ht]\centering\rule{0.5\linewidth}{0.1\linewidth}\caption{First figure}\label{fig:1}\end{figure} \noindent Standard figure reference (\textbackslash ref): \ref{fig:1}\\ Cleveref figure reference (\textbackslash cref): \cref{fig:1} \end{document}

To cross-reference multiple labels of the same or different kinds, the \cref command is used. Labels are separated by commas without white-space (\cref{ref1,ref2,etc.}). For a range of the same label kind, the command \crefrange{first}{last} is available.

latex
\documentclass[11pt]{article} \usepackage{graphicx} \usepackage{amsmath} \usepackage{cleveref} \begin{document} \begin{align} y&=a_1x+b_1\label{eqn:1}\\ y&=a_2x+b_2\label{eqn:2}\\ y&=a_3x+b_3\label{eqn:3}\\ y&=a_4x+b_4\label{eqn:4} \end{align} \noindent Range example: \crefrange{eqn:1}{eqn:4} \begin{figure}[ht]\centering\rule{0.5\linewidth}{0.1\linewidth}\caption{First figure}\label{fig:1}\end{figure} \noindent Mixed references example: \cref{eqn:1,eqn:3,eqn:4,fig:1} \end{document}

By default, label names are produced with a small initial letter. To capitalize the first letter at the beginning of a sentence, use \Cref and \Crefrange instead.

For capitalization of all label names throughout the document, load the package with the capitalise option.

latex
\usepackage[capitalise]{cleveref}