Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
    \fancyhead[R]{Page \thepage\ of \pageref{LastPage}}
\fi

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

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

You can try loading the refcount package and using

\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}
\fancyhead[R]{%
  \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}
\fancyhead[R]{%
  \ifnum\ztotpages=1
  \else
    Page \thepage\ of \ztotpages
  \fi
}
\pagestyle{fancy}
share|improve this answer
    
@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
add comment

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}
\fancyhead[R]{%
  \ifnum\getpagerefnumber{LastPage}=1\relax%
    \else
       Page \thepage\ of \pageref{LastPage}%
  \fi%
}
\pagestyle{fancy}
\begin{document}

Text 

\clearpage
TExt
\end{document}

@egreg: Sorry

share|improve this answer
    
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
add comment

Here is a solution using the xstring package:

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

\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\fancyhead[R]{\IfEq{\getpagerefnumber{LastPage}}{1}{}{Page \thepage\ of \pageref{LastPage}}}

\begin{document}
\lipsum[1-3]
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.