1

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

\documentclass{scrreprt}

\usepackage[headsepline,automark,autooneside=false]{scrlayer-scrpage}
\clearpairofpagestyles
\ihead{\leftmark\ifstr{\rightmark}{\leftmark}{}{ --- \rightmark}}

% 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...

3

Maybe the following does what you want:

\documentclass{scrreprt}
\usepackage[headsepline,autooneside=false]{scrlayer-scrpage}
\automark[subsection]{section}
\renewcommand*{\sectionmarkformat}{}
\renewcommand*{\subsectionmarkformat}{}

\newmarks\chaptermarkwithnum
\makeatletter
\renewcommand\chaptermark[1]{%
  % code changed/added by @Schweinebacke:
  \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
\ihead{%
  \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}

enter image description here

enter image description here


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? – Keks Dose 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!! – Kalaschnik 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... – Kalaschnik Sep 27 '18 at 16:12
  • 1
    @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. – Schweinebacke Sep 27 '18 at 17:06
  • 1
    @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. – Schweinebacke Sep 28 '18 at 6:58

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.