Referencing equations, figures and tables in LaTeX

Help centre › LaTeX › Equations · Packages: amsmath, cleveref

Give the numbered thing a \label{fig:map} (after \caption in a figure or table, inside the equation for an equation) and write Figure~\ref{fig:map} or \eqref{eq:energy} where you mention it. LaTeX fills in the numbers on the second compile; with the cleveref package, \cref{fig:map} also writes the word "Figure" for you.

A complete example

A whole document: paste it into an empty project and compile it.

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\begin{document}
\section{Results}\label{sec:results}
The energy is given by
\begin{equation}
  E = mc^2 . \label{eq:energy}
\end{equation}
\begin{figure}[ht]
  \centering
  \rule{4cm}{2cm}
  \caption{A placeholder figure.}
  \label{fig:map}
\end{figure}
\begin{table}[ht]
  \centering
  \caption{Two values.}
  \label{tab:values}
  \begin{tabular}{lr}
    a & 1\\
    b & 2
  \end{tabular}
\end{table}
Section~\ref{sec:results} uses Equation~\eqref{eq:energy},
shows Figure~\ref{fig:map} on page~\pageref{fig:map} and lists Table~\ref{tab:values}.
\end{document}

How \label and \ref work

\label records the number of the most recent numbered item — the last \caption, equation, section or list item — in the .aux file. \ref reads it back on the next compile. On the first compile every reference prints as ??; compile once more and the numbers appear.

That is why \label must come after \caption in a figure or table: placed before it, the label picks up the section number instead.

The commands

  • \ref{key}: the number only — write the word yourself, Figure~\ref{fig:map}. The ~ is a non-breaking space, so the number never starts a line.
  • \eqref{key} (amsmath): the equation number in parentheses, (1).
  • \pageref{key}: the page the item is on.
  • \autoref{key} (hyperref): the number with its name, Figure 1.
  • \cref{key} and \Cref{key} (cleveref): the number with its name, lowercase or capitalized, and lists and ranges such as \cref{eq:a,eq:b,eq:c}.

cleveref

cleveref knows what each label points to and writes the name: \cref{fig:map} gives "fig. 1", \Cref{fig:map} "Figure 1" at the start of a sentence. Options set the style: capitalise always capitalizes, noabbrev writes names in full. Load it after hyperref and amsmath.

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage[capitalise,noabbrev]{cleveref}
\begin{document}
\begin{align}
  a &= b \label{eq:a}\\
  c &= d \label{eq:b}
\end{align}
\begin{figure}[ht]
  \centering
  \rule{3cm}{1cm}
  \caption{A placeholder.}\label{fig:one}
\end{figure}
\Cref{fig:one} illustrates \cref{eq:a,eq:b}.
\end{document}

Choosing label names

Prefix each label with its kind — fig:, tab:, eq:, sec:, alg: — so you can tell them apart while writing. Labels may contain letters, digits, :, - and .; avoid spaces and special characters such as _, # and %. A label must be unique in the whole document, \input files included.

Equation numbering by section

\numberwithin{equation}{section} (amsmath) numbers equations 1.1, 1.2, 2.1 …; \counterwithin{figure}{section} does the same for figures. References follow automatically.

Common mistakes

In journal templates

Errors you may meet

In Manulyx

Insert → Cross-reference and Insert → Label write the commands, typing \ref{, \eqref{ or \cref{ lists your labels, and every compile runs the extra passes, so numbers never stay as ??.

Write in Manulyx for free · Journal templates

Related

The examples on this page were compiled with pdfLaTeX to check that they work as shown. LaTeX quick reference · LaTeX errors explained