References with automatic names in LaTeX (cleveref, cref)
With the cleveref package, \cref{key} prints the name and the number: "fig. 1", "table 2" or "eq. (3)", depending on what the label belongs to. \Cref is the form for the start of a sentence.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{cleveref}Name and number in one command
\cref prints the abbreviated lower-case name, here "fig. 1". \Cref prints the full name with a capital letter, "Figure 1".
\begin{figure}[h]
\centering
\includegraphics[width=4cm]{image.png}
\caption{Test setup}
\label{fig:setup}
\end{figure}
The sensors are shown in \cref{fig:setup}.
\Cref{fig:setup} also shows the wind tunnel.Several labels at once
Separate the keys with commas and no spaces. cleveref sorts them, joins neighbours into a range and uses the plural: the example prints "eqs. (1) to (3)". \crefrange{eq:a}{eq:c} gives the same range from its two ends.
\begin{align}
a &= b + c \label{eq:a} \\
d &= e + f \label{eq:b} \\
g &= h + i \label{eq:c}
\end{align}
The sums are defined in \cref{eq:a,eq:b,eq:c}.Full names and capitals everywhere
The option noabbrev writes "figure" and "equation" in full, capitalise starts every name with a capital letter, and nameinlink makes the name a part of the link.
Complete document:
\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage[capitalise, noabbrev, nameinlink]{cleveref}
\begin{document}
\begin{equation}
E = mc^2 \label{eq:energy}
\end{equation}
The relation is given in \cref{eq:energy}.
\end{document}German names
cleveref takes the language from the class options. With ngerman there, \cref prints "Abb. 1" and \Cref prints "Abbildung 1".
Complete document:
\documentclass[ngerman]{article}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\begin{figure}[h]
\centering
\includegraphics[width=4cm]{image.png}
\caption{Versuchsaufbau}
\label{fig:aufbau}
\end{figure}
Die Sensoren sind in \cref{fig:aufbau} zu sehen.
\end{document}Set your own names
\crefname takes the type of the label, the singular and the plural for \cref. \Crefname does the same for \Cref.
This goes into the preamble.
\crefname{section}{sec.}{secs.}
\Crefname{section}{Section}{Sections}Pitfalls
- Load
cleverefas the last of these packages: afteramsmath, aftervariorefand afterhyperref. - A space after a comma in the key list becomes part of the next key.
\cref{eq:a, eq:b}prints question marks for the second reference. \autoref{key}fromhyperrefalso prints a name, such as "Figure 1", without another package. It takes one key at a time.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
cleveref - Package
hyperref - How-to Cross-references in LaTeX
- How-to Refer to a page number in LaTeX
- Error Reference `sec:methods' on page 1 undefined
- Package
amsmath - Package
graphicx - Package
babel