Looks like you want something like
\newcommand*\figureref[1]{Figure~\vref{#1}}
\renewcommand*\reftextbefore{(p.~\thevpagerefnum)}
\let\reftextfacebefore\reftextbefore
\let\reftextafter\reftextbefore
\let\reftextfaceafter\reftextbefore
\renewcommand*\reftextfaraway[1]{(p.~\pageref{#1})}
Edit: After reading varioref
more closely, I see my redefinition of \vref was unnecessary.
Edit 2: You didn't mention anything about hyperref
before. I'm not sure what the best way to go about this is, but the following sort of works.
\newcommand*\figureref[1]{Figure~\ref{#1}\vpageref[]{#1}}
\makeatletter
\let\old@vpageref\@vpageref
\def\@vpageref[#1]{\@ifnextchar[%]
{\vpageref@interpose{#1}}%
{\vpageref@interpose{#1}[\unskip\vref@space]}%
}
\def\vpageref@interpose#1[#2]#3{%
\def\thereflabel{#3}%
\@@vpageref{#1}[#2]{#3}%
}
\makeatother
\renewcommand*\reftextbefore{\reftextfaraway{\thereflabel}}
\let\reftextfacebefore\reftextbefore
\let\reftextafter\reftextbefore
\let\reftextfaceafter\reftextbefore
\renewcommand*\reftextfaraway[1]{(p.~\pageref{#1})}
I think hyperref
changes \@@vpageref
which is why I didn't just modify that directly.
Update:
Okay, going back to your original question. This is essentially what varioref
does to solve this problem.
\documentclass{article}
\newcount\testpagecount
\testpagecount0
\DeclareRobustCommand\ifrefthispage[3]{%
\global\advance\testpagecount1
\getpagenum\refstartnum{tpcstart\the\testpagecount}%
\getpagenum\refendnum{tpcend\the\testpagecount}%
\getpagenum\labelnum{#1}%
\ifx\refstartnum\refendnum\else
\GenericWarning{}{Reference to #1 fell on page
boundary \refstartnum-\refendnum}%
\fi
\testpagelabel{tpcstart\the\testpagecount}%
\ifx\labelnum\refendnum
#2%
\else
#3%
\fi
\testpagelabel{tpcend\the\testpagecount}%
}
\makeatletter
\def\getpagenum#1#2{%
\@ifundefined{r@#2}{\@namedef{r@#2}{{??}{??}}}{}%
\protected@edef#1{\expandafter\expandafter\expandafter
\@cdr\csname r@#2\endcsname\@nil}%
}
\def\testpagelabel#1{%
\protected@write\@auxout{}{\string\newlabel{#1}{{}{\thepage}}}%
}
\makeatother
\begin{document}
\section{Foo}\label{sec:foo}
\ifrefthispage{sec:foo}{Ref is on this page}{Ref is not on this page}
\newpage
\ifrefthispage{sec:foo}{Ref is on this page}{Ref is not on this page}
\end{document}
Basically, it generates two new labels, one on either side of the two replacement texts using \testpagelabel
which is nearly identical to the standard \label
, just without the actual label. On subsequent runs, it checks if the two labels straddle a page boundary. If they do, a warning is issued. (Admittedly, I didn't check that code path.) Then, if the second label is on the same page as the label you want to test, it uses the tokens in the second argument, otherwise it use the tokens in the third argument.
Note that on page boundaries, it could change every time you compile. I don't really know of a way to handle that .
\makeatletter
for the second of my three solutions. Anyway, my third solution was to basically copy whatvarioref
does to solve this problem, so it's not really very original.