Headers with fancyhdr (or others): Chapter on odd pages, Section and Subsection on even pages

fancyhdrheader-footersubsection

I'm using a double sided report for my thesis and in the headers I want to show: Chapter number and name (on the left) on odd pages; Section number and name (on the left) and Subsection number and name (on the right) on even pages. If there is no subsection I want to display only the section.
Example, if you run the code below I would like to have:

  • on page 9: 1.3 Mechanical characterization (as there is no subsection)
  • on page 10: Chapter 1. Experimental section (ok)
  • on page 11: 1.3 Mechanical characterization (on the left) 1.3.1 Tensile tests (on the right)

I tried \renewcommand solution, suggested by several users but it still doen't work:

\renewcommand{\sectionmark}[1]{\markright{\thesection\ ##1}}

\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ ##1}}

\renewcommand{\subsubsectionmark}[1]{\markright{\thesubsubsection\ ##1}}

This is my code, every chapter contains several sections and subsections:

    \documentclass[openright, twoside]{report}
    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}
    \usepackage{geometry}
    \setcounter{tocdepth}{3}
    \setcounter{secnumdepth}{3}
    \usepackage{lipsum}
    \usepackage{adjustbox}
    \usepackage{afterpage}
    \usepackage[sorting=none]{biblatex} %Imports biblatex package
    \addbibresource{references.bib} %Import the bibliography file
    \usepackage{indentfirst}
    \usepackage{tabularx}
    \usepackage{multirow} %Required for multirows
    \usepackage{enumerate}
    \usepackage[skip=5pt]{caption}
    \usepackage{subcaption}
    \usepackage{blindtext}
    
    \newcommand\blankpage{%
        \null
        \thispagestyle{empty}%
        \addtocounter{page}{-1}%
        \newpage}
    
    \newgeometry
    {
    top=2.5cm,
    bottom=2cm,
    outer=2cm,
    inner=2cm,
    }
    \usepackage{fancyhdr}
    
    \renewcommand{\sectionmark}[1]{\markright{\thesection\ ##1}}
    
    \renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ ##1}}
    
    \renewcommand{\subsubsectionmark}[1]{\markright{\thesubsubsection\ ##1}}
    
    
    \begin{document}
    \setlength{\parskip}{0pt}
    
    
    \tableofcontents
    \listoffigures
    \listoftables
    \afterpage{\null\newpage}
    \newpage
    
    \pagestyle{fancy}
    \fancyhf{}
    \fancyhead[LE]{\leftmark}
    \fancyhead[RO]{\rightmark}
    \fancyfoot[LE,RO]{\thepage}
    
    \chapter{Experimental section}
\section{Examined membranes}
\label{examined_CM_section}
\blindtext 
\section{Structural characterization}
\blindtext 
\subsection{Methodological and experimental details}
\blindtext 
\subsection{Results}
\blindtext 
\section{Mechanical characterization}
\blindtext\blindtext\blindtextblindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext\blindtext
\blindtext \blindtext \blindtext 
\subsection{Tensile tests}
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext \blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 
\blindtext\blindtext\blindtext \blindtext \blindtext \blindtext 

\subsubsection{Methodological and experimental details}
\blindtext 
\subsubsection{Results}
\blindtext 
\subsubsection{Comparison of different membranes}
\subsection{Fracture mechanics tests}
\subsubsection{Methodological and experimental details}
\paragraph{Determination of $\eta_{pl}$}
\paragraph{Essential work of fracture approach}
\subsubsection{Results}
\subsubsection{Comparison of different membranes}
    \end{document}

I put the \pagestyle{fancy} command after \tableofcontents, \listoffigures, \listoftables because I don't want headers in this part of the document.

I'm opened to other solutions that fancydhr package.

Thank you.

Best Answer

In your headers you have three pieces of information: chapter, section and subsection. Standard LaTeX only has two marks, \leftmark and \rightmark, so this doesn't fit. Fortunately, chapters always start at a new page, so you can put the chapter header in a macro, and then you can use \leftmark for section and \rightmark for subsection.


\newcommand{\chaptertitle}{}
\renewcommand{\chaptermark}[1]{\renewcommand{\chaptertitle}{Chapter \thechapter\ #1}}
\renewcommand{\sectionmark}[1]{\markboth{\thesection\ #1}{}}
\renewcommand{\subsectionmark}[1]{\markright{\thesubsection\ #1}}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LO]{\chaptertitle}
\fancyhead[LE]{\leftmark}
\fancyhead[RE]{\rightmark}
\fancyfoot[LE,RO]{\thepage}

enter image description here