# Show “Page X of Y” only if there are multiple pages?

I'm trying to add a line in the header to be "Page X of Y", but only if there are multiple pages. My main problem is how to format the if statement. So far I have

    \ifx \pageref{LastPage} 1
\relax
\else
\fi


I think it's the first line that is problematic, as the rest seems to work.

-

\ifnum\getpagerefnumber{LastPage}=1


instead of \ifx\pageref{LastPage}1 (which is wrong for other reasons). See this question.

"Full code":

\usepackage{refcount,lastpage}
\usepackage{fancyhdr}
\ifnum\getpagerefnumber{LastPage}=1
\else
Page \thepage\ of \pageref{LastPage}%
\fi
}


However, Marco's suggestion to use zref seems to be better:

\usepackage{fancyhdr}
\usepackage[totpages,user]{zref}
\ifnum\ztotpages=1
\else
Page \thepage\ of \ztotpages
\fi
}
\pagestyle{fancy}

-
@Gonzalo Ah, yes! I'll add to my answer. – egreg Aug 30 '11 at 19:55
For information, I ran into trouble when I tried using etoolbox's \AtEndDocument with the lastpage package loaded. I kept getting Page 3 of 2. The zref-totpages solved my problem and seems more reliable. – Jubobs Nov 21 '13 at 12:55

I tried the solution of egreg but it doesn't work. With the hint of Gonzalo Medina(see comment below) it works well (see the example below)

I used the zref packages which is recommended by lastpage

\documentclass{report}
\usepackage{fancyhdr}

\usepackage[totpages,lastpage,user]{zref}
\usepackage{ifthen}
\usepackage{hyperref}
\AtBeginDocument{
\ifthenelse{\ztotpages = 1}{}{\fancyhead[R]{Page \thepage\ of \zpageref{LastPage}}}}
\pagestyle{fancy}
\begin{document}

Text \zpageref{LastPage}

\clearpage
TExt
\end{document}


Example of egreg:

\documentclass{report}
\usepackage{fancyhdr}
\usepackage{refcount,lastpage}
\ifnum\getpagerefnumber{LastPage}=1\relax%
\else
Page \thepage\ of \pageref{LastPage}%
\fi%
}
\pagestyle{fancy}
\begin{document}

Text

\clearpage
TExt
\end{document}


@egreg: Sorry

-
egreg's solution works using \fancyhead[R]{ \ifnum\getpagerefnumber{LastPage}=1 \relax \else Page \thepage\ of \pageref{LastPage} \fi } – Gonzalo Medina Aug 30 '11 at 20:26
@Gonzalo Medina: Thanks. I will edit my post. – Marco Daniel Aug 30 '11 at 20:30

Here is a solution using the xstring package:

\documentclass{article}
\usepackage{lipsum}
\usepackage{refcount}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{xstring}

\pagestyle{fancy}