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.
Result
The compiled example

Open in the editor

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}
Result
The compiled example

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.
Result
The compiled example

Open in the editor

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.
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages