Cross-references in LaTeX
Updated:
Mark any numbered element with a \label and refer to it later with \ref. LaTeX fills in the right number even after you reorder figures. The prefixes fig:, tab: and eq: help keep labels apart.
Labelling a section
\section{Data analysis}\label{sec:analysis}\label records the number of the last numbered element before it. After \section that is the section number, after \caption the figure or table number, and inside equation the equation number.
Putting a lab report together? In CamteX you draw formulas and tables, then send the finished document to Overleaf or download it as a PDF. Try it free
References in the text
The results are listed in Table~\ref{tab:pendulum}, and Figure~\ref{fig:chart} on page~\pageref{fig:chart} shows the trend.The tilde ~ is a non-breaking space, so "Table 2" is never split across two lines. \pageref gives the page where LaTeX finally placed the figure, even if it floated further on.
Referring to an equation
Substituting \eqref{eq:period} into \eqref{eq:g} gives the result.\eqref from amsmath adds the same parentheses that appear next to the equation number. Figures and tables are referenced without parentheses, so they keep the plain \ref.
Automatic names with cleveref
\usepackage{hyperref}
\usepackage{cleveref}
% ...
As \cref{fig:chart} shows, the period grows with length.cleveref adds the word for you: "fig. 2", "table 1", "eq. (3)", and \Cref gives the capitalised form for the start of a sentence. Load it last, after hyperref, or the links break.
Clickable links in the PDF
\usepackage[hidelinks]{hyperref}With hyperref every reference number, citation and table of contents entry becomes a link in the PDF. The hidelinks option removes the coloured boxes around them. Load hyperref after most other packages.
Common mistakes
- "??" in the PDF: references are only filled in on the second compilation.
- A normal space before \ref: the number can wrap to the next line. A tilde ~ keeps it attached.
- The same label used twice: LaTeX warns about multiply defined labels, and the reference points to the last occurrence.
- A typo in a label name: the PDF shows "??" and the compilation log has a Reference undefined warning with the name it could not find.
See also
Don't want to memorize commands?
Draw a formula or paste a screenshot, and CamteX turns it into ready LaTeX.