Cross-Referencing in LaTeX: \label, \ref, \eqref, \autoref and \cref

Quick answer

  • Mark it: \label{fig:plot} after \caption, \section or inside an equation.
  • Refer to it: Figure~\ref{fig:plot}, \eqref{eq:energy}, \pageref{fig:plot}.
  • Add the name automatically: \cref{fig:plot} with cleveref, or \autoref{fig:plot} with hyperref.
  • ?? in the PDF: compile twice.
\documentclass{article}
\usepackage{amsmath}
\begin{document}

\section{Results}\label{sec:results}
\begin{equation}
  E = mc^2 \label{eq:energy}
\end{equation}
Section~\ref{sec:results} derives Equation~\eqref{eq:energy}
on page~\pageref{eq:energy}.

\end{document}

Never type a section, figure or equation number by hand. Give the item a label, refer to the label, and LaTeX fills in the number, keeping every reference right when you reorder chapters or add a figure. This guide covers \label and \ref for each kind of object, \eqref and \pageref, why references show ?? until the second compile, clickable links with hyperref, automatic names with \autoref and cleveref, and varioref. Every example compiles with pdfLaTeX.

\label and \ref

\label{key} remembers the number of the most recent numbered item: the current section, the last caption, an equation or a list item. \ref{key} prints that number and \pageref{key} the page it is on. A prefix in the key (sec:, fig:, tab:, eq:) is only a naming habit, but it keeps hundreds of labels readable:

\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\begin{document}

\section{Introduction}\label{sec:intro}
We measure growth (Section~\ref{sec:method}), plot it in
Figure~\ref{fig:growth} on page~\pageref{fig:growth}, list the data in
Table~\ref{tab:data} and fit Equation~\eqref{eq:model}.

\section{Method}\label{sec:method}
\subsection{Model}\label{sec:model}
\begin{equation}
  y = a e^{bt} \label{eq:model}
\end{equation}

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.5\linewidth]{example-image}
  \caption{Growth over time.}
  \label{fig:growth}
\end{figure}

\begin{table}[ht]
  \centering
  \caption{Measured values.}
  \label{tab:data}
  \begin{tabular}{cc}
    $t$ & $y$ \\
    0   & 1.0 \\
    1   & 2.7
  \end{tabular}
\end{table}

\begin{enumerate}
  \item First step.
  \item Second step.\label{item:second}
\end{enumerate}
Step~\ref{item:second} matters most; subsection~\ref{sec:model} explains why.

\end{document}
ObjectWhere to put \labelReference
Section, chapterRight after \section{...}Section~\ref{sec:intro}
FigureAfter \caption, inside figureFigure~\ref{fig:growth}
TableAfter \caption, inside tableTable~\ref{tab:data}
EquationInside equation or on a line of align\eqref{eq:model} prints (1)
List itemAfter \item\ref{item:second}
Theorem, footnoteInside the theorem environment or the footnote text\ref as usual

The tie ~ stops a line break between the word and the number. Equation numbering schemes and \tag are covered in equation numbering; captions and float placement in the figures guide; heading numbering in sections and headings.

?? Marks and the Two-Pass Rule

LaTeX reads the document once from top to bottom, so on the first compile it cannot know the number of a figure that appears later. It writes every label to the .aux file and prints ?? for anything it has not yet seen. The next compile reads the .aux and fills the numbers in. The log tells you when another run is needed:

LaTeX Warning: Reference `fig:growth' on page 1 undefined on input line 7.
LaTeX Warning: There were undefined references.
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

latexmk, Octree and Overleaf rerun automatically until the labels settle. If a ?? survives a second compile, the label does not exist under that name: check the spelling (keys are case-sensitive) and that the \label is not commented out or in a file the document never inputs. Citations work the same way; see BibTeX.

Clickable References with hyperref and \autoref

Load hyperref (near the end of the preamble) and every \ref, \eqref, citation and table-of-contents entry becomes a link. It also adds \autoref, which prints the name with the number, and \nameref, which prints the title:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\hypersetup{
  colorlinks = true,     % coloured text instead of boxes
  linkcolor  = blue,     % \ref, \eqref, table of contents
  citecolor  = teal,     % \cite
  urlcolor   = magenta   % \url, \href
}
\begin{document}

\section{Background}\label{sec:background}
\begin{equation}
  a^2 + b^2 = c^2 \label{eq:pythagoras}
\end{equation}

The theorem in \autoref{sec:background} (\nameref{sec:background})
is \autoref{eq:pythagoras}.

\end{document}

That prints “The theorem in section 1 (Background) is Equation 1”: hyperref names sections in lower case and equations without parentheses. Change a name with \renewcommand{\sectionautorefname}{Section}.

Without colorlinks, links get coloured boxes that show on screen but not in print. Link styles, \url and \href are covered in the hyperlinks guide.

cleveref: \cref and \Cref

cleveref goes further than \autoref: it names the type, groups several labels, compresses ranges and lets you set every name. Load it after hyperref and amsmath:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage[capitalise, noabbrev]{cleveref}
\begin{document}

\section{Model}\label{sec:model}
\begin{align}
  x &= 1 \label{eq:a} \\
  y &= 2 \label{eq:b} \\
  z &= 3 \label{eq:c}
\end{align}

\Cref{sec:model} defines \cref{eq:a}, and \cref{eq:a,eq:b,eq:c}
follow from it; see also \crefrange{eq:a}{eq:c}.

\end{document}

That prints “Section 1 defines Equation (1), and Equations (1) to (3) follow from it”. Without the options, \cref abbreviates (“eq. (1)”, “fig. 2”); use \Cref at the start of a sentence. Change a name with \crefname{figure}{Fig.}{Figs.}.

varioref: References with the Page

varioref's \vref adds the page only when it helps: nothing if the target is on the same page, “on the next page” or “on page 7” otherwise. With cleveref loaded after it, \vref also adds the name. The order is varioref, then hyperref, then cleveref:

\documentclass{article}
\usepackage{graphicx}
\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}

As \vref{fig:map} shows, the route is short.
\clearpage

\begin{figure}[ht]
  \centering
  \includegraphics[width=0.4\linewidth]{example-image-a}
  \caption{The route.}\label{fig:map}
\end{figure}

\end{document}

Common Mistakes

Message or symptomCauseFix
?? in the PDF, Reference ‘key’ on page 1 undefinedOnly one compile, or a misspelled keyCompile twice; check the key
A figure reference prints the section number\label before \caption, or no captionPut \label after \caption
Reference to equation* or \section* prints the wrong numberUnnumbered items have no number of their ownUse the numbered form, or \tag{...} for an equation
LaTeX Warning: Label ‘fig:x’ multiply definedThe same key used twice, often in a copied figureGive every label a unique key
Package cleveref Error: cleveref must be loaded after amsmath!Wrong package orderamsmath, varioref, hyperref, then cleveref
Red or green boxes around every linkhyperref defaults\hypersetup{colorlinks=true}, or hidelinks

Frequently Asked Questions

How do I reference a figure, table or section in LaTeX?

Put \label{fig:plot} right after the \caption of the figure (or after \section{...} for a section), then write Figure~\ref{fig:plot} in the text. \ref prints the number, \pageref{fig:plot} prints the page, and the ~ keeps the word and number on the same line.

Why does my LaTeX reference show ?? instead of a number?

LaTeX writes labels to the .aux file during one compile and reads them in the next, so a new reference shows ?? until you compile twice. If it stays, the label is misspelled, missing, or inside a part of the document that is not compiled. The log lists each one as “Reference `key' on page 1 undefined”.

Why does \ref give the section number instead of the figure number?

The \label comes before \caption, or the figure has no caption. A label picks up the last number that was stepped, and inside a figure that is the caption counter only after \caption has run. Move \label to just after \caption, inside the same figure or table.

What is the difference between \ref, \autoref and \cref?

\ref prints only the number, so you type “Figure” yourself. \autoref from hyperref adds the name (“Figure 3”) automatically. \cref from cleveref does the same, handles lists and ranges such as “figs. 2 and 3”, and lets you configure every name; \Cref capitalises it at the start of a sentence.

How do I change the colour of reference links in LaTeX?

Load hyperref and set \hypersetup{colorlinks=true, linkcolor=blue, citecolor=teal, urlcolor=magenta}. colorlinks colours the text instead of drawing boxes; linkcolor covers \ref, \eqref, \cref and the table of contents. Set the colours to black for a printed copy.

Next Steps

Free LaTeX Tools

No more ?? in your PDF

Octree reruns LaTeX until every reference resolves. Its AI finds the label behind a stray ??, adds missing labels and switches your references to \cref, with the PDF next to your code.

Try Octree Free →