Putting book part information into page headers in LaTeX
A recent question on the LaTeX subreddit prompted me to learn more about headings in two-sided LaTeX documents and how to control their content.

A once proficient user, watching from the shadows
Over the past few months, I’ve spent some time lurking on various programming-related subreddits. While watching from the shadows I’ve seen some interesting discussions play out. This reminded me of back in the 90s when I lurked on programming-related newsgroups: it was interesting to see the problems people were trying to solve as well as the discussions and solutions provided by other members of the newsgroup. The same dynamic seems to play out on Reddit. Although it appears that the half-life of a discussion is much shorter: it’s usually on the order of a day or two before a discussion thread peters out. I remember newsgroup discussions lasting a week or longer. Perhaps the internet was a bit slower back then, I dunno.
One of the subreddits that I’ve been following with interest is the LaTeX subreddit. I’ve been using LaTeX since the mid 90s. I started on LaTeX 2.09 and remember when the sysadmin upgraded our installation to LaTeX 2e. This was during my Masters degree in Physics. LaTeX of course lends itself well to any topic requiring a lot of mathematics.
Back in the day creating equations with Microsoft Word 5 (for Mac System 7, yes I’m old) was painful, to say the least. Once I’d learned LaTeX it was great to have a system in which I could write code to create the equations in my Masters thesis. And the output looked good!
Writing my thesis was simple: I split each chapter into separate files and included them in a master file which built the entire document. It was simple and worked. A friend of mine used Microsoft Word 6 on Windows for his thesis and had huge problems working with such a large document. Even though he’d globally defined the style of headings etc., the formatting was still inconsistent throughout the document (with Word resisting attempts at fixing the situation). Sometimes simply scrolling through the document would change the pagination! Crazy! He’d split up the document into separate files so that writing a chapter was even possible. It was barely possible to open the whole thesis without his computer becoming unusable or crashing. Printing the document was a minor nightmare. I remember being rather happy that I didn’t have to worry about such things! Writing a thesis isn’t much fun at the best of times; one doesn’t need any stress from one’s tools!
Over the last 10 years or so I’d not had much opportunity to use LaTeX, so you could say that I’d gotten rusty or a bit “out of touch”. Lurking in the subreddit showed me how vibrant and active the LaTeX community is with new developments coming out all the time. It’s wonderful to see!
People are creating some amazing documents. See in particular the LaTeX Showcase selection in the LaTeX subreddit for many examples. Not only are there the usual suspects writing papers and PhDs, but also Bachelors students using LaTeX to write up their notes. There are even high school students using LaTeX. Wow!
This trend seems counterintuitive. LaTeX isn’t well known for its clear error messages1. Also, the idea of writing code to create a PDF or printed document is sort of archaic in the age of word processors much more capable than those available in the mid-90s. Even so, people are coming to LaTeX for the same reasons as back then: software bloat, difficulty in building large documents, difficulty in the creation of mathematical equations, and (of course) aesthetics. LaTeX “just works”, handles maths easily and the output is beautiful.
One thing I noticed while hanging out on the LaTeX subreddit was that people usually answer very quickly, and with a completeness and quality that I’m not able to match. It’s been very instructive for me to see different perspectives and such elegant solutions.
Sometimes, however, there aren’t any (or many) answers to someone’s problem.
This post describes one such situation where I thought I could be able to
provide a nice solution and hence decided to answer one user’s plea for
help. This also gave me the opportunity to learn more about configuring
headings in two-page document layouts using the standard LaTeX book
class
as well as the KOMA-Script scrbook
class.
Book part information in headings: a special case using fancyhdr
So, here’s the problem to solve:
Trying to make Part titles show up in heading
Hello ! I’d like to preface this by saying that I am fairly new to latex, so I apologise if this is a stupid question.
I’m trying to customise my headings so that odd pages show (a) page number on the left hand side and (b) Part title on the right hand side, while the even pages show (a) page number on the right and (b) section title on the left.
right now my headings show the section title on both even and odd pages and the “code” I’m using is:
\fancyhead[RE]{\thepage} \fancyhead[LE]{\nouppercase{\leftmark}} \fancyhead[LO]{\thepage} \fancyhead[RO]{\nouppercase{\leftmark}}
How can I change this so that
\fancyhead[RO]
shows parts istead of sections? I’ve been trying to figure it out using copilot but everything it suggests ends up showing subsection titles, which is basically the opposite of what I’m trying to do.Thank you so much !
In the time since I first posted my answer, I’ve changed my solution code slightly and have updated the explanatory text to fit more with the flow of this post. What follows in this section is my updated answer.
As Martin Scharrer mentions in his answer to the StackOverflow question “How to get part name in LaTeX?” “Putting the part title into the header is not common […].”, however, it is possible. Here’s a minimum working example:
\documentclass[a4paper]{book}
\usepackage{fancyhdr}
\usepackage{lipsum}
% \parttitle solution from Martin Scharrer via StackOverflow:
% https://tex.stackexchange.com/a/13392
\newcommand*\parttitle{}
\let\origpart\part
\renewcommand*{\part}[2][]{ %
\ifx\\#1\\% optional argument not present?
\origpart{#2}%
\renewcommand*\parttitle{#2}%
\else
\origpart[#1]{#2}%
\renewcommand*\parttitle{#1}%
\fi
}
% basic fancyhdr setup adapted from the Overleaf headers and footers
% documentation: https://www.overleaf.com/learn/latex/Headers_and_footers
% set the page style to "fancy"
\pagestyle{fancy}
% define a macro for the header content
\newcommand{\partheader}{\partname\ \thepart.\ \parttitle}
% configure the "fancy" page style
\fancyhead{} % clear all header fields
\fancyhead[RE]{\thepage}
\fancyhead[LE]{\nouppercase{\leftmark}}
\fancyhead[LO]{\thepage}
\fancyhead[RO]{\partheader}
\title{Thesis}
\author{Erika Mustermann}
\begin{document}
\maketitle
\part{Background}
\chapter{Introduction}
\lipsum[1]
\section{Etiam euismod}
% need enough text so that we get to a page needing a header on a normal odd
% page (and not just a chapter's first page)
\lipsum[1-12]
\end{document}
This produces:
Note that I used Martin Scharrer’s suggestion for extracting the part
title and I’ve assumed use of the
standard LaTeX book
document class. Ulrike Fischer mentions in her
StackOverflow answer that
KOMA-Script defines a \partmark
command; something missing from the
standard LaTeX book
class. Thus, with KOMA-Script one can use \partmark
in the \rightmark
and \leftmark
definitions.
To be clear: this solution is specific to the standard book
class.
Although it does work with the KOMA-Script scrbook
class, using
fancyhdr
with scrbook
is actively discouraged.
It is also necessary to use a lot of blind text (via the lipsum
package)
so that one can see a “normal” odd page typeset with the desired header.
This is in contrast to the first page of a chapter, which, although an odd
page, doesn’t include a header, hence the need to pad things out with blind
text.
I’ve also extracted the commands defining the RO
header content into a
macro to try to make the code a bit more “self-documenting”.
I’ve checked that the document builds with both latexmk
(and hence
LuaLaTeX) and with pdflatex
. Thus the proposed solution should work in
most LaTeX installations.
More than meets the eye
The code above produces what the original poster (OP) wanted to achieve, but the situation’s not quite so clear-cut. It seems that there’s a hierarchy determining which document elements can be placed in the header on a particular page.
Also, header placement seems to be a question of typographical style. I’ve
not been able to find a definitive source, however, the KOMA-Script
documentation mentions that, for two-sided documents, the chapter is set
in the header on the left (even page) and the subordinate document element
is set in the header on the right (odd page). The fancyhdr
documentation
also implies this relationship between superior and subordinate document
elements appearing on the left and right pages respectively.
To see what I mean, let’s dig into the issue further. We’ll first see that
fancyhdr
shouldn’t be used with KOMA-Script classes. Then we’ll create a
standard header layout in KOMA-Script. Finally, we’ll try (and fail) to
achieve the same result as that implemented above using only KOMA-Script.
To begin, let’s use fancyhdr
with the KOMA-Script scrbook
class.
Using fancyhdr with KOMA-Script
It turns out that the official advice about using fancyhdr
with
KOMA-Script classes is very simple: don’t.
To see this in action replace the line
\documentclass[a4paper]{book}
in the code above with
\documentclass{scrbook}
and compile the file.
We find that the PDF output will put the chapter information on the left page and the part information on the right page, as requested by the OP.
But, a warning appears in the LaTeX log output recommending against using fancyhdr with the KOMA-Script classes:
Class scrbook Warning: Usage of package `fancyhdr' together
(scrbook) with a KOMA-Script class is not recommended.
(scrbook) I'd suggest to use
(scrbook) package `scrlayer' or `scrlayer-scrpage', because
(scrbook) they support KOMA-Script classes.
(scrbook) With `fancyhdr' several features of class `scrbook'
(scrbook) like options `headsepline', `footsepline' or command
(scrbook) `\MakeMarkcase' and the commands `\setkomafont' and
(scrbook) `\addtokomafont' for the page style elements need
(scrbook) explicite user intervention to work.
(scrbook) Nevertheless, using requested
(scrbook) package `fancyhdr' on input line 4.
A warning as obvious as this gives us a clear signal that we should not be
using fancyhdr
with KOMA-Script. So how do we configure page headers
properly in KOMA-Script?
Using the default page headers in KOMA-Script with scrlayer-scrpage
The KOMA-Script documentation recommends using the scrlayer-scrpage
package to configure page headers and footers.2 Besides
the standard header styles (empty
, plain
, headings
and myheadings
),
KOMA-Script also defines the scrheadings
and plain.scrheadings
page
styles. Thus, a minimal book example–containing title page, part, chapter,
and section document elements–styled using the default scrheadings
setting looks like this:
\documentclass{scrbook}
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}
\pagestyle{scrheadings}
\title{Thesis}
\author{Erika Mustermann}
\begin{document}
\maketitle
\part{Background}
\chapter{Introduction}
\lipsum[1]
\section{Etiam euismod}
% need enough text so that we get to a page needing a header on a normal odd
% page (and not just a chapter's first page)
\lipsum[1-12]
\end{document}
Building this document produces output with left and right page headers within a chapter as shown in the figure below.
Note that the chapter number and name appear in the top left-hand corner of
the left (even) page and that the section number and name appear in the top
right-hand corner of the right (odd) page. This is also the default
behaviour in scrbook
without using the scrlayer-scrpage
package and
without the scrheadings
page style.
This mirrors the default behaviour of the standard LaTeX book
class: the
chapter information is on the left, and the section information is on the
right. Note that the exact header layout is different though, as shown in
the following image.
Customising page headers in KOMA-Script with scrlayer-scrpage
Let’s try something different to the default settings: let’s put the part
information on the left page and the chapter information on the right page.
To do this with scrlayer-scrpage
, we can use the \automark
command:
\automark[<right-side-header-content>]{<left-side-header-content>}
Confusingly, the first argument to \automark
sets the page header content
for the right page and the last argument sets the page header content for
the left page. I’m sure there’s a good reason for organising things like
this, but naively setting the arguments based on their order within the
command will produce unexpected results. Hence I thought it best to
highlight this peculiarity before proceeding further.
Extending the default scrlayer-scrpage
example from above to use our
desired settings is very easy:
\documentclass{scrbook}
\usepackage{lipsum}
\usepackage{scrlayer-scrpage}
\pagestyle{scrheadings}
% part on LHS, chapter on RHS
\automark[chapter]{part}
\title{Thesis}
\author{Erika Mustermann}
\begin{document}
\maketitle
\part{Background}
\chapter{Introduction}
\lipsum[1]
\section{Etiam euismod}
% need enough text so that we get to a page needing a header on a normal odd
% page (and not just a chapter's first page)
\lipsum[1-12]
\end{document}
We only needed to tell scrlayer-scrpage
to put the chapter on the right
side and the part on the left by using the names chapter
and part
. This
makes the code easy to read and understand. The output is as we would
expect and looks like this:
Note that we have direct access to the part information when using
KOMA-Script. This information isn’t available in standard LaTeX. This is
why it was necessary to redefine the \part
command in my suggested
solution to the Reddit question.
Hitting a wall with the OP’s request
We now know how to configure page headers with the scrlayer-scrpage
package. Let’s try to set the page headers as requested by the OP in the
Reddit thread I mentioned at the beginning. The OP wished to show part
information on the right page and chapter information on the left.
Naively setting \automark
to
\automark[part]{chapter}
produces this output:
which sets the chapter information on the right page header but leaves the left page header empty. This is not what we intended.
Higher-level left, lower-level right
Now, you could be like me and go down a rather deep rabbit hole and try to
make this work. If so, you’ll likely find out what I did: it doesn’t work.
It’s not possible to use scrlayer-scrpage
to set the part on the right
page and the chapter on the left.
Similarly, you’ll also find that it’s not possible to set the chapter information on the right page and section information on the left.3
If a penny dropped when you read that sentence, then that’s how it was for me: “Hang on, it looks like it’s only possible to have document elements higher in the document structure on the left page and (relatively) lower (or subordinate) document elements on the right page”. And that seems to be the case: the document elements on the left and right pages reflect the hierarchical structure of the entire document. For instance, if you have the chapter information on the left page, then the right page can only contain section or subsection information. Thus, to include part information in a running header, then it should be on the left page, otherwise, the document will be typeset incorrectly (as in the image above).
It later dawned on me (after spending too much time staring at the
documentation) that the \chaptermark
and \sectionmark
commands in the
fancyhdr
package are implicitly associated with the left and right pages
respectively. Staring at the fancyhdr
documentation
even longer, I stumbled across this information:
There are two ways you can use and change the higher- and lower-level sectioning information available to you. The macros:
\leftmark
(higher-level) and\rightmark
(lower-level) contain the information processed by LaTeX, and you can use them directly as shown in section 14.
It seems that typographical convention places higher-level document elements on left-hand pages and lower-level document elements on right-hand pages. Unfortunately, I wasn’t able to find any definitive evidence to support this hypothesis (that, or my Google-foo is failing me). My guess is that this is common knowledge for experienced typographers that no one thinks about it anymore.
So what does this mean for the OP in the Reddit thread? I think it means
that what they want to achieve isn’t correct from a typographical style
perspective. Thus, although it’s possible to use fancyhdr
to create the
behaviour that they request, the fact that it’s not possible in pure
KOMA-Script, along with the naming of commands in fancyhdr
, strongly
suggests that placing chapter information on the left page and part
information on the right isn’t intended.
Conclusion
What did we learn? Well, it’s definitely possible to include part
information in page headers. The scrlayer-scrpage
and fancyhdr
packages
provide ample flexibility for the customisation of both headers and footers.
Also, configuring header placement in KOMA-Script is rather simple. The
trick is to realise that the headers on the left page have to be higher up
in the document hierarchy than the headers on the right page. Trying to
reverse this order isn’t possible in KOMA-Script and (it seems) is not
intended in fancyhdr
, although it’s possible should one wish to.
-
Perhaps I should say that it’s well known for its unclear error messages? ↩
-
Although this is possible with
fancyhdr
. Use\fancyhead[LE]{\nouppercase{\rightmark}}
for section information on the left page and\fancyhead[RO]{\nouppercase{\leftmark}}
for chapter information on the right page. ↩
Support
If you liked this post and want to see more like this, please buy me a coffee!
