3

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.

  • 2
    titleps might work better here than fancyhdr. It is by the author of titlesec and is designed to play nicely with it. It has a lot more facilities for managing marks. – cfr Jul 7 '15 at 23:12
4

If you switch to titleps, you can use your current code and it seems to work. Better would probably be to use the package's extended support for additional sets of marks, avoiding the need for a workaround.

Or, since you are using titlesec, you could probably just set the sectioning command up to avoid the need for special measures. However, without seeing how you are using this already, it is difficult to say for sure.

\documentclass[a4paper, 11pt, openany]{book}
\usepackage[english]{babel}
\usepackage[pagestyles]{titlesec}
\usepackage{lipsum}% just for the example

\newcommand{\markedsection}[2]{\section[#2]{#2% See the linked answer
\sectionmark{#1}}
\sectionmark{#1}}

\newpagestyle{myfancy}[\normalfont\normalsize\scshape]{%
  \setheadrule{.4pt}%
  \sethead[\toptitlemarks\chaptertitle][][]{}{}{\toptitlemarks\thesection\space\sectiontitle}%
  \setfoot*{}{}{\thepage}%
}
\pagestyle{myfancy}

\author{Author}
\title{Test book}

\begin{document}
\maketitle
\tableofcontents

\chapter[Test chapter]{Test chapter}
\section{First section}
\lipsum[4-11]

\markedsection{Short section title}{Long section title}

\lipsum[1-6]
\end{document}

top marks

  • I can't believe none of the answers I looked at mentioned this package. It would have saved me so much time. This seems to work in my 300+ page book without breaking anything. Thank you! – Pétur Jul 7 '15 at 23:51

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.