Cross references (LaTeX2e unofficial reference manual (October 2018))

## Cross references

We often want something like ‘`See Theorem~31`’. But by-hand typing the 31 is poor practice. Instead you should write a label such as `\label{eq:GreensThm}` and then reference it, as with `See equation~\ref{eq:GreensThm}`. LaTeX will automatically work out the number, put it into the output, and will change that number later if needed.

    We will see this with Theorem~\ref{th:GreensThm}. % forward reference
    ...
    \begin{theorem} \label{th:GreensThm}
      ...
    \end{theorem}
    ...
    See Theorem~\ref{th:GreensThm} on page~\pageref{th:GreensThm}.

LaTeX tracks cross reference information in a file having the extension `.aux` and with the same base name as the file containing the `\label`. So if `\label` is in `calculus.tex` then the information is in `calculus.aux`. LaTeX puts the information in that file every time it runs across a `\label`.

The behavior described in the prior paragraph results in a quirk that happens when your document has a forward reference, a `\ref` that appears before the associated `\label`. If this is the first time that you are compiling the document then you will get ‘`LaTeX Warning: Label(s) may have changed. Rerun to get cross references right`’ and in the output the forward reference will appear as two question marks ‘`??`’, in boldface. A similar thing happens if you change some things so the references changes; you get the same warning and the output contains the old reference information. In both cases, resolve this by compiling the document a second time.

The `cleveref` package enhances LaTeX’s cross referencing features. You can arrange that if you enter `\begin{thm}\label{th:Nerode}...\end{thm}` then `\cref{th:Nerode}` will output ‘`Theorem 3.21`’, without you having to enter the “Theorem.”

### `\label`

Synopsis:

    \label{key}

Assign a reference number to key. In ordinary text `\label{key}` assigns to key the number of the current sectional unit. Inside an environment with numbering, such as a `table` or `theorem` environment, `\label{key}` assigns to key the number of that environment. Retrieve the assigned number with the `\ref{key}` command (see \ref).

A key name can consist of any sequence of letters, digits, or common punctuation characters. Upper and lowercase letters are distinguished, as usual.

A common convention is to use labels consisting of a prefix and a suffix separated by a colon or period. Thus, `\label{fig:Post}` is a label for a figure with a portrait of Emil Post. This helps to avoid accidentally creating two labels with the same name, and makes your source more readable. Some commonly-used prefixes:

`ch`

  for chapters

`sec`
`subsec`

  for lower-level sectioning commands

`fig`

  for figures

`tab`

  for tables

`eq`

  for equations

In the auxiliary file the reference information is kept as the text of a command of the form `\newlabel{label}{{currentlabel}{pagenumber}}`. Here currentlabel is the current value of the macro `\@currentlabel` that is usually updated whenever you call `\refstepcounter{counter}`.

Below, the key `sec:test` will get the number of the current section and the key `fig:test` will get the number of the figure. (Incidentally, put labels after captions in figures and tables.)

    \section{section name}
    \label{sec:test}
    This is Section~\ref{sec:test}.
    \begin{figure}
      ...
      \caption{caption text}
      \label{fig:test}
    \end{figure}
    See Figure~\ref{fig:test}.

### `\pageref`

Synopsis:

    \pageref{key}

Produce the page number of the place in the text where the corresponding `\label`{key} command appears.

If there is no `\label{key}` then you get something like ‘`LaTeX Warning: Reference `th:GrensThm' on page 1 undefined on input line 11.`’

Below, the `\label{eq:main}` is used both for the formula number and for the page number. (Note that the two references are forward references so this document would need to be compiled twice to resolve those.)

    The main result is formula~\ref{eq:main} on page~\pageref{eq:main}.
      ...
    \begin{equation} \label{eq:main}
       \mathbf{P}=\mathbf{NP}
    \end{equation}

### `\ref`

Synopsis:

    \ref{key}

Produces the number of the sectional unit, equation, footnote, figure, …, of the corresponding `\label` command (see \label). It does not produce any text, such as the word ‘Section’ or ‘Figure’, just the bare number itself.

If there is no `\label{key}` then you get something like ‘`LaTeX Warning: Reference `th:GrensThm' on page 1 undefined on input line 11.`’

In this example the `\ref{popular}` produces ‘`2`’. Note that it is a forward reference since it comes before `\label{popular}` so this document would have to be compiled twice.

    The most widely-used format is item number~\ref{popular}.
    \begin{enumerate}
    \item Plain \TeX
    \item \label{popular} \LaTeX
    \item Con\TeX t
    \end{enumerate}

The `cleveref` package includes text such as ‘`Theorem`’ in the reference. See the documentation on CTAN.