# Include chapter, section and subsection in running head

I’m using scrlayer-scrpage to get chapter and section in my running head:

\documentclass{scrreprt}

\clearpairofpagestyles

% remove section numbers before section text
\renewcommand*{\sectionmarkformat}{}

\usepackage{lipsum}
\begin{document}
\chapter{Cht}
\section{Sct}
\lipsum
\subsection{Subsection}
\lipsum
\section{SScctt}
\lipsum
\chapter{Next}
\lipsum
\end{document}


Resulting in:

1 Cht — Sct

Yet, is there a way to include the subsection? So that it looks like this:

1 Cht — Sct — Subsection

And is there a way to check if there is a curren Section/Subsection in the current chapter, since I’ve seen cases that the section refers to the one in a previous chapter...

Maybe the following does what you want:

\documentclass{scrreprt}
\automark[subsection]{section}
\renewcommand*{\sectionmarkformat}{}
\renewcommand*{\subsectionmarkformat}{}

\newmarks\chaptermarkwithnum
\makeatletter
\renewcommand\chaptermark[1]{%
\begingroup
\let\label\relax \let\index\relax \let\glossary\relax
\@temptokena{\ifnumbered{chapter}{\chaptermarkformat}{}#1}%
\unrestored@protected@xdef\@themark{\the\@temptokena}%
\@temptokena\expandafter\expandafter{\expandafter\MakeMarkcase\expandafter{\@themark}}%
\marks\chaptermarkwithnum{\the\@temptokena}%
\endgroup
%
\markboth{}{}%
}
\makeatother

\clearpairofpagestyles
\firstmarks\chaptermarkwithnum
\ifstr{\leftbotmark}{}{}{
--- \leftbotmark\ifstr{\rightbotmark}{\leftbotmark}{}{ --- \rightbotmark}%
}%
}

\usepackage{lipsum}
\begin{document}
\chapter{Cht}
\section{Sct}
\lipsum
\subsection{Subsection}
\lipsum
\section{SScctt}
\lipsum
\chapter{Next}
\lipsum
\end{document}


The mark mechanism is used for the running header entries. A mark command sets three variables: one for the last mark of the previous page, one for the first mark set on the current page and one for the last mark set on the current page. When a page starts the three variables are equal.

\markright sets its argument as the "right" mark. \markboth sets its first argument as the "left" mark and its second argument as the "right" mark.

\rightmark uses the first "right" mark set by \markright or the second argument of \markboth on a page. \leftmark uses the last (bottom) "left" mark set by the first argument of \markboth. The KOMA-Script class provides also \righttopmark, \rightfirstmark, \rightbotmark, \lefttopmark etc.

If chapter, section and subsection should be in the running header an three marks are needed.

With \automark[subsection]{section} each \section command executes \markboth with the section in its first and its second argument and each \subsection executes \markright with the subsection in its argument. So \leftbotmark is the last section on the page and \rightbotmark is either the last section or the last subsection on the page.

To add the chapter in a third mark an additional mark must be defined. In the example it is \chaptermarkwithnum. Then \chaptermark is redefined to set this new mark. Therefore an adaption of LaTeX's \markboth definition is used (thanks to @Schweinebacke). Additionally \markboth{}{} cleans the "right" and the "left" mark. So in a new chapter \leftbotmark and \rightbotmark are empty until the next \section command.

For the header entry in \ihead the first value of the new mark and the last values of the left and the right mark are used.

• Very sophisticated. I don't even understand \newmarks and \marks and by the way, I've got no idea what you are doing there. If the wonderful weather doesn't distract you: could you include some words of explanation, please? Sep 27 '18 at 12:09
• I second @KeksDose. Very well done, and it works conditionally! But some explanation would be really cool to understand the code better! Thank you!! Sep 27 '18 at 14:09
• @esdd I just noticed that the answer you provided sets the entire body text in italic... cannot figure out why that is... Sep 27 '18 at 16:12
• @esdd I've adapted your definition of \chaptermark. Now, it even works if \MakeMarkcase is, e.g., \MakeUppercase. The \usekomafonts are not needed because they are automatically part of the head definition not the marks. Sep 27 '18 at 17:06
• @Kalaschnik \newmarks and \firstmarks are e-TeX extensions to TeX's original \mark/\firstmark. They allow to use additional mark registers (originally TeX had only one). The rest of the code (the part I've added to esdd's original code) is an adaption of LaTeX's \markboth definition for that new mark. Sep 28 '18 at 6:58