Chapter 07 – Using Cross-References

Here are the Code examples of this chapter. You can compile them online right on this web page by pressing the Typeset / Compile button. You can also edit them for testing, and compile again.

For a better view with the online compiler, I sometimes use \documentclass[border=10pt]{standalone} instead of \documentclass{article}. Instead of having a big letter/A4 page, the standalone class crops the paper to see just the visible text without an empty rest of a page.

Any question about a code example? Post it on LaTeX.org, I will answer. As forum admin I read every single question there. (profile link).

Setting labels and references

\documentclass{book}
\begin{document}
\chapter{Statistics}
\section{Most used packages by LaTeX.org users}
\label{sec:packages}
The Top Five packages, used by LaTeX.org
members\footnote{according to the 2021 survey on
LaTeX.org\label{fn:project}}:
\begin{enumerate}
  \item graphicx\label{item:graphicx}
  \item babel
  \item amsmath\label{item:amsmath}
  \item geometry
  \item hyperref
\end{enumerate}
\chapter{Mathematics}
\emph{amsmath}, on position \ref{item:amsmath} of the top list
in section~\ref{sec:packages} on page~\pageref{sec:packages},
is indispensable to high-quality mathematical typesetting in
\LaTeX. \emph{graphicx}, on position \ref{item:graphicx},
is for including images. See also the footnote \ref{fn:project}
on page~\pageref{fn:project}.
\end{document}

figure

Figure 7.1


figure

Figure 7.2


figure

Figure 7.3


figure

Figure 7.4



Referring to a page

\documentclass{book}
\begin{document}
\chapter{Statistics}
\section{Introduction}
\newpage
\section{Most used packages by LaTeX.org users}
\label{sec:packages}
The Top Five packages, used by LaTeX.org
members\footnote{according to the 2021 survey on
LaTeX.org\label{fn:project}}:
\begin{enumerate}
  \item graphicx\label{item:graphicx}
  \item babel
  \item amsmath\label{item:amsmath}
  \item geometry
  \item hyperref
\end{enumerate}
\chapter{Mathematics}
\emph{amsmath}, on position \ref{item:amsmath} of the top list
in section~\ref{sec:packages} on page~\pageref{sec:packages},
is indispensable to high-quality mathematical typesetting in
\LaTeX. \emph{graphicx}, on position \ref{item:graphicx},
is for including images. See also the footnote \ref{fn:project}
on page~\pageref{fn:project}.
\end{document}

figure

Figure 7.5



Producing intelligent page references

\documentclass{book}
\usepackage[nospace]{varioref}
\begin{document}
\chapter{Statistics}
\section{Introduction}
\newpage
\section{Most used packages by LaTeX.org users}
\label{sec:packages}
The Top Five packages, used by LaTeX.org
members\footnote{according to the 2021 survey on
LaTeX.org\label{fn:project}}:
\begin{enumerate}
  \item graphicx\label{item:graphicx}
  \item babel
  \item amsmath\label{item:amsmath}
  \item geometry
  \item hyperref
\end{enumerate}
\chapter{Mathematics}
\emph{amsmath}, on position \vref{item:amsmath}
of the top list in section~\vref{sec:packages},
is indispensable to high-quality mathematical
typesetting in \LaTeX. \emph{graphicx}, on position
\vref{item:graphicx}, is for including images.
See also the footnote \vref{fn:project}, that is,
\vpageref{fn:project}.
\end{document}

figure

Figure 7.6



varioref with figures

\documentclass{article}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage{varioref}
\usepackage{blindtext}
\begin{document}
\section{Figures}
\begin{figure}
\includegraphics{example-image}
\caption{An example figure}
\label{fig:example}
\end{figure}
\blindtext
\begin{figure}
\includegraphics{example-image}
\caption{Another example figure}
\label{fig:other}
\end{figure}
\newpage
Same reference code, different output:
\begin{verbatim}
See the \vpageref[above figure][figure]{fig:other} and
the \vpageref[above figure][figure]{fig:example}.
\end{verbatim}
See the \vpageref[above figure][figure]{fig:other} and
the \vpageref[above figure][figure]{fig:example}.
\end{document}

figure

Figure showing referencing figures



varioref with figures and ranges

\documentclass{book}
\usepackage{parskip}
\usepackage{graphicx}
\usepackage[nospace]{varioref}
\usepackage{xcolor}
\usepackage{blindtext}
\begin{document}
\chapter{Figures}
\begin{figure}
\includegraphics{example-image}
\caption{An example figure}
\label{fig:example}
\end{figure}
\clearpage
\begin{figure}
\includegraphics{example-image}
\caption{Another example figure}
\label{fig:other}
\end{figure}
\clearpage
\begin{figure}
\includegraphics{example-image}
\caption{And another example figure}
\label{fig:last}
\end{figure}
\clearpage
See the figures \textcolor{red}{\vpagerefrange{fig:example}{fig:last}} and
figures \textcolor{blue}{\vrefrange{fig:example}{fig:last}}.
\end{document}

figure

Figure showing referencing figures with ranges



Using automatic reference names

\documentclass{book}
\usepackage{cleveref}
\crefname{enumi}{position}{positions}
\begin{document}
\chapter{Statistics}
\label{stats}
\section{Most used packages by LaTeX.org users}
\label{packages}
The Top Five packages, used by LaTeX.org
members\footnote{according to the 2021 survey on
LaTeX.org\label{project}}:
\begin{enumerate}
  \item graphicx\label{graphicx}
  \item babel
  \item amsmath\label{amsmath}
  \item geometry
  \item hyperref
\end{enumerate}
\chapter{Mathematics}
\label{maths}
\emph{amsmath}, on \cref{amsmath} of the top list in 
\cref{packages} of \cref{stats}, is indispensable to
high-quality mathematical typesetting in \LaTeX.
\emph{graphicx}, on \cref{graphicx}, is for including images.
See also the \cref{project} on \cpageref{project}.
\end{document}

figure

Figure 7.7



cleveref and ranges

\documentclass{book}
\usepackage{cleveref}
\crefname{enumi}{position}{positions}
\begin{document}
\chapter{Statistics}
\label{stats}
\section{Most used packages by LaTeX.org users}
\label{packages}
The Top Five packages, used by LaTeX.org
members\footnote{according to the 2021 survey on
LaTeX.org\label{project}}:
\begin{enumerate}
  \item graphicx\label{graphicx}
  \item babel
  \item amsmath\label{amsmath}
  \item geometry
  \item hyperref
\end{enumerate}
\chapter{Mathematics}
\label{maths}
See \crefrange{graphicx}{amsmath} and \cpagerefrange{stats}{maths}.
\end{document}

figure

Figure showing automated referencing with ranges



This code is available on Github. It is licensed under the MIT License, a short and simple permissive license with conditions only requiring preservation of copyright and license notices.

Go to next chapter.