[Tex/LaTex] Different headers for different chapters

fancyhdrheader-footer

I cannot find in any latex forums a solution for my problem with chapter-specific headers.

I am compiling a one-sided report with various chapters. For all the chapters, I have the header showing the chapter's number and title. But for one of them I would like to have the header showing the section's number and title instead (this is due to the nature of that particular chapter, which is longer than the others).

Everything works fine and I manage to show the section in the header. However, all the following chapters will not go back to the initial settings that shows the chapter in the header.

This is a minimal example with 3 chapters and you can run it to see the results. Chapter 1 and 3 should show the chapter in the header and only Chapter 2 is supposed to have the sections in its header. But Chapter 3 keeps applying the settings of the preceding although I've changed them.

I try resetting to \pagestyle{plain} or \pagestyle{empty} before adding the \renewcommand{\chaptermark} but it doesn't work.

\documentclass[11pt,a4paper,oneside]{report}
\usepackage{fancyhdr}
\begin{document}

\pagestyle{fancy}
\fancyhead{} % clear all header fields
\fancyhead[LO,LE]{\leftmark}
\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}}

\chapter{My First Chapter}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage
\section{Section 3}
\clearpage      

\renewcommand{\sectionmark}[1]{%
\markboth{\thesection.\ #1}{}}    
\chapter{My Second Chapter}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage

\renewcommand{\chaptermark}[1]{%
\markboth{\thechapter.\ #1}{}}    
\chapter{My Third Chapter}    
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage    

\end{document}

Best Answer

Define two page styles:

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

\pagestyle{fancy}
\fancypagestyle{normal}{%
  \fancyhead{} % clear all header fields
  \fancyhead[L]{\leftmark}}
\fancypagestyle{special}{%
  \fancyhead{} % clear all header fields
  \fancyhead[L]{\rightmark}}

\renewcommand{\chaptermark}[1]{%
  \markboth{\thechapter.\ #1}{}}
\renewcommand{\sectionmark}[1]{%
  \markright{\thesection.\ #1}{}}

\setlength{\headheight}{13.6pt}

\begin{document}

\chapter{My First Chapter}
\pagestyle{normal}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage
\section{Section 3}
\clearpage      

\chapter{My Second Chapter}
\pagestyle{special}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage

\chapter{My Third Chapter}    
\pagestyle{normal}
\section{Section 1}
\clearpage    
\section{Section 2}
\clearpage    
\section{Section 3}
\clearpage    

\end{document}

Don't forget to modify \headheight as suggested during compilation:

Package Fancyhdr Warning: \headheight is too small (12.0pt): 
 Make it at least 13.59999pt.
 We now make it that large for the rest of the document.
 This may cause the page layout to be inconsistent, however.