Menu

HowTo_HeaderSeparateNumberText

How to display the chapter number on the outside and the chapter title on the inside of the page header

Most classes use \chaptermark to set the marks for the living column title, i.e. usually the page header. With scrreprt, the default definition is in the page style headings:

\renewcommand*{\chaptermark}[1]{%
  \if@twoside\expandafter\markboth\else\expandafter\markright\fi
  {%
    \Ifnumbered{chapter}{%
      \MakeMarkcase{\chaptermarkformat}%
    }{}\MakeMarkcase{#1}%
  }{}%
}%

With scrbook, there is additionally a test for the main matter (\mainmatter). When using scrlayer-scrpage, the definition looks a little more complicated, but also boils down to the fact that the number (defined in \chaptermarkformat) is output before the title text (in #1).

If you wanted to set the number left-aligned and the title right-aligned, you could simply start at \chaptermarkformat and in its definition:

\newcommand*\chaptermarkformat{\chapappifchapterprefix{\ }%
  \thechapter\autodot\enskip}

and replace the distance of half a square \enskip with an infinitely stretchable distance \hspace{.5em plus 1fill}:

\renewcommand*\chaptermarkformat{\chapappifchapterprefix{\ }%
  \thechapter\autodot\hspace{.5em plus 1fill}}

However, we want the position of the number and text to be swapped on right-hand (=odd) pages of a double-sided document. We therefore need a new instruction that does exactly that:

\documentclass[headsepline]{scrbook}
\usepackage[english]{babel}
\usepackage{blindtext}
\DeclareRobustCommand*{\FlipNumberText}[2]{%
  \ifodd\value{page}% This only works in the head or foot, otherwise \ifthispageodd is needed instead!!!
    #2\hfill #1%
  \else
    #1\hfill #2%
  \fi
}
\renewcommand*{\chaptermark}[1]{%
  \csname @mkboth\endcsname
  {\MakeMarkcase{\FlipNumberText{\chaptermarkformat}{#1}}}
  {\MakeMarkcase{\FlipNumberText{\chaptermarkformat}{#1}}}
}
\renewcommand*{\sectionmark}[1]{}% \section should not change any mark
\begin{document}
\blinddocument 
\end{document}

It is important to use \DeclareRobustCommand (or alternatively \NewDocumentCommand) so that the command does not expand too early. If \newcommand is used, the test as to whether the page is even or odd would take place when the marker is set within \chapter and not only when it is used.

Of course, this also works when using scrlayer-scrpage:

\documentclass[headsepline]{scrbook}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{scrlayer-scrpage}
\automark[chapter]{chapter}
\NewDocumentCommand{\FlipNumberText}{m+m}{%
  \ifodd\value{page}% This only works in the head or foot, otherwise \ifthispageodd is needed instead!!!
    #2\hfill #1%
  \else
    #1\hfill #2%
  \fi
}
\renewcommand*{\chaptermark}[1]{%
  \csname @mkboth\endcsname
  {\MakeMarkcase{\FlipNumberText{\chaptermarkformat}{#1}}}
  {\MakeMarkcase{\FlipNumberText{\chaptermarkformat}{#1}}}
}
\begin{document}
\blinddocument 
\end{document}

It also works with scrlayer-scrpage and a standard class, but then you have to move the page number from the header to the footer, otherwise it will overlap with either the number or the text of the chapter marker:

\documentclass[headsepline]{book}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{scrlayer-scrpage}
\automark[chapter]{chapter}
\clearpairofpagestyles
\ohead{\headmark}
\ofoot*{\pagemark}
\NewDocumentCommand{\FlipNumberText}{m+m}{%
  \ifodd\value{page}% This only works in the head or foot, otherwise \ifthispageodd is needed instead!!!
    #2\hfill #1%
  \else
    #1\hfill #2%
  \fi
}
\renewcommand*{\chaptermark}[1]{%
  \csname @mkboth\endcsname
  {\MakeMarkcase{\FlipNumberText{\chaptermarkformat}{#1}}}
  {\MakeMarkcase{\FlipNumberText{\chaptermarkformat}{#1}}}
}
\begin{document}
\blinddocument 
\end{document}

By the way, I only used the headsepline option in the examples to make the header area better distinguishable. What the \MakeMarkcase is all about can be found in the KOMA-Script user manuals.


Related

Wiki (English): HowTo_PageHeadFoot

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.