I'm using fancyhdr
to display the current section title in the header of even-numbered pages of a book
. I achieve this with the following code:
\setlength{\headheight}{14pt}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyhead[EL]{\leftmark}
\fancyhead[OR]{\scshape\nouppercase\rightmark}
\renewcommand{\chaptermark}[1]{%
\markboth{\scshape #1}{}}
Some section titles are long and I have to display a shorter title in the headers (but not in the table of contents). Usually the following works:
\section[Long section title]{Long section title}
\marksection{Short section title}
However, when an odd-numbered page starts with a new section, this results in the long section name
on that first page. In this answer, the problem is solved with this method:
\section[Long section title]{Long section title%
\sectionmark{Short section title}}
\sectionmark{Short section title}
(see also the macro provided in the answer).
But this doesn't work for me either. I noticed they used the scrartcl
document class while I'm using book
, which may be the reason.
Here is my MWE (hopefully sufficiently minimal):
\documentclass[a4paper, 11pt, openany]{book}
\usepackage[english]{babel}
\usepackage{titlesec}
\usepackage{fancyhdr}
\usepackage{lipsum}% just for the example
\setlength{\headheight}{14pt}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[LE,RO]{\thepage}
\fancyhead[EL]{\leftmark}
\fancyhead[OR]{\scshape\nouppercase\rightmark}
\renewcommand{\chaptermark}[1]{%
\markboth{\scshape #1}{}}
\newcommand{\markedsection}[2]{\section[#2]{#2% See the linked answer
\sectionmark{#1}}
\sectionmark{#1}}
\author{Author}
\title{Test book}
\begin{document}
\maketitle
\tableofcontents
\chapter[Test chapter]{Test chapter}
\section{First section}
\lipsum[4-11]
% Method 1
%\sectionmark{Short section title}
%\section[Long section title]{Long section title}
%\sectionmark{Short section title}
% Method 2
\markedsection{Short section title}{Long section title}
\lipsum[1-6]
\end{document}
(I can provide a .pdf if it helps). Some answers suggest using the KOMA-script book class, but this is not an option because the book is almost finished and it's rather too late to switch classes.
titleps
might work better here thanfancyhdr
. It is by the author oftitlesec
and is designed to play nicely with it. It has a lot more facilities for managing marks.