# Test equality between number of the current page and the one of one reference indicated

In the following code, I have one problem with the test \ifthenelse{\equal{\pageref{#1}}{\thepage}}{...}{...}. Why doesn't this detect the equality hoped if the reference is in the same page where it is used and defined ?

% Sources :
%   * http://tex.stackexchange.com/questions/35095/special-footnotes-for-url/35097#35097
%   * http://tex.stackexchange.com/questions/35043/how-to-reference-different-places-to-the-same-footnote
%   * http://forum.mathematex.net/latex-f6/forcer-le-retour-a-la-ligne-dans-texttt-t13246.html#p127511
%   * http://tex.stackexchange.com/questions/33465/changing-the-catcode-of-in-one-command

\documentclass[10pt]{article}
\usepackage[svgnames]{xcolor}
\usepackage{manyfoot}
\usepackage{xifthen}
\usepackage{hyperref}
\usepackage{varioref}

% Cosmetic
\hypersetup{urlcolor=blue}
\definecolor{urlColor}{named}{DarkRed}

\newcommand{\urlText}[2][0]{%
\textcolor{urlColor}{%
\ifthenelse{\equal{#1}{0}}{%
\textbf{#2}%
}{%
#2%
}%
}%
}

% Special footnote
\newfootnote{Url}

\newcounter{footnoteUrl}
\newcommand{\footnoteUrl}{%
%       \renewcommand\thefootnoteUrl{\Alph{footnoteUrl}}
\stepcounter{footnoteUrl}%
\Footnotemark{\urlText{\#\thefootnoteUrl}}\FootnotetextUrl{}%
}

% New url
\newcommand{\newUrl}[2][0]{%
\ifthenelse{\equal{#1}{0}}{%
\footnoteUrl{\, \url{#2}}%
}{%
\footnoteUrl{\,\label{#1} \url{#2}}%
}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%
% THE PROBLEM IS HERE... %
%%%%%%%%%%%%%%%%%%%%%%%%%%

% Old url
\newcommand{\oldUrl}[1]{%
\ifthenelse{\equal{\pageref{#1}}{\thepage}}{}{%
\urlText[1]{(cf.\,page\,\pageref{#1})}%
}%
}

\usepackage{lipsum}

\begin{document}

and another \newUrl{http://tex.stackexchange.com/}.
Let's try to indicate one old URL \oldUrl{linkGoogle}.
and one more \newUrl{http://tug.org/texlive/pkginstall.html}
and another \newUrl[ctan]{http://www.ctan.org/}
and then the same \oldUrl{ctan}.
I would like to indicate something \footnote{... but here !}.

\lipsum

Just only old URLS \oldUrl{linkGoogle} and \oldUrl{ctan}.
I would like to indicate something \footnote{... but here !}.
\lipsum

Just only old URLS \oldUrl{linkGoogle} and \oldUrl{ctan}.
I would like to indicate something \footnote{... but here !}.

\end{document}

-
You should use the bigfoot package that cooperates with hyperref, while manyfoot doesn't. – egreg Nov 16 '11 at 23:59
– Werner Nov 17 '11 at 0:33

Since you use hyperref, your \pageref is not just a number, but a link. Thus it cannot be equal to \thepage.

Fortunately, varioref package provides the command \vrefpagenum, which assigns to its first argument (a macro!) the page label generated by the second argument.

This works for me:

\newcommand{\oldUrl}[1]{%
\vrefpagenum{\urlpage}{#1}%
\ifthenelse{\equal{\urlpage}{\thepage}}{}{%
\urlText[1]{(cf.\,page\,\pageref{#1})}%
}%
}


UPDATE: Note the important caveat about page number in the answer by Gonzalo Medina

-
Thanks, your solution works. – projetmbc Nov 17 '11 at 8:36

You can use the commands provided by the refcount package; from the documentation:

References are not numbers, however they often store numerical data such as section or page numbers. \ref or \pageref cannot be used for counter assignments or calculations because they are not expandable, generate warnings, or can even be links, The package provides expandable macros to extract the data from references.

So, you could say something like

\usepackage{refcount}

\newcommand{\oldUrl}[1]{%
\ifthenelse{\equal{\getpagerefnumber{#1}}{\thepage}}{}{%
\urlText[1]{(cf.\,page\,\pageref{#1})}%
}%
}


Also, due to the asynchronous page building mechanism, it's not completely safe to rely on \thepage. A standard trick is to use a \label and then invoke \pageref for this label, so you could use something like

\usepackage{refcount}

\newcounter{sp}
\newcommand{\oldUrl}[1]{%
\stepcounter{sp}\label{sp-\thesp}%

Why \protect? AFAIR, the LaTeX manual says never to put \protect in front of \value. – egreg Nov 17 '11 at 0:04
@egreg: right; Lamport says that \value is robust and must never be preceded by a \protect command. When I was writing my answer I didn't use initially \protect and got the error ! You can't use \relax' after \the. <recently read> \c@abspage; I decided then to \protect \value, and didn't get any errors, so I posted my answer; now, however, that you have pointed this issue out, I see that my answer doesn't work as expected? – Gonzalo Medina Nov 17 '11 at 1:01
@egreg: while I try to solve the abspage issue, I've edited my answer with the usual \label, \pageref` trick. – Gonzalo Medina Nov 17 '11 at 2:04