1

I want to insert a header in the book class such that:

  • Odd pages show only the book title on the left header:
  • Even pages show either the section name, or the subsection (if this exists) on the right header

MWE:

\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{hyperref}
\usepackage{lipsum}

\fancyhf{}
\fancyhead[LO]{Book title}\fancyfoot[CO]{\thepage}
\fancyhead[RE]{\nouppercase{\rightmark}}\fancyfoot[CE]{\thepage} %this should be defined in some other way
\pagestyle{fancy}

\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}

However, even pages do not show the section name, while they show the subsection name when that is already "initialized". How can I define a command that changes the section name into the subsection name of the header?

3
  • @Bobyandbob, not exactly. In the linked case, every even page has the subsection name as header. In my case, the header has to be the section name, except when the subsection is initialized, in which case I want the header to be the subsection name.
    – Py-ser
    Mar 2, 2017 at 16:22
  • Please provide a compilable document, not just fragments
    – user31729
    Mar 2, 2017 at 17:15
  • Where is \markedsection from?
    – user31729
    Mar 2, 2017 at 23:29

1 Answer 1

2

For the book class there is no real \subsectionmark command -- it uses the \let\subsectionmark\@gobble statement from latex.ltx.

Within all section levels commands defined with \@startsection and finally using \@sect, there is the call \csname #1mark\endcsname{#7}, which will set the mark if #1mark, i.e. if \subsectionmark would be really usefully defined, but it is used as \@gobble, i.e. throwing away the argument for book.cls.

So one possible solution is to define \subsectionmark, I used the definition of \sectionmark within \ps@headings style from book.cls and changed it slightly.

Since all \....mark commands use \markboth which is setting internal variables, the last call to \markboth will set the real \@leftmark and \@rightmark settings, so the last call of a mark on the page is the winner!

\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage[headheight=15pt]{geometry}
\usepackage{lipsum}
\usepackage{hyperref}

\makeatletter
\def\subsectionmark#1{%
  \markright {\MakeUppercase{%
      \ifnum \c@secnumdepth >\z@
      \thesubsection. \ %
      \fi
      #1}}}
\makeatother

\fancyhf{}
\fancyhead[LO]{Book title}\fancyfoot[CO]{\thepage}
\fancyhead[RE]{\nouppercase{\rightmark}}\fancyfoot[CE]{\thepage} %this should be defined in some other way
\pagestyle{plain}

\author{Author}
\title{Test book}

\begin{document}
\maketitle
\tableofcontents
\clearpage
\pagestyle{fancy}

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

\subsection{Foo subsection}
%\markedsection{Short section title}{Long section title}

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

enter image description here

enter image description here

9
  • Thanks a lot for the answer, I was able to grab the logic even if I am not an expert! However, I still have the same problem as before: even pages do NOT show the section name on the header (that is, an empty header), while they show the subsection name (when this is declared).
    – Py-ser
    Mar 3, 2017 at 9:57
  • @Py-ser: I checked and the page 6 has the section title as you requested, here on the right side of the page, since you specified RE
    – user31729
    Mar 3, 2017 at 10:03
  • @ChristianHupfer I vote always Christian. Look look :-). The votes are gratis.
    – Sebastiano
    Mar 3, 2017 at 10:13
  • 1
    @Sebastiano: As are mine...
    – user31729
    Mar 3, 2017 at 10:28
  • @ChristianHupfer, you are right. However, it seems that if I \input{chapterfile.tex} the section name is not shown. Is it because, in that way, I do not explicitely initialize the section? does this make sense?
    – Py-ser
    Mar 3, 2017 at 10:56

You must log in to answer this question.

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