[Tex/LaTex] Issue redefining plain style fancyhdr

fancyhdrheader-footer

I am trying to generate a header/footer layout per the following:

Frontmatter – roman page numbers in footer, right aligned, no header

Mainmatter – arabic page numbers in footer, chapter name and number in header, all right
aligned

Backmatter – arabic page numbers in footer, no header, right aligned

Using the below code, I am almost there – except the page numbers are not right aligned in \pagestyle{plain}

\documentclass[11pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{lipsum}

%Fancyhdr settings
\lhead{}
\chead{}
\lfoot{}
\cfoot{}
\rfoot{\thepage}
\renewcommand{\chaptermark}[1]{\markboth{#1}{#1}}
\fancyhead[R]{\chaptername\ \thechapter\ --\ \leftmark}
\fancyhead[L]{}
\renewcommand{\headrulewidth}{0pt}  

\begin{document}

\chapter*{Abstract}
\pagestyle{plain}
\pagenumbering{roman}

\lipsum{}

\chapter{Introduction}
\pagestyle{fancy}
\pagenumbering{arabic}

\lipsum{}

\chapter*{Appendix}
\pagestyle{plain}

\lipsum{}

\end{document}

When I redefine the plain pagestyle footer to be right aligned using

\fancypagestyle{plain}{%
  \fancyhf{}% clear all header and footer fields
  \rfoot{\thepage}%
}

The header in the fancy pagestyle is no longer displayed.

I've tried a few workarounds, to no avail. The line \renewcommand{\chaptermark}[1]{\markboth{#1}{#1}} also appears to have some handling issues when used inside a page style redefinition (i.e. \fancypagestyle{plain}{%) or if inserted after \begin{document}

Any help would be much appreciated

Best Answer

You could use pagestyle fancyplain and command \fancyplain{<plain content>}{<fancy content>}:

\documentclass[11pt,a4paper]{report}
\setlength{\headheight}{13.6pt}
\usepackage{lipsum}
\usepackage{fancyhdr}

\pagestyle{fancyplain}
\fancyhf{}
\fancyfoot[R]{\thepage}
\renewcommand{\chaptermark}[1]{\markboth{\chaptername\ \thechapter\ --\ #1}{}}
\fancyhead[R]{\fancyplain{}{\leftmark}}
\renewcommand{\headrulewidth}{0pt}

\begin{document}
\cleardoublepage
\pagenumbering{roman}
\chapter*{Abstract}
\pagestyle{plain}
\lipsum

\cleardoublepage
\pagenumbering{arabic}
\pagestyle{fancy}
\chapter{Introduction}
\lipsum

\clearpage
\pagestyle{plain}
\chapter*{Appendix}
\lipsum{}
\end{document}

or maybe better

\documentclass[11pt,a4paper]{report}
\setlength{\headheight}{13.6pt}
\usepackage{lipsum}
\usepackage{fancyhdr}

\pagestyle{fancyplain}
\fancyhf{}
\fancyfoot[R]{\thepage}
\fancyhead[R]{\fancyplain{}{\nouppercase\leftmark}}
\renewcommand{\headrulewidth}{0pt}

\usepackage{xpatch}
\xpatchcmd\chaptermark{. }{\ --}{}{\PatchFailed}

\begin{document}
\cleardoublepage
\pagenumbering{roman}
\chapter*{Abstract}
\pagestyle{plain}
\lipsum

\cleardoublepage
\pagenumbering{arabic}
\pagestyle{fancy}
\chapter{Introduction}
\lipsum

\clearpage
\pagestyle{plain}
\chapter*{Appendix}
\lipsum{}
\end{document}

Result:

enter image description here

Related Question