Let
X
Journal

Cross-Referencing in LaTeX with \label and \ref

SPECIMEN IDLETX-SPEC-LATE
DATE RECORDEDJun 3, 2026
READING COMPLEXITY2 min read
TAG INDEX
latexcross-referencestructuretutorial
Document Abstract

To cross-reference in LaTeX, mark targets with \label{key} and cite with \ref{key} for the number or \pageref{key} for the page. cleveref adds the type word.

To cross-reference in LaTeX, mark the target with \label{key} and refer to it with \ref{key} for the number or \pageref{key} for the page. LaTeX resolves the actual numbers automatically, so reordering sections or figures never breaks a reference. The cleveref package goes further and prints the type word ("Figure 3") for you. Here's the system.

1. Label a target, reference it anywhere

\section{Method}\label{sec:method}
...
\begin{figure}[t]
  \centering
  \includegraphics[width=0.7\linewidth]{f.png}
  \caption{The pipeline.}
  \label{fig:pipeline}      % AFTER the caption
\end{figure}
...
As shown in `Section~\ref{sec:method}` and `Figure~\ref{fig:pipeline}` (page `\pageref{fig:pipeline}`).

Use a non-breaking tilde ~ before \ref so the number never wraps to a new line away from its word.

2. The golden rule: label after caption

Inside a float, \label must come immediately after \caption. The caption sets the figure/table counter; a label placed before it captures the section number instead and your reference points to the wrong thing. This single mistake causes most "my figure reference is wrong" bugs.

3. Naming convention

Prefix labels by type so keys stay readable:

| Prefix | For | |---|---| | sec: | Sections | | fig: | Figures | | tab: | Tables | | eq: | Equations | | alg: | Algorithms | | app: | Appendices |

4. cleveref: never type "Figure" again

\usepackage{hyperref}   % load first
\usepackage{cleveref}   % then this
...
\cref{fig:pipeline}     % prints "Figure 3"
\cref{fig:a,fig:b}      % prints "Figures 1 and 2"

\cref adds the type word and handles ranges/lists, staying correct if you change the target. Load it after hyperref.

5. Why references show ??

They resolve through the .aux file across two compiles, so a new reference reads ?? until the second pass. LetX runs the extra pass automatically, so you see resolved numbers immediately — the same reason it handles citations without a [?].

→ Get always-resolved cross-references with instant compile in LetX.


Written by Shihab Shahriar Antor — AI Engineer & Founder of Shahriar Labs, maker of LetX.

Frequently Asked Questions

Why do my \ref numbers show as ?? or 0?

Cross-references resolve via the .aux file, which LaTeX writes on one compile and reads on the next, so a fresh reference shows ?? until you compile a second time. If it persists, check that the \label exactly matches the \ref key (they're case-sensitive) and that the \label comes immediately after the \caption inside a float — a label placed before the caption picks up the wrong number. Online editors run the extra pass automatically.

Where exactly should \label go in a figure or table?

Place \label immediately after \caption, inside the float environment. The caption sets the float's counter, and \label captures whatever counter was most recently incremented; putting the label before the caption captures the current section number instead, producing a wrong reference. So the order is always \caption{...} then \label{...}. For equations, put \label inside the equation or align environment.

What does cleveref add over plain \ref?

cleveref's \cref{key} prints the reference type word along with the number — 'Figure 3', 'Table 2', 'Equation (5)' — so you never type the word yourself and it stays correct if you change the target type. It also formats ranges and lists, like \cref{a,b,c} producing 'Figures 1 to 3'. Load cleveref after hyperref. It's a major quality-of-life upgrade for any document with many references.