[Tex/LaTex] Fancyhdr header: chapter name when there is no section

fancyhdrheader-footer

I have a problem with fancyhdr. My header shows on odd pages the chapter name, and on even pages the section name. However, I have a unnumbered chapter with no sections (the conclusions), and in that case the even page header shows the name of the last section from the previous chapter. This is an example of what I have

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

\fancypagestyle{mypagestyle}{
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}

\fancyhead[EL]{\small \textsl{\nouppercase{\rightmark}}}
\fancyhead[OR]{\small \textsl{\nouppercase{\leftmark}}}
\fancyhead[OL,ER]{\small \textsl \thepage}
}

\pagestyle{mypagestyle}

\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\renewcommand{\sectionmark}[1]{ \markright{#1}{} }

\begin{document}
\chapter{First chapter}
\section{First section}
\lipsum

\chapter*{Second chapter}
\lipsum

\chapter{Third chapter}
\lipsum
\end{document}

As you can see, this is a problem only with the unnumbered chapter, while for the numbered chapter with no sections nothing is shown instead of the section name.

What I would like to have in general would be the name of the chapter instead of the name of the section, when my chapter does not have sections. Any ideas?

Best Answer

First of all, if you want the header in numbered chapters, you have to change

\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }

to

\renewcommand{\chaptermark}[1]{ \markboth{#1}{#1} }

Then, since unnumbered chapters don't issue any \chaptermark command, you have to manually insert

\markboth{Second chapter}{Second chapter}

just after your unnumbered chapter.

MWE:

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

\fancypagestyle{mypagestyle}{
\fancyhf{}
\renewcommand{\headrulewidth}{0.5pt}

\fancyhead[EL]{\small \textsl{\nouppercase{\rightmark}}}
\fancyhead[OR]{\small \textsl{\nouppercase{\leftmark}}}
\fancyhead[OL,ER]{\small \textsl \thepage}
}

\pagestyle{mypagestyle}

\renewcommand{\chaptermark}[1]{ \markboth{#1}{#1} }
\renewcommand{\sectionmark}[1]{ \markright{#1}{} }

\begin{document}
\chapter{First chapter}
\section{First section}
\lipsum[1-30]

\chapter*{Second chapter}
\markboth{Second chapter}{Second chapter}
\lipsum[1-30]

\chapter{Third chapter}
\lipsum[1-30]
\end{document}

Output (unnumbered chapter)

enter image description here