Cross-references in LaTeX (label, ref)
Mark the target with \label{key} and refer to it with \ref{key}, which prints the number of the section, figure, table or equation. The numbers are correct after the second run.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{amsmath}
\usepackage{graphicx}Refer to a section
Put \label directly after the heading. \ref prints only the number, so you write the word before it. The tilde ~ is a space at which the line does not break.
\section{Introduction}\label{sec:intro}
The method is described in section~\ref{sec:method}.
\section{Method}\label{sec:method}
Section~\ref{sec:intro} gave the motivation.Refer to a figure or table
In a figure or table, the number comes from \caption, so \label has to stand after it. A \label before the caption gives the number of the surrounding section.
Figure~\ref{fig:setup} shows the test setup.
\begin{figure}[h]
\centering
\includegraphics[width=4cm]{image.png}
\caption{Test setup}
\label{fig:setup}
\end{figure}Refer to an equation
Put \label inside the equation. \eqref from amsmath prints the number with its parentheses, as in (1).
\begin{equation}
E = mc^2 \label{eq:energy}
\end{equation}
Equation~\eqref{eq:energy} relates energy and mass.Refer to an item of a list
A \label after \item in an enumerate list stores the number of that item.
\begin{enumerate}
\item Calibrate the sensor. \label{step:calibrate}
\item Start the measurement.
\end{enumerate}
Repeat step~\ref{step:calibrate} after every change of the setup.Pitfalls
- Two question marks in place of a number mean that the reference is not resolved yet. Compile again; if they stay, the key in
\refmatches no\label, see Reference `sec:methods' on page 1 undefined. - Every key may be used for only one
\label. A second one gives the warning about multiply defined labels, see Label `sec:intro' multiply defined. - Keys are case-sensitive. Prefixes such as
sec:,fig:,tab:andeq:are a habit that keeps keys apart; LaTeX does not evaluate them.
Compiled with pdfLaTeX from TeX Live 2023.