0

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.

3
  • you should replace your \include with some dummy sectioning commands so that the code can be tested. But apart from this: it is normally not a good idea to show both section and subsection in a header: If a section begins on a page you could get e.g. 2.2. Section 1.3.9 Subsection which simply looks odd. And replace this curious \afterpage{\null\newpage} with \cleardoublepage. Dec 10, 2021 at 9:38
  • Your code can't even work because of the ##1. Dec 10, 2021 at 10:19
  • You should run your code before putting it here, so that you can remove the errors first. Dec 10, 2021 at 11:01

1 Answer 1

0

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

3
  • Your solution works, there is only one issue. I have a section with a long name and in the header it overlaps with the subsection name, specifically \section{Fracture mechanics as a possible reference scheme for the fracture characterization of the collagen membranes} \subsection{Introduction to fracture mechanics}. Is there a way to sove this? Maybe setting the maximum width for the section name at 0.5textwidth? Dec 10, 2021 at 11:07
  • The fancyhdr manual has a section on this problem: 33 Large chapter/section titles Dec 11, 2021 at 9:37
  • Thank you very much! Dec 12, 2021 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.