3

I am trying to get a header set-up, but I'm facing some issues. The way I want to have it displayed is:

(Part Nr.): (Part Name) ---- (Ch nr.). (Chapter name)

The header should be displayed on all pages except the page where the new part starts.

\documentclass[a4paper]{report}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{lipsum}
\pagestyle{fancy}

\makeatletter
\@addtoreset{chapter}{part}
\makeatother

\begin{document}
\part{Part one}
\chapter{Chapter one}
\lipsum[1]
\section{Section one}
\lipsum[1-4]
\part{Part two}
\chapter{Chapter one}
\lipsum[1]
\section{Section one}
\lipsum[1-4]
\end{document}
0

1 Answer 1

6

report does not provide marks for the part level. You could use scrreprt and scrlayer-scrpage instead of report and fancyhdr:

\documentclass{scrreprt}
\usepackage[autooneside=false]{scrlayer-scrpage}
\pagestyle{headings}
\automark[chapter]{part}
\chead{}
\ihead{\leftmark}
\ohead{\rightmark}

\renewcommand*{\chapterpagestyle}{headings}% use headings page style also for chapter pages

\usepackage{mwe}
\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\part{Second Part}
\blinddocument
\end{document}

example with scrreprt and scrlayer-scrpage

If scrreprt should by more lookalike to report you can use option emulatestandardclasses:

\documentclass[emulatestandardclasses,autooneside=false]{scrreprt}
\renewcommand*{\chaptermarkformat}{\thechapter\autodot\enskip}
\pagestyle{headings}
\automark[chapter]{part}
\chead{}
\ihead{\leftmark}
\ohead{\rightmark}

\renewcommand*{\chapterpagestyle}{headings}% use headings page style also for chapter pages

\usepackage{mwe}
\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\part{Second Part}
\blinddocument
\end{document}

more like report

If you want a head separation line, you can use option headsepline. See the KOMA-Script manual for more information on the configuration of KOMA-Script classes or package scrlayer-scrpage.

Note: You should not use titlesec together with a KOMA-Script class like scrreprt. You should use the KOMA-Script features to configure the sectioning commands instead.

To get the same with report you have to patch the class:

\documentclass{report}
\usepackage[autooneside=false]{scrlayer-scrpage}
\pagestyle{headings}
\automark[chapter]{part}
\chead{}
\ihead{\leftmark}
\ohead{\rightmark}

\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{\@endpart}{%
  \partmark{#2}%
  \@endpart
}{}{}
\makeatother
\renewcommand*{\partmarkformat}{\thepart.\enskip}
\renewcommand*{\chaptermarkformat}{\thechapter.\enskip}

\xpatchcmd{\chapter}{\thispagestyle{plain}}{}{}{}% dont use pagestyle plain for chapter pages

\usepackage{mwe}
\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\part{Second Part}
\blinddocument
\end{document}

example using report and scrlayer-scrpage

Note: You cannot combine fancyhdr and scrlayer-scrpage. So if you use scrlayer-scrpage to configure the page headings like shown above, you must not load fancyhdr!

But if you absolutely want to use fancyhdr you can replace the scrlayer-scrpage code of the last example by some fancyhdr code, e.g.,

\documentclass{report}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\@part}{\@endpart}{%
  \partmark{#2}%
  \@endpart
}{}{}
\makeatother

\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand*{\partmark}[1]{\markboth{\thepart.\enskip \MakeUppercase{#1}}{}}
\renewcommand*{\chaptermark}[1]{\markright{\thechapter.\enskip \MakeUppercase{#1}}}
\renewcommand*{\sectionmark}[1]{}
\lhead{\leftmark}
\rhead{\rightmark}

\xpatchcmd{\chapter}{\thispagestyle{plain}}{}{}{}% dont use pagestyle plain for chapter pages

\usepackage{mwe}
\begin{document}
\tableofcontents
\part{First Part}
\blinddocument
\part{Second Part}
\blinddocument
\end{document}

Note: If you are using package titlesec you have to use \renewcommand to (re)define \partmark. And patching \@part may fail, if you use titlesec to redefine \part. And patching \chapter may fail, if you use titlesec to redefine \chapter.

6
  • Thanks for your reply. I got the header style using your second example, with scrreprt class, without scrlayer-scrpage. There is a slight difference with the original layout however, I'm not exactly sure what, but my introduction fitted exactly on one page and has now 1 line on the next page. Also, I get a lot of errors about \bf, but the document does compile. Lastly, I want a line underneath the header as with fancyref, and no header on the 'special' pages such as ToC and References
    – Tristan
    Commented Oct 16, 2017 at 10:04
  • @Tristan: \bf should not be used anymore. Use either \bfseries or \textbf{…} or \normalfont\bfseries. And scrreprt with option emulatestandardclasses is still not exactly the same as the standard class report. But you can change most things and use, e.g, package geometry to change the margins. For the line in the page header see my note about headsepline. Commented Oct 16, 2017 at 10:07
  • I should have clearified: I don't use the \bf , but it is encoutered in the ToC, apparently
    – Tristan
    Commented Oct 16, 2017 at 10:13
  • Then you are using a package, that uses \bf and should not. Make a bug report to the author of that package. Related: tex.stackexchange.com/questions/516/… KOMA-Script itself does not use \bf for the ToC. However, if using a KOMA-Script class is a problem in your case, see the report solutions in my answer. Commented Oct 16, 2017 at 10:15
  • Found it, there was a \bf somewhere after all. You are great. Last question: the header still appears at ToC and bibliography, how can I remove it on those pages?
    – Tristan
    Commented Oct 16, 2017 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .