[Tex/LaTex] How to translate \ifthenelse from “ifthen” to “etoolbox”

conditionalsetoolbox

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

Best Answer

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.