# How can I display the page number of a cross-referenced object, but only if the object is on different page?

I am creating a command called \fullref{<label>} that takes a label and outputs the number of the labeled object and name and optionally the page number where to find the object if it's on a page that differs from where the cross-referencing call-out occurs.

Examples:

Let a label \label{chap_intro} and a text … as seen in chapter \fullref{chap_intro}..

If they are on the same page, the text will output:

… as seen in chapter 1.1: "Introduction".

If they are on different pages, the text will output:

… as seen in chapter 1.1: "Introduction" on page 2.

Here is my command so far:

latex

\newcommand{\fullref}[1]{\ref{#1}: "\nameref{#1}" \ifthenelse{
\equal{\thepage}{\pageref{#1}} % if the current page and the referenced page are the same
}
{} % then do nothing
{ on page \pageref{#1}} % else append "on page …" if different
}


Even when on the same page, it displays "on page …".

What did I do wrong?

• Welcome to TeX.SE. Are you familiar with the varioref package? – Mico Aug 7 '20 at 7:50
• I am familiar with nothing, I started using LaTeX 2 days ago. I'll have a look, thank you! – SteeveDroz Aug 7 '20 at 7:52
• @Mico it works! If you transform your comment into an answer, I'll accept it. – SteeveDroz Aug 7 '20 at 8:09
• In general, \getrefnumber and \getpagerefnumber will allow you to perform tests on the values (refcount and hyperref packages). – John Kormylo Aug 7 '20 at 13:18

The varioref package and its \vref user macro accomplish what your \fullref macro aims to do.
The package works with babel, and it offers ways to customize what is supposed to be typeset if the cross-referenced object happens to be on, near, or far away from the cross-referencing call-out. This customization works by redefining the macros \reftextbefore, \reftextfacebefore, \reftextafter, \reftextfaceafter, and \reftextfaraway; see section 5 of the package's user guide for the full details.