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}.
Result
Page 1 of the compiled examplePage 1
Page 2 of the compiled examplePage 2

Open in the editor

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}.
Result
Page 1 of the compiled examplePage 1
Page 2 of the compiled examplePage 2

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}
Result
Page 1 of the compiled examplePage 1
Page 2 of the compiled examplePage 2

Open in the editor

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}
Result
Page 1 of the compiled examplePage 1
Page 2 of the compiled examplePage 2

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages