10

Trying to make conditional expressions that inspect the total page count of the document (see this question), I've found this solution using ifthen package:

\documentclass{article}
\usepackage{lipsum}
\usepackage{ifthen}
\usepackage{lastpage}
\begin{document}

\ifthenelse{\pageref{LastPage}>1}{\pagestyle{plain}}{\pagestyle{empty}}

\lipsum[2-3]

\end{document}

I would like to try to use etoolbox instead of ifthen because it looks like now it is a more commonly used package. My following attempt does not work:

\documentclass{article}
\usepackage{lipsum}
\usepackage{etoolbox}
\usepackage{lastpage}

\begin{document}

\ifnumcomp{\pageref{LastPage}}{>}{1}{\pagestyle{plain}}{\pagestyle{empty}}

\lipsum[2-13]

\end{document}

So, how can i do this with etoolbox

  • Probably this should be a Community Wiki. – Alexey Sep 27 '14 at 12:04
  • My other question has a couple of acceptable answers now, but i hesitate to delete this question because i am still curious if it is possible to use something like \pageref{LastPage} in a conditional from etoolbox. – Alexey Sep 27 '14 at 12:50
  • Asking for improvements in working solutions should be fully legitimate. – Sveinung Sep 27 '14 at 12:57
  • 1
    It is not a good idea to use the result of \pageref as a number and to do number comparision -- neither with ifthen or with etoolbox. It could also contain text or formatting instruction or be a roman number. – Ulrike Fischer Sep 27 '14 at 13:31
  • 1
    You can compare two \pagerefs, e.g. with etoolbox \ifdefstrequal{\pageref{a}}{\pageref{b}}{yes}{no}. When I really want to be on the safe side I use the zref package to get real numbers to compare. – Ulrike Fischer Sep 27 '14 at 13:52
9

The main problem is that \pageref{LastPage} cannot be used in the argument of \ifthenelse, nor in the etoolbox functions, because it's only good for printing the page reference.

One has to use a different approach, with the safer package zref-lastpage.

\documentclass{article}
\usepackage{zref-lastpage}

\makeatletter
\AtBeginDocument{% this must be executed after the aux file has been input
  \ifnum\zref@extractdefault{LastPage}{page}{0}>1
    \pagestyle{plain}%
  \else
    \pagestyle{empty}%
  \fi
}
\makeatother

\begin{document}
x

%\clearpage
%y

\end{document}

Instead of \ifnum the corresponding etoolbox function can be used

\makeatletter
\AtBeginDocument{% this must be executed after the aux file has been input
  \ifnumcomp{\zref@extractdefault{LastPage}{page}{0}}{>}{1}
    {\pagestyle{plain}}
    {\pagestyle{empty}}%
}
\makeatother

However this still requires \makeatletter and \makeatother and \AtBeginDocument like the code above, because we have to ensure the code is executed after the .aux file has been read in.

Uncommenting the lines with % will show the page number on both pages; as it is no page number will be printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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