1

I would like to build a macro \myautoref{eq:a}, that

  1. if the link and the linked object are on the same page produces \autoref{eq:a}
  2. if the link and the linked object are on different pages produces \autoref{eq:a} on \autopageref{eq:a}

In my example below that would mean that the page reference appears only after the reference on page 1.

\documentclass{book}
\usepackage{amsmath}
\usepackage{nameref}
\usepackage[unicode,pdfpagelabels]{hyperref}
\begin{document}
\autoref{eq:a} on \autopageref{eq:a}

\pagebreak

\autoref{eq:a} on \autopageref{eq:a}

\begin{gather}\label{eq:a}
a
\end{gather}
\end{document}
1

answer based on Test equality between number of the current page and the one of one reference indicated

\documentclass{book}
\usepackage{amsmath}
\usepackage{nameref}
\usepackage[unicode,pdfpagelabels]{hyperref}

\usepackage{ifthen}
\usepackage{refcount}

\newcounter{sp}
\newcommand{\myautoref}[1]{%
    \stepcounter{sp}\label{sp-\thesp}%
    \autoref{#1}%
    \ifthenelse{\equal{\getpagerefnumber{#1}}{\getpagerefnumber{sp-\thesp}}}{}{%
        on \autopageref{#1}%
    }%
}

\begin{document}

\myautoref{eq:a}

\pagebreak

\myautoref{eq:a}

\begin{gather}\label{eq:a}
  a
\end{gather}
\end{document}
  • Thank you, there is a minor problem: if the "on page" is not needed a space is added(add a dor after myautoref to see it); from your code I can not understand why, its probably some latex trick. (I use xifthen instead of ifthen, but that seams not to make a difference) – PeptideChain Jul 13 at 4:45
  • 1
    I found the solution: after \autoref{#1} a % is neeeded: \autoref{#1}% – PeptideChain Jul 13 at 8:15
  • I just updated the solution to remove the space after \autoref – ebcontrol Jul 15 at 10:18
2

The functionality you're looking may be obtained in a straightforward fashion by loading the varioref, hyperref, and cleveref packages -- in this order.

Then, \vref{eq:1} produces

  • equation (1) if the cross-referencing call-out (produced by \vref) and the object being cross-referenced (here, an equation numbered (1)) are both on the same page;

  • equation (1) on the facing page if the document employs a two-sided layout -- as is the case for the book document class -- and if the call-out and the equation are located on facing pages;

  • either equation (1) on the following page or equation (1) on the preceding page if the page numbers differ by exactly 1 and and aren't placed on facing pages;

  • equation (1) on page [x] if the page numbers of the cross-reference and equation differ by 2 or more.

This setup is easily adapted to non-English languages. E.g., if ngerman is specified as a document class option and if the babel package is loaded as well, \vref{eq:1} will automatically produce "Gleichung (1) auf der vorherigen Seite" instead of "equation (1) on the preceding page".

An MWE:

\documentclass{book}

\usepackage{varioref}
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[noabbrev,nameinlink]{cleveref}

\begin{document}
\vref{eq:a}

\clearpage
\vref{eq:a}

\clearpage
\vref{eq:a}
\begin{equation}\label{eq:a} a \end{equation}

\clearpage
\vref{eq:a}

\clearpage
\vref{eq:a}

\end{document} 
  • 1
    Note that the answer doesn't use the \autoref macro of the hyperref package. Instead, it uses the \vref package of the varioref package. In the code above, hyperref is loaded mainly to make the cross-references into hyperlinks. A separate remark: The cleveref package is loaded above with the option nameinlink so that the output of \vref mimics that of \autoref; if you don't like this effect, just omit the option nameinlink. – Mico Jul 12 at 12:04
  • 1
    Thank, you. I would start now, I would probably use this solution, but after more than 1000 page I need to minimize the refactoring. – PeptideChain Jul 13 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.