Cross-references

“See Figure 3,” “by equation (2),” “refer to page 2” — write those numbers and pages by hand and a single moved figure or section throws them all off. LaTeX instead lets you attach a name and let the machine count. Starting from the core \label and \ref/\pageref, through amsmath’s \eqref, hyperref’s \autoref and \nameref, and on to varioref and cleveref, this page walks through the tools that keep every reference correct on their own.

The core — \label and \ref

The whole system rests on just two commands. **\label{key}** ties the name key to the value of the counter that was most recently incremented at that point. **\ref{key} then drops that number** into your text. LaTeX does the counting, so you never type a number yourself. The key is an arbitrary string you choose — the nickname by which your source refers to a figure, equation, or section.

The phrase “most recently incremented” is the crux: what \label captures depends on where you put it. For a figure or table, place it **right after \caption** (it is \caption that steps the counter — put it just after \begin{figure} and you may capture the chapter number instead); for an equation, **inside the equation environment; for a heading, right after \section** (or the like). The rule of thumb: put \label immediately after the command that produces the number.

latex
\section{はじめに}
\label{sec:intro}

\begin{equation}
  \label{eq:euler}
  e^{i\pi} + 1 = 0
\end{equation}

\begin{figure}
  \centering
  \includegraphics{leaf}
  \caption{葉脈の構造}
  \label{fig:leaf}
\end{figure}\ref{sec:intro}節で述べたように、式~\ref{eq:euler}と図~\ref{fig:leaf}を参照する。

\ref returns only the number, so you write the word yourself — “Figure,” “equation,” “Section.” The convention is to join the word and number with a **non-breaking space ~** (a tie) rather than an ordinary space, so a line break never strands the word from its number (e.g. Figure~\ref{fig:leaf}). It is exactly this manual wording — and the risk of mislabelling it — that \autoref and \cref below remove.

Why you compile twice

Cross-references do not settle in a single compilation. Each time LaTeX meets a \label, it writes that number and page into the **.aux file** (an auxiliary file with the same base name as your document). What \ref can use is the information written to .aux on the *previous* run. So on the first pass the target numbers are not yet known: the output shows a **bold ??**, and the log carries this warning.

latex
LaTeX Warning: Label(s) may have changed. Rerun to get cross references right.

Compile once more and the values from .aux are read back in, turning ?? into the right numbers. If numbering shifts so that a referenced value changes, the warning returns — so you repeat until it disappears (two passes suffice in practice). Build tools like latexmk run this loop for you, and Overleaf compiles several times behind the scenes, so you rarely see the ?? there.

Page references and naming

When you want the page rather than the number, use **\pageref{key}. It takes the same name and returns the number of the page where that \label sits**. This lets you cite both number and page together — “see Figure 3 on page 12” (e.g. Figure~\ref{fig:leaf} on page~\pageref{fig:leaf}).

The key can be anything, but a widely used convention is to give it a prefix for its kind: sec: (section), fig: (figure), tab: (table), eq: (equation), ch: (chapter), lst: (listing). Two benefits follow. Naming by meaning like fig:leaf means reordering figures never forces you to rename labels. And a glance at \ref{fig:...} tells you the kind of target, while keeping names from a caption and a listing from colliding.

PrefixUsed for
ch:Chapters
sec:Sections and subsections
fig:Figures
tab:Tables
eq:Equations
lst:Code listings

Equation references — \eqref (amsmath)

Equation numbers are conventionally written in parentheses (“by equation (3)”). Plain \ref returns only 3, leaving you to add the parentheses by hand, but **\eqref{key}** — available once you load amsmathadds them for you, returning (3). Better still, the parentheses are always set upright regardless of the surrounding font, so they never lean even inside italic text. For equations, \eqref is the convenient choice over \ref.

latex
\usepackage{amsmath}
% ...
式~\eqref{eq:euler}はオイラーの等式である。
% 出力: 式 (1) はオイラーの等式である。

hyperref extras — \autoref and \nameref

Loading **hyperref turns every reference into a clickable link in the PDF and adds two convenient referencing commands. \autoref{key} automatically prepends the word for the target’s kind** — “Figure,” “section,” and so on (a section yields “section 3.4,” a figure “Figure 3”) — and makes the whole thing a hyperlink. That removes the manual “Figure” in Figure~\ref{...}, and the risk of getting it wrong. You can change the prepended word by redefining \figureautorefname, \sectionautorefname, and the like (this is also how you localize it).

The other, **\nameref{key}, inserts not a number but the title text** of the target. Reference the label on \section{Introduction} with \nameref and you get the words “Introduction.” Use it when you want to refer by title rather than number — “as discussed in ‘Introduction.’”

latex
\usepackage{hyperref}
% ...
\autoref{fig:leaf}を参照。   % 出力: Figure 3 を参照。(リンク付き)
\nameref{sec:intro}で述べた。 % 出力: はじめに で述べた。

Page-aware references — varioref

In a bound document it reads better to say “the figure on the next page” when it is close, or “the figure on the facing page” when it is on the same spread, rather than mechanically printing “the figure on page N.” **varioref’s \vref{key}** prints the number like \ref but also checks whether the target sits on a different page and automatically adds page-aware wording — “on the next page,” “on the facing page,” “on page 5.” If it is on the same page, it adds nothing.

When you want only the page guidance, use **\vpageref{key}** (no number, just “on the next page,” etc.). \vpageref takes two optional arguments: the first sets the wording used when it lands on the same page, the second the lead-in wording when it does not. This helps when the reference and its target are close and may or may not be split by a page break. Because its output varies dynamically with page position, varioref may need an extra compilation to settle.

latex
\usepackage{varioref}
% ...
See~\vref{fig:leaf}.
% 同ページ: See figure 3.
% 別ページ: See figure 3 on the next page.  /  ... on page 12.

Clever references — cleveref (\cref / \Cref)

cleveref raises referencing a level. \cref{key}** prepends the kind’s word like \autoref (by default “fig. 1,” or “figure 1” with the noabbrev option), but its real strength is multiple and range references. Pass labels comma-separated without spaces — \cref{a,b,c} — and it sorts and compresses the numbers automatically, emitting “figures 1, 2 and 3” or “equations~(2) and~(4),” even getting the singular/plural of the word right. A consecutive span contracts via **\crefrange{first}{last}** into “figures 1 to 3.”

At the start of a sentence you want the word capitalized, so use **\Cref{key}** (capital initial) — for openings like “Figure 3 shows …” (use lowercase \cref mid-sentence). To capitalize everywhere, load it with the capitalise option. The prepended word is defined and changed with **\crefname{type}{singular}{plural}** (capitalized variant \Crefname), which is also how you localize it to Japanese and other languages (e.g. \crefname{figure}{図}{図}). When you want only the number, like plain \ref, use **\labelcref{key}**.

The load order is decisive: cleveref must come **last, after hyperref**. If you also use varioref, order them variorefhyperrefcleveref. Get it wrong and references can point at entirely the wrong target with no warning in the log.

latex
\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}   % 必ず最後に / always last
% ...
\cref{fig:a,fig:b,fig:c}   % figures 1 to 3
\cref{eq:euler,eq:max}     % equations (1) and (4)
\Cref{sec:intro} discusses % Section 1 discusses ...

If unsure which to use, **standardize on cleveref throughout the document.** It handles the kind word, singular/plural, multiple references, and ranges all automatically — so the slip you can make with Figure~\ref{...} (writing “Table” where you meant “Figure”) simply cannot happen.

CommandWhat it prints
\label{key}Tags the most recent counter (no output)
\ref{key}The number only (e.g. 3)
\pageref{key}The page number of that label
\eqref{key}Equation number in parentheses (e.g. (3)) / amsmath
\autoref{key}Kind word + number + link (e.g. Figure 3) / hyperref
\nameref{key}The target heading’s title text / hyperref
\vref{key}Number + page hint (on the next page, …) / varioref
\cref{key}Kind word + number; handles lists/ranges / cleveref
\Cref{key}Capitalized \cref for sentence start / cleveref
\crefrange{a}{b}A reference range (e.g. figures 1 to 3) / cleveref