Written and maintained by CASRAI Editorial Board
Last updated
In LaTeX, the ref{key} command provides automated internal cross-referencing by inserting the generated number of a numbered structural element—such as a section, figure, table, or equation—marked by a corresponding label{key} command. In scientific writing, automated cross-referencing eliminates manual renumbering errors when floating environments, diagrams, or sections are reordered during manuscript revisions.
What it is used for in research
Academic manuscripts, technical dissertations, and peer-reviewed journal papers routinely contain dozens of floating figures, numerical tables, and mathematical derivations. Manually tracking static numbers across iterative review rounds introduces severe numbering discrepancies. The LaTeX cross-referencing engine decouples textual mentions from static numbering by tracking counter increments dynamically.
Researchers rely on ref to:
- Maintain relational consistency: When an earlier figure or section is inserted or removed, every downstream reference updates automatically without manual edits.
- Coordinate multi-file manuscripts: Large research projects divided across chapters using
includeorinputpreserve continuous numbering across the entire document tree. - Separate structural cross-references from bibliographic citations: Internal document elements rely on
ref, whereas external literature citations rely oncitecommands backed by tools like BibTeX or Zotero and JabRef. - Enable interactive PDF navigation: When paired with the
hyperrefpackage, raw textual numbers transform into direct, clickable hyperlinks within digital PDF publications (see details in our LaTeX hyperlink guide).
Install or access (official source only)
The core cross-referencing system—including label, ref, and pageref—is built directly into the standard LaTeX2e kernel. Authors do not need to install additional packages for baseline cross-referencing functionality. The command is accessible through all standard, official TeX distributions and platforms:
- TeX Live: The cross-platform TeX system maintained by the TeX Users Group (TUG) available at
tug.org/texlivefor Linux, Windows, and Unix environments. - MacTeX: The macOS distribution of TeX Live, distributed through the TeX Users Group at
tug.org/mactex. - MiKTeX: An open-source Windows, macOS, and Linux TeX implementation with on-the-fly package management, available at
miktex.org. - Overleaf: The official cloud-based collaborative LaTeX authoring platform at
overleaf.com, which runs a standard TeX Live backend.
To extend cross-referencing capabilities with specialized equation brackets or automatic noun generation, researchers load standard macro packages available on the Comprehensive TeX Archive Network (CTAN):
amsmath: Maintained by the American Mathematical Society (AMS) on CTAN (ctan.org/pkg/amsmath) to provideeqref.hyperref: Maintained on CTAN (ctan.org/pkg/hyperref) to generate interactive PDF hyperlinks.cleveref: Maintained on CTAN (ctan.org/pkg/cleveref) to provide context-aware references viacref.
Step-by-step task (numbered, with menu names or commands exactly as in the current docs)
Implementing cross-referencing in LaTeX follows a strict five-step lifecycle: defining an element, attaching a label, positioning the label correctly, invoking the reference in prose, and compiling the auxiliary file.
-
Establish a standardized semantic key: Assign a unique identifier inside
label{key}. To avoid key collisions across large research projects, follow standard prefix conventions:sec:for sections, subsections, and appendicesfig:for figures and diagramstab:for data tableseq:for numbered equations
-
Attach the label to the target structural element: Place the label inside or immediately adjacent to the numbered environment. For sections, place the label directly beneath the heading declaration:
section{Empirical Methodology} label{sec:methodology}For mathematical equations, place the label inside the display math environment (such as those configured in aligned amsmath environments):
begin{equation} f(x) = int_{-infty}^{infty} hat{f}(xi) e^{2pi i xi x} dxi label{eq:fourier-inversion} end{equation} -
Ref figure latex: syntax and caption sequencing
A frequent error when referencing figures in LaTeX (searched as ref figure latex or latex ref figure) is placing the
labelin the wrong position relative to the caption. In LaTeX, floating environments do not increment the counter on their own; the counter is advanced by thecaptioncommand viarefstepcounter{figure}.Always place
labelinside the caption or immediately after the caption command:begin{figure}[htbp] centering includegraphics[width=0.85linewidth]{figures/roc_curve.pdf} caption{Receiver operating characteristic curve comparing classifier accuracy.} label{fig:roc-comparison} end{figure}If
labelis placed beforecaption, the label binds to the enclosing section counter rather than the figure counter, causingrefto print the wrong number. -
Call ref in running prose with a non-breaking tie: When referencing an object in text, precede
refwith the type noun and a non-breaking space (the tilde symbol~):As demonstrated in Figure~ref{fig:roc-comparison}, the model achieves superior convergence. The mathematical formulation is defined in Equation~ref{eq:fourier-inversion} of Section~ref{sec:methodology}.The tilde prevents typographic line breaks between the noun and the numeric value, ensuring clean manuscript formatting consistent with standard LaTeX spacing rules.
-
Execute a two-pass compilation: LaTeX writes cross-reference coordinates to an auxiliary file (
jobname.aux) during the first pass and resolves them on the second pass. Compile the document at least twice using your standard engine (pdflatex,lualatex, orxelatex):pdflatex manuscript.tex pdflatex manuscript.tex
Output and how to read it
Understanding the exact output generated by ref ensures accurate formatting in camera-ready manuscripts:
- Bare numeric values: Standard
ref{key}produces only the raw counter string (for example,3,2.1, orIV). It never prefixes nouns like “Figure” or “Table”. Authors must write the preceding noun explicitly or utilize helper packages likecleveref. - Page number retrieval: To cite the physical page location of an element rather than its numerical sequence, use
pageref{key}:See Table~ref{tab:demographics} on page~pageref{tab:demographics}. - Hyperlinked references in PDF output: When the document preamble includes
usepackage{hyperref}, everyref{key}becomes an active, clickable cross-reference in the generated PDF. To print the reference number without generating an active hyperlink, use the starred form:ref*{key}. - Upright parentheses for equations: When using the
amsmathpackage,eqref{key}outputs the equation number wrapped in upright Roman parentheses:(1). Even if called inside an italicized theorem or lemma environment,eqrefpreserves standard mathematical typography.
Troubleshooting
Cross-referencing issues in LaTeX typically stem from the multi-pass compilation lifecycle or label positioning. Use the following diagnostic solutions:
| Symptom or Warning | Root Cause | Corrective Action |
|---|---|---|
| Output shows bold question marks (??) | Auxiliary file missing cross-reference data or unmatched label key. | Verify spelling in ref{key} matches label{key} exactly (case-sensitive). Recompile the document a second time to update jobname.aux. |
| Figure reference displays the section number (e.g. “Figure 2” instead of “Figure 1”) | label was positioned before caption inside the float environment. |
Move label{fig:...} immediately after caption{...} or nest it directly inside the caption argument. |
LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right. |
Content repositioning shifted page numbers or section counters during typesetting. | Run the LaTeX compiler once more until the warning disappears from the console log. |
LaTeX Warning: There were multiply-defined labels. |
The identical label key was declared more than once in the document. | Search the source files for duplicate label{...} declarations and assign distinct semantic names. |
| Corrupted references after build crash | Partial write left malformed records inside the auxiliary cache. | Delete the temporary jobname.aux file from the build folder and execute two fresh compilation passes. |
Free alternatives (table)
The TeX ecosystem includes several free, open-source packages that extend or automate baseline cross-referencing for research manuscripts:
| Command / Package | Package Prerequisite | Primary Use Case | Output Format | Automatic Noun | Hyperlink Support |
|---|---|---|---|---|---|
ref{key} |
LaTeX Kernel (Built-in) | General-purpose numbering across sections, figures, and tables | Bare counter (e.g., 2, 1.3) |
No (author writes noun) | Yes (via hyperref) |
pageref{key} |
LaTeX Kernel (Built-in) | Physical page lookups across print and digital documents | Bare page number (e.g., 14) |
No (author writes “page”) | Yes (via hyperref) |
eqref{key} |
amsmath (CTAN) |
Mathematical derivations and numbered equation references | Upright parentheses (e.g., (3.1)) |
No (author writes “Eq.”) | Yes (via hyperref) |
autoref{key} |
hyperref (CTAN) |
Automatic macro-based noun insertion for active PDF links | Prefixed noun + number (e.g., Figure 1) |
Yes (macro dictionary) | Yes (native) |
cref{key} / Cref{key} |
cleveref (CTAN) |
Context-aware multi-element referencing and range compression | Smart noun + number (e.g., fig. 1, eqs. (1) to (3)) |
Yes (sentence-case sensitive) | Yes (load after hyperref) |
FAQ
Why does LaTeX ref display question marks ?? instead of numbers?
LaTeX displays bold double question marks (??) when a reference key cannot be resolved from the auxiliary (.aux) file. This occurs on the first compilation pass of a new label or when the key in ref{key} contains a typographical error. Running the compiler a second time allows LaTeX to read the generated auxiliary file and populate the resolved reference.
Where should label be placed in a LaTeX figure environment?
In a LaTeX figure environment, label must always be placed either immediately after the caption command or directly inside the caption braces. Placing label before caption attaches the label to the preceding section counter rather than the figure counter, resulting in incorrect reference numbers in your manuscript.
What is the difference between ref and eqref in LaTeX?
The standard ref command prints only the unformatted counter number. The eqref command, provided by the AMS-LaTeX amsmath package, specifically formats mathematical equation citations by enclosing the number in upright Roman parentheses (such as (1)), ensuring that equation references remain upright even within italicized theorem environments.
How do I make LaTeX cross-references clickable in a PDF?
Include usepackage{hyperref} in the document preamble. The hyperref package automatically converts standard ref, pageref, and eqref commands into clickable internal hyperlinks that navigate directly to the target figure, section, or equation in the compiled PDF.
Can I reference multiple figures or equations at once in LaTeX?
Standard ref requires separate calls for each element (for example, Figures~ref{fig:a} and~ref{fig:b}). The cleveref package automates multiple references by accepting comma-separated keys inside cref{fig:a,fig:b,fig:c}, which automatically sorts and formats them into compressed ranges such as “figures 1 to 3”.
Sources (official docs)
- LaTeX Project / Berry, K., and Belaïche, V. (2026). LaTeX2e: An unofficial reference manual. Cross references: ref, pageref, and label. https://latexref.xyz/_005cref.html. Checked 2026-11-07.
- Overleaf Documentation Team. (2026). Cross referencing sections, equations and floats. Overleaf Guides. https://www.overleaf.com/learn/latex/Cross_referencing_sections,_equations_and_floats. Checked 2026-11-07.
- American Mathematical Society. (2026). User’s Guide for the amsmath Package (Version 2.0). Comprehensive TeX Archive Network (CTAN). https://ctan.org/pkg/amsmath. Checked 2026-11-07.
- Cubitt, T. (2026). The cleveref package: Intelligent cross-referencing. Comprehensive TeX Archive Network (CTAN). https://ctan.org/pkg/cleveref. Checked 2026-11-07.
- TeX Users Group. (2026). TeX Live: Comprehensive TeX system. https://tug.org/texlive/. Checked 2026-11-07.







