# ifthenelse doesn't work in case of string comparison

I tried to use this code to place footer in all pages except the last page in which I just want an "end".

\ifthenelse
{\equal
{\detokenize{\thepage}}
{\detokenize{\pageref{LastPage}}}
}
{
\rfoot[]{\textit{End}}
}
{
\rfoot[]{\textit{Continued on next page :: \curpage :: \mylastpage}}
}


However, this prints Continued on next page... in all pages including the last page. I have tried without detokenize as well, but it doesn't work. Tried other ifs like pdfstrcmp, that also fails. Why does this comparison not work?

• Please add some code to make this example working (at least compiling) – user31729 Jan 9 '15 at 8:20
• with \detokenize you are comparing essentially thepage against pageref{LastPage} which are not equal strings, and without it you are comparing the expansion of \thepage which is the print form of the page number (not necessarily the number that this text will appear on) against the expansion of \pageref which could be all sorts of things including an error. \pageref does not work by expansion, and has different behaviour on thw first run when the ref is not known, or if hyperref is being used, when it makes a link, none of these will be \ifx equal to the expansion of \thepage – David Carlisle Jan 9 '15 at 9:04
• why not simply use \thispagestyle{empty} before \end{document} so the last page has an empty foot? – David Carlisle Jan 9 '15 at 9:06
• I've added another approach, which does not use the non-working \detokenize which is of no use here due to wrong concept of comparison partially non-expanding commands – user31729 Jan 9 '15 at 9:06
• Ok, I just have seen that David Carlisle provided a far more elaborate comment ;-) – user31729 Jan 9 '15 at 9:07

Doing \detokenize{\pageref{LastPage}} is surely not what you want, but even \pageref{LastPage} wouldn't work, because it doesn't produce a number, but the instructions to print a number.

Using zref-lastpage is the safest method I know.

\documentclass[12pt]{article}

\usepackage{blindtext}
\usepackage{fancyhdr}
\usepackage{zref-lastpage}
\usepackage{hyperref}

\makeatletter
\newcommand{\LastPageNumber}{\zref@extractdefault{LastPage}{page}{0}}
\makeatother

\pagestyle{fancy}
\fancyhf{}% clear all fields

\fancyfoot[R]{%
\ifnum\value{page}<\LastPageNumber
\textit{Continued on next page :: \thepage\ :: \LastPageNumber}%
\else
\textit{End}%
\fi
}

\begin{document}
\pagestyle{fancy}

\tableofcontents
\clearpage

\section{First}

\blindtext
\section{Second}
\blindtext[15]

\end{document}


• I recognize some parts of the code, especially the ones in the lower half ;-) – user31729 Jan 9 '15 at 15:54
• I took the liberty of using your text. ;-) – egreg Jan 9 '15 at 15:58
• Thank you. I actually tried to use the zref package as well but it didn't work. Your explanation clears things out that we need to get the actual value of the last page. – soham Jan 10 '15 at 1:44

This provides another way of achieving the requested feature (if I have understood the question correctly)

The totcount and assoccnt packages work together to get the counter value of the total number of pages, which can be used in \ifnumless comparison by etoolbox package.

\documentclass[12pt]{article}

\usepackage{etoolbox}
\usepackage{blindtext}
\usepackage{totcount}
\usepackage{assoccnt}
\usepackage{fancyhdr}

\usepackage{hyperref}

\newtotcounter{totalpages}
\setcounter{totalpages}{1}
\DeclareAssociatedCounters{page}{totalpages}

\rfoot[]{%
\ifnumless{\number\value{page}}{%
\number\totvalue{totalpages}
}{
\textit{Continued on next page :: \thepage :: \number\totvalue{totalpages}}
}{%
\textit{End}%
}%
}

\begin{document}
\pagestyle{fancy}

\tableofcontents
\clearpage

\section{First}

\blindtext
\section{Second}%
\blindtext[15]

\end{document}