1

I tried to typeset the total pages number in right header by \@abspage@last under pagestyle fancy. Instead of number, the text abspage@last is printed. Why? And how to deal with it?

MWE:

\documentclass[a4paper]{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{Page \thepage\ of \makeatletter\@abspage@last\makeatother}
\begin{document}
First page:\\
total pages are: \@abspage@last\\ % typeset abspage@last. Why not give error message?
total pages are: \makeatletter\@abspage@last\makeatother % typeset the right number
\clearpage
Second page
\clearpage
Third page
\end{document}
lyl
  • 2,727
  • LaTeX is built on top of TeX, its capability to give error messages is limited (besides, that's a totally valid thing you can typeset in LaTeX)
  • – user202729 May 07 '22 at 06:23
  • use \makeatletter\rhead{Page \thepage\ of \@abspage@last}\makeatother. You must change the catcode before the @ is seen by TeX. – Ulrike Fischer May 07 '22 at 08:57
  • @Ulrike Fischer As for \rhead{Page \thepage\ of \makeatletter\@abspage@last\makeatother}, why does\makeatletter not change the catcode before the @ is seen by TeX? – lyl May 07 '22 at 15:17
  • TeX sees it when you define the \rhead in the preamble, not when it is executed. – Ulrike Fischer May 07 '22 at 15:19