[Tex/LaTex] fancyhdr overwrites the page numbering settings

fancyhdrheader-footerpage-numbering

\documentclass{mwrep}

%page number on the end of chapter
\makeatletter
\let\ps@closing\ps@plain
\makeatother

%margins
\usepackage{anysize}
\marginsize{3cm}{3cm}{2cm}{2cm}

\usepackage{lipsum}

%mark
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}\chead{}\rhead{}\lfoot{}\cfoot{}\rfoot{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%mark

\begin{document}
\chapter{AAAAAAA}
\section{aaaaaaa}
\lipsum
\section{bbbbbbb}
\lipsum
\section{ccccccc}
\lipsum
\chapter{BBBBBBB}
\section{aaaaaaa}
\lipsum
\section{bbbbbbb}
\lipsum
\section{ccccccc}
\lipsum
\chapter{CCCCCCC}
\section{aaaaaaa}
\lipsum
\section{bbbbbbb}
\lipsum
\section{ccccccc}
\lipsum
\end{document}

When you remove code between %marks, you get a number on every page.
When I use fancyhdr, those settings are overwritten and page number is ommited at the beginning and the end of chapter. Is there a way to fix that? I'm only using fancyhdr to change position of page number.

Best Answer

The mwrep class has a very peculiar way of managing page styles, so its interaction with fancyhdr might be difficult; however this seems to work:

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}\fancyfoot[R]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancypagestyle{opening}{\fancyhf{}\fancyfoot[R]{\thepage}%
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}}
\fancypagestyle{closing}{\fancyhf{}\fancyfoot[R]{\thepage}%
  \renewcommand{\headrulewidth}{0pt}
  \renewcommand{\footrulewidth}{0pt}}

We simply redefine the opening and closing page styles so that they use fancyhdr way of doing the business.

Also remove the code you have regarding \ps@closing.

A solution without fancyhdr might be

\makeatletter
\def\ps@plain{%
  \let\@oddhead\@empty
  \def\@oddfoot{\reset@font\hfil\thepage}%
  \let\@evenhead\@empty
  \let\@evenfoot\@oddfoot}
\let\ps@opening=\ps@plain
\let\ps@closing=\ps@plain
\makeatother
\pagestyle{plain}

All things considered, I prefer the first solution, albeit a bit long, because it lets you customize the page style in case of needs just by using the commands provided by fancyhdr rather than going into low level commands such as \@oddfoot and so on.