# Refer to LastPage when its Renamed

I am trying to get my page numbering as Page X of XX and I am using the standard syntax like,

\rhead{Page \thepage\ of \pageref{LastPage}}


It should work fine, expect that my last page has definitions like

\cleardoublepage
\renewcommand{\CoverName}{Back Cover}
\renewcommand{\thepage}{\CoverName}
\thispagestyle{empty}
\centering
\mbox{}
\vfill
\vfill


So I can get this

How can I keep the Back Cover and to enable the LastPage? I am thinking something like resetting the page number but I am not sure how to do it.

Thanks.

• That's not standard syntax. \rhead and LastPage are not part of LaTeX. (At least two packages define LastPage and various classes/packages define \rhead. These definitions are not equivalent.) Why don't you provide an MWE? – cfr May 30 '18 at 4:01

You are redefining the builtin \thepage-command, which is not good! If any other package use it (as you demonstrate e.g. LastPage) it will be affected by this. Also It has no purpose for the header on the last page when you have \thispagestyle{empty}.

I assume you do not want the back cover to count as the last page.

LastPage puts the current page number into a macro called \lastpage@lastpage. To achieve the (presumably) desired effect you could combine this with \number\numexpr. So you'd do \rhead{\thepage~of~\the\numexpr\lastpage@lastpage-1\relax}.

Furthermore, it seems that you might want to have the text "Back Cover" on the top right part of the back cover. In this case, you could just redefine \rhead instead of \thepage.

All in all:

\documentclass{article}
\usepackage{lastpage}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\makeatletter
% ^so we can use \macros containing @ in their macronames

% Just remove the -1 below if you did not want to subtract 1
\makeatother
\begin{document}
Page 1\clearpage Page 2\clearpage
% Switch out the line below with \rhead{Back Cover} if you want "Back Cover" in your header
\thispagestyle{empty}
\centering
~\vfill
\vfill
\end{document}

• hmmm, in my full tex file (along with lots of other packages), it did not work and gives me ! Undefined control sequence. \f@nch@orh ...of~\the \numexpr \lastpage@lastpage -1\relax \strut warning. I think I might have to find a different work around, as I also change the \rhead{ in the body of my tex. – Chen Stats Yu May 30 '18 at 13:41
• Did you remember to insert the \makeatletter? – Andreas Storvik Strauman May 30 '18 at 13:42