1

Using \documentclass[twoside]{report}, I would like the following headers:

  • on the left, for even pages Part Title: Chapter Title
  • on the right, for odd pages Section Title

In the MWE, I'm mostly getting what I want, based on these questions:

However, as seen on pages 3 and 7, when a chapter begins on an odd-numbered page, the section title doesn't appear in the header. Given the behavior on page 6, headings are working for even-numbered chapter start pages.

How do I get the section title to appear in the header on odd-numbered pages where a chapter begins?

MWE

    \documentclass[twoside]{report}
    \usepackage{fancyhdr}
    \usepackage[english]{babel}
    \usepackage{blindtext}
    \usepackage{etoolbox}

    %https://tex.stackexchange.com/a/13395/56480
    \let\Oldpart\part
    \newcommand{\parttitle}{}
    \renewcommand{\part}[1]{
        \Oldpart{#1}
        \renewcommand{\parttitle}{#1}   
    }

    \pagestyle{fancy}
    \fancyhf{}
    \renewcommand{\chaptermark}[1]{\markboth{\parttitle: #1}{}}
    \renewcommand{\sectionmark}[1]{\markright{#1}}

    \fancyhead[RO]{\rightmark}       
    \fancyhead[LE]{\leftmark}  
    \fancyfoot[C]{\thepage}

    %https://tex.stackexchange.com/a/19741/56480
    \patchcmd{\chapter}{plain}{fancy}{}{}

    \begin{document}

    \tableofcontents
    \part{First Part}
    \chapter{First Chapter}
    \blindtext[1]
    \section{First Section}
    \blindtext[5]
    \section{Second Section}
    \blindtext[7]
    \chapter{Second Chapter}
    \blindtext[1]
    \section{Third Section}
    \blindtext[2]
    \chapter{Third Chapter}
    \blindtext[1]
    \section{Fourth Section}
    \blindtext[5]\end{document}
  • This is not good typography: headers are provided to help you find at which place you are in the document you're reading. On the first page of a chapter, this information is totally redundant. – Bernard Sep 26 '16 at 18:50
  • @Bernard: You're right. When posting a question, it seems we find ourselves caught between the tension between an over-complicated MWE, or an MWE that captures the functional concerns of the behavior, but doesn't necessarily satisfy the typographical concerns of every reader. It seems either case inspires ire. Meanwhile, my question remains unanswered. – twip Sep 26 '16 at 19:16
  • this is a puzzler. i don't see any reason in the file report.cls for this to be happening. one can see from the heading on the toc page that running head text is inserted, and if the first \chapter is changed to \chapter*, it gets text too. so there is something different between \@chapter and \@schapter that isn't obvious. the next place to look, it seems, is to latex.ltx, but that makes no sense (and also, i haven't got time to dig through that just now). i'll take another look later, but maybe this clue will help someone else. – barbara beeton Sep 26 '16 at 19:25
  • I wasn't angry. Just wanted to draw your attention to this point. That said, what do you mean with part name: the word ‘part’, or the part title? All this should be easy to do with titlesec/titleps ‘ at least for standard classes. – Bernard Sep 26 '16 at 19:28
  • @barbarabeeton: Thank you for taking a look. I wasn't sure where to investigate, let alone where the trail might lead. It might be that messing with the headers in this way will end in TeXnicalities. – twip Sep 26 '16 at 20:03
1

A mark command sets three variables: \topmark (last mark of the previous page), firstmark (first mark on the current page) and \botmark (last mark on the current page). \rightmark uses the \firstmark set by \markright or the second argument of \markboth. On a chapter page the first call of \markboth is done by \chapter with an empty second argument to reset the header entry.

If it is alright to use the last section occurring on a page you could define a command \rightbotmark using \botmark instead \firstmark.

\makeatletter
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\makeatother

and then

\fancyhead[RO]{\rightbotmark}

enter image description here

Code:

\documentclass[twoside]{report}
\usepackage{fancyhdr}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{etoolbox}


%http://tex.stackexchange.com/a/13395/56480
\let\Oldpart\part
\newcommand{\parttitle}{}
\renewcommand{\part}[1]{
    \Oldpart{#1}
    \renewcommand{\parttitle}{#1}   
}

\makeatletter
\newcommand*{\rightbotmark}{%
    \expandafter\@rightmark\botmark\@empty\@empty
}
\makeatother

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{\parttitle: #1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}

\fancyhead[RO]{\rightbotmark}
\fancyhead[LE]{\leftmark}
\fancyfoot[C]{\thepage}

%http://tex.stackexchange.com/a/19741/56480
\patchcmd{\chapter}{plain}{fancy}{}{}

\begin{document}

\tableofcontents
\part{First Part}
\chapter{First Chapter}
\blindtext[1]
\section{First Section}
\blindtext[5]
\section{Second Section}
\blindtext[7]
\chapter{Second Chapter}
\blindtext[1]
\section{Third Section}
\blindtext[2]
\chapter{Third Chapter}
\blindtext[1]
\section{Fourth Section}
\blindtext[5]
\end{document}
  • Thank you for the explanation about what goes on with the \mark command. I'm accepting this answer because I found it to be the most helpful. – twip Sep 27 '16 at 15:45
2

Here is a solution with titlesec and etoolbox. We have to redefine \part from within titlesec:

\documentclass[twoside]{report}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{etoolbox}

\usepackage[pagestyles,  outermarks,  clearempty]{titlesec}%
\titleformat{\part}[display]{\filcenter\bfseries\Huge}{\partname~\thepart}{3ex}{}[\thispagestyle{empty}]
\newpagestyle{mypagestyle}{
\settitlemarks{part, chapter, section}
\headrule
\sethead[\parttitle:   \chaptertitle][][]{}{}{\sectiontitle}
\setfoot{}{\thepage}{}
}
\pagestyle{mypagestyle}
\patchcmd{\chapter}{plain}{mypagestyle}{}

\begin{document}

\tableofcontents\thispagestyle{plain}
\part{First Part}
\chapter{First Chapter}%\thispagestyle{mypagestyle}
\blindtext[1]
\section{First Section}
\blindtext[5]
\section{Second Section}
\blindtext[7]
\chapter{Second Chapter}
\blindtext[1]
\section{Third Section}
\blindtext[2]
\chapter{Third Chapter}
\blindtext[1]
\section{Fourth Section}
\blindtext[5]

\end{document} 

enter image description here

  • Thank you for the alternative using titlesec. I like that syntax, and I'll be looking at using that package in the future. – twip Sep 27 '16 at 15:46
  • I draw your attention to the fact that titleps is incompatible with fancyhdr. Personally, I find the former more easy to use (no need to fiddle with marks). – Bernard Sep 27 '16 at 15:53

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.