I use this code to prevent hyperlinks if the target of the link is presented on the same page as the reference: \newcommand*{\myref}[1]{\ifnumequal{\thepage}{\getpagerefnumber{#1}}{\ref*{#1}}
However, I just realised that the code does not work if the hyperlink is in the first paragraph on a new page, see the minimal working example below. Does anyone have any idea what is wrong? Any help is much appreciated!
\documentclass{article}
\usepackage{hyperref}
\usepackage{etoolbox}
\usepackage{lipsum}
\makeatletter
\newcommand*{\myref}[1]{\ifnumequal{\thepage}{\getpagerefnumber{#1}}{\ref*{#1}}{\ref{#1}}}
\makeatother
\begin{document}
\section{first page}
Figures on first page \myref{fig:1} and second page \myref{fig:2}. \LaTeX consider this to be on page \thepage %%% links to figure 2 but not 1 - correct
Figures on first page \myref{fig:1} and second page \myref{fig:2}. \LaTeX consider this to be on page \thepage %%% links to figure 2 but not 1 - correct
\begin{figure}[ht]
\centering
\rule{6cm}{3cm}
\caption{Figure caption text} \label{fig:1}
\end{figure}
\lipsum[1-3]
\section{second page}
Figures on first page \myref{fig:1} and second page \myref{fig:2} %%% links to figure 2 but not 1 - incorrect!
Figures on first page \myref{fig:1} and second page \myref{fig:2} %%% links to figure 1 but not 2 - correct
\begin{figure}[ht]
\centering
\rule{6cm}{3cm}
\caption{Figure caption text} \label{fig:2}
\end{figure}
\end{document}
pagecounter still has the value even 1 on the second page on the top (first paragraph) so the comparison jumps to the false branch. You can see this, if you insert a\thepagebetween the two linesFigures on ..in the lower half of your document – Jan 14 '15 at 13:34thepageis the typeset page, not a number ->\value{page}. – Johannes_B Jan 14 '15 at 13:38variorefdo to vary the reference text depending whether or not it is a same-page reference. – David Carlisle Jan 14 '15 at 13:43