Cross-Referencing in LaTeX: \label, \ref, \eqref, \autoref and \cref
Quick answer
- Mark it:
\label{fig:plot}after\caption,\sectionor inside an equation. - Refer to it:
Figure~\ref{fig:plot},\eqref{eq:energy},\pageref{fig:plot}. - Add the name automatically:
\cref{fig:plot}withcleveref, or\autoref{fig:plot}withhyperref. - ?? 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}| Object | Where to put \label | Reference |
|---|---|---|
| Section, chapter | Right after \section{...} | Section~\ref{sec:intro} |
| Figure | After \caption, inside figure | Figure~\ref{fig:growth} |
| Table | After \caption, inside table | Table~\ref{tab:data} |
| Equation | Inside equation or on a line of align | \eqref{eq:model} prints (1) |
| List item | After \item | \ref{item:second} |
| Theorem, footnote | Inside 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 symptom | Cause | Fix |
|---|---|---|
?? in the PDF, Reference ‘key’ on page 1 undefined | Only one compile, or a misspelled key | Compile twice; check the key |
| A figure reference prints the section number | \label before \caption, or no caption | Put \label after \caption |
Reference to equation* or \section* prints the wrong number | Unnumbered items have no number of their own | Use the numbered form, or \tag{...} for an equation |
| LaTeX Warning: Label ‘fig:x’ multiply defined | The same key used twice, often in a copied figure | Give every label a unique key |
| Package cleveref Error: cleveref must be loaded after amsmath! | Wrong package order | amsmath, varioref, hyperref, then cleveref |
| Red or green boxes around every link | hyperref 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
- Equation Numbering – Numbering by section, \tag and \eqref
- Figures in LaTeX – Captions, placement and labels
- Sections and Headings – Numbered headings you can reference
- Hyperlinks – hyperref options, \url and \href
- BibTeX – Citations, which follow the same two-pass rule
Free LaTeX Tools
- LaTeX Preview – Compile any example above and see the result instantly
- AI LaTeX Generator – Describe a document and get the LaTeX
Related Articles
Delta Symbols in Math and Science: Complete Reference Guide
Zotero Citation Guide: Reference Management for Research
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.