The cleveref package
Writes the kind of object in front of a reference: \cref{fig:setup} prints "fig. 1" and handles several labels at once.
Loading the package
\usepackage{cleveref}\ref prints only the number, so the word "Figure", "Table" or "Section" has to be typed in front of every reference. \cref knows which counter a label belongs to and adds the word itself. When a figure becomes a table, the text follows.
Several labels can go into one command. \cref{eq:a,eq:b,eq:c} sorts them and prints "eqs. (1) to (3)".
The names come in the language given as package option or as class option, for example ngerman. With \crefname you set your own names.
Main commands
\cref{fig:setup} | Reference with name, for use inside a sentence. |
\Cref{fig:setup} | The same with a capital letter and without abbreviation, for the start of a sentence. |
\cref{eq:a,eq:b,tab:data} | Several labels, also of different kinds; no spaces after the commas. |
\crefrange{eq:first}{eq:last} | A range, such as "eqs. (1) to (5)". |
\cpageref{fig:setup} | Page reference with the word "page". |
\crefname{equation}{Eq.}{Eqs.} | Sets singular and plural name for one kind of object. |
\usepackage[capitalise,noabbrev]{cleveref} | capitalise writes all names with a capital letter, noabbrev writes them out in full. |
\usepackage[nameinlink]{cleveref} | With hyperref: the name is part of the link, not only the number. |
Complete example
\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\section{Model}\label{sec:model}
\begin{equation}\label{eq:force}
F = m a
\end{equation}
\begin{equation}\label{eq:energy}
E = \tfrac{1}{2} m v^2
\end{equation}
\Cref{eq:force,eq:energy} belong to \cref{sec:model}.
See also \cpageref{eq:energy}.
\end{document}Compiled with pdfLaTeX from TeX Live 2023. Tools run in this order: pdflatex → pdflatex.
Pitfalls
- Load
clevereflast. Beforehyperrefit stops withcleveref must be loaded after hyperref, and beforeamsmathwithcleveref must be loaded after amsmath. - A language that is given only to
babel, as in\usepackage[ngerman]{babel}, does not reachcleveref, and the names stay English. Pass the language as class option or directly tocleveref. - The reference shows two question marks until the label is known, that is after the second run; see Reference `sec:methods' on page 1 undefined.
- In a float,
\labelhas to come after\caption. Otherwise it belongs to the surrounding section and\crefprints "section" with the wrong number.