Site icon texblog

Cleveref, a clever way to reference in LaTeX

Using standard cross-referencing in LaTeX only produces the label number, a name describing the label such as figure, chapter or equation has to be added manually. The cleveref package overcomes this limitation by automatically producing the label name and number. It further allows cross-referencing ranges of labels and multiple labels of the same or different kinds, including auto-sorting and compression of labels.

 

Basic cross-reference command

Cleveref implements \cref for basic cross-referencing. The command is used in the same way as the standard \ref command. Besides the label number it also produces the reference kind.

Warning: <strong>cleverref</strong> has to be loaded after <strong>hyperref</strong>!

Here is an example:

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{cleveref}
\begin{document}

\begin{align}
y&=a_1x+b_1\label{eqn:1}
\end{align}

\noindent
Standard equation reference (\textbackslash ref): \ref{eqn:1}\\
Cleveref equation reference (\textbackslash cref): \cref{eqn:1}

\begin{figure}[ht]\centering\rule{0.5\linewidth}{0.1\linewidth}\caption{First figure}\label{fig:1}\end{figure}

\noindent
Standard figure reference (\textbackslash ref): \ref{fig:1}\\
Cleveref figure reference (\textbackslash cref): \cref{fig:1}

\end{document}

 

Multiple labels (sort and compress)

To cross-reference multiple labels of the same or different kinds, the \cref command is used. Labels are separated by commas without white-space (\cref{ref1,ref2,etc.}). For a range of the same label kind, the command \crefrange{first}{last} is available.

\documentclass[11pt]{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{cleveref}

\begin{document}

\begin{align}
y&=a_1x+b_1\label{eqn:1}\\
y&=a_2x+b_2\label{eqn:2}\\
y&=a_3x+b_3\label{eqn:3}\\
y&=a_4x+b_4\label{eqn:4}
\end{align}

\noindent
Range example: \crefrange{eqn:1}{eqn:4}

\begin{figure}[ht]\centering\rule{0.5\linewidth}{0.1\linewidth}\caption{First figure}\label{fig:1}\end{figure}

\noindent
Mixed references example: \cref{eqn:1,eqn:3,eqn:4,fig:1}

\end{document}

 

Capitalize at beginning of sentence

By default, label names are produced with a small initial letter. To capitalize the first letter at the beginning of a sentence, use \Cref and \Crefrange instead.

For capitalization of all label names throughout the document, load the package with the capitalise option.

\usepackage[capitalise]{cleveref}

 

Full label names

If you prefer to use the full label name rather than abbreviations such as fig. or eq., load the package with the noabbrev option:

\usepackage[noabbrev]{cleveref}

 

Clickable references with hyperref

The cleveref package is fully compatible with hyperref (package manual). It is important to load the cleveref package last, after hyperref.

% Preamble
\usepackage{...} % other packages
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
...

 

Other features of the package

The cleveref package offers a lot more, including:

See the package documentation for more details.

 

Alternative packages

There are a number of packages implementing similar features. According to the documentation, cleveref is among the most extensive in terms of functionality and flexibility. However, through a recently published post on LaTeX Alive I learnt that the refstyle package is a valid alternative to cleveref and I’ll definitely give it a try.

Exit mobile version