Refer to a page number in LaTeX (pageref, varioref)
\pageref{key} prints the number of the page on which \label{key} stands. \vref from the varioref package prints the number of the object and adds the page only when the target is on another page.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{graphicx}
\usepackage{varioref}Print the page of a label
\pageref uses the same keys as \ref. It prints the page number in the style of that page, so a roman number in the front matter.
\section{Method}\label{sec:method}
Text of the section.
\newpage
The method is described in section~\ref{sec:method}
on page~\pageref{sec:method}.Add the page only when it is needed
\vref prints only the number when the target is on the same page. One page away it adds words such as "on the previous page" or "on the next page", and further away "on page 5". \vpageref prints the page part alone.
\begin{figure}[h]
\centering
\includegraphics[width=4cm]{image.png}
\caption{Test setup}
\label{fig:setup}
\end{figure}
Same page: figure~\vref{fig:setup}.
\newpage
Next page: figure~\vref{fig:setup}.German wording
varioref takes the language from the class options. With ngerman it prints "auf der vorherigen Seite" and "auf Seite 5".
Complete document:
\documentclass[ngerman]{article}
\usepackage{babel}
\usepackage{varioref}
\begin{document}
\section{Methode}\label{sec:methode}
Text des Abschnitts.
\newpage
Siehe Abschnitt~\vref{sec:methode}.
\end{document}Page references with a name
With hyperref, \autopageref{key} prints "page 3" as one link, and \pageref*{key} prints the number without a link. With cleveref, \cpageref{key} prints "page 3" and \cpagerefrange{a}{b} prints "pages 3 to 5".
Complete document:
\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\begin{document}
\section{Method}\label{sec:method}
Text of the section.
\newpage
\section{Results}\label{sec:results}
The method is on \autopageref{sec:method}.
Method and results fill \cpagerefrange{sec:method}{sec:results}.
\end{document}Pitfalls
- The order of the three packages is fixed:
varioref, thenhyperref, thencleveref. - For the total number of pages, as in "page 3 of 10", the
lastpagepackage provides the labelLastPage, see Page numbers in LaTeX. - Text that you add later can move a target to another page. LaTeX then warns that labels may have changed, and one more run corrects the numbers, see Label(s) may have changed. Rerun to get cross-references right.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- How-to Cross-references in LaTeX
- How-to References with automatic names in LaTeX
- How-to Page numbers in LaTeX
- Package
hyperref - Error Label(s) may have changed. Rerun to get cross-references right
- Package
graphicx - Package
babel - Package
cleveref