[Tex/LaTex] Alternating page numbers

fancyhdrheader-footerpage-numbering

How can I have alternating page numbers in the footer (using fancyhdr) in the form of (left footer) page x of y -nextpage- (right footer)page x2 of y? I now have

\pagestyle{fancy}
\cfoot{\thepage\ of \pageref{LastPage}}

But note that on the pages where the chapter starts for some reason the numbering does not apply. I am using the report class

Best Answer

Something like this? The pagestyle is patched into the \chapter command, however, this must be done after \tableofcontents has been issued, otherwise, this would be applied there too.

\documentclass[twoside]{report}

\usepackage{lastpage}
\usepackage{fancyhdr}
\usepackage{blindtext}
\usepackage{xpatch}

\fancypagestyle{mystyle}{%
  \fancyhf{}%
  \fancyfoot[LE,RO]{\thepage~of \pageref{LastPage}}%
  \renewcommand{\headrulewidth}{0pt}%
}

\begin{document}
\tableofcontents

% Now patch the command
\xpatchcmd{\chapter}{%
  \thispagestyle{plain}%
}{%
  \pagestyle{mystyle}
}{}{}

\chapter{My first chapter}
\blindtext[10]

\end{document}