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
- A label before
\captionrefers to the section, not the figure. - A label in an unnumbered environment (
equation*,align*, a line with\notag) has no number of its own, so the reference shows the previous number or the section. - In
align, each line needs its own\label; one label for the block points at the last numbered line. - "Label multiply defined" means two
\labelcommands use the same key, often after copying a figure. - "Labels may have changed. Rerun to get cross-references right" only asks for one more compile.
In journal templates
- Journal classes set the name in captions: IEEEtran prints "Fig. 1", so write
Fig.~\ref{…}in the text, or with cleveref add\crefname{figure}{Fig.}{Figs.}to match.
Errors you may meet
- LaTeX Warning: Reference `<label>' on page <n> undefinedA \ref, \eqref or \pageref names a label LaTeX has not seen: a typo, a missing \label, or a part excluded by \includeonly. Prints ?? in the PDF.
- LaTeX Warning: There were undefined references.The end-of-run summary: some \ref or \cite did not resolve. Find the individual "Reference ... undefined" or "Citation ... undefined" lines above it.
- LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.Some label's number or page changed during this run, so references printed from the old .aux may be wrong. Compile again; if it never settles, look for a loop.
- LaTeX Warning: Label `<label>' multiply defined.Two \label commands use the same name, so every \ref to it shows whichever came last. Rename one, and update the \ref commands that meant it.
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
- Aligning equations in LaTeX: align, aligned and splitAlign multi-line equations at = with amsmath: align for numbered lines, aligned and split for one number, \notag to number only some lines.
- Adding appendices in LaTeXStart appendices with \appendix so sections become A, B, C, or use the appendix package for "Appendix A" headings and contents entries.
- Writing an algorithm in pseudocode in LaTeXTypeset pseudocode with the algorithm float and algpseudocode: \State, \If, \For, \While, \Function, line numbers, captions and references.
The examples on this page were compiled with pdfLaTeX to check that they work as shown. LaTeX quick reference · LaTeX errors explained