# How can I conditionally include the page number in a cross-reference (when the object referenced is on a different page)?

When a cross-reference to a figure is not on the same page as the figure, I would like to include the page number in the figure. But when they are on the same page, this is redundant--and I want to omit the page number.

In other words, I want to switch between these two styles of figure referencing depending on samepage/differentpage:

\newcommand{\figref}[1]{Figure~\ref{fig:#1}, page \pageref{fig:#1}}
\newcommand{\simplefigref}[1]{Figure~\ref{fig:#1}}


Probably Testing for current page and conditionals can do this, has anybody got a recipe worked out?

-

Try the varioref package.

\documentclass[english]{scrartcl}

\usepackage{babel}
\usepackage{varioref}

\usepackage{blindtext}% just for testing

\begin{document}
Text \vref{fig1}. \blindtext

\blindtext[5]% change this number to see
% the different outputs of \vref

\begin{figure}
\rule{2cm}{2cm}
\caption{Text}\label{fig1}
\end{figure}
\end{document}

-