2

I am writing my thesis in the oneside scrbook class and would like the section number and name to show up on the top left of all pages (except pages on which a new chapter starts). I was facing the problem that if a new section starts halfway in the page, then the new section number and title would show up, whereas I wanted to show the section number and title that is on top of the page. The following code that I found on the internet almost fixed this:

\documentclass[oneside]{scrbook}
\usepackage{lipsum}
\usepackage{xcolor}
\RequirePackage[headsepline=true,footsepline=false]{scrlayer-scrpage}

\makeatletter
\def\leftmark{\expandafter\@leftmark\topmark\@empty\@empty}
\makeatother

\renewcommand{\sectionmark}[1]{\markleft{\textcolor{black}{\thesection}\ ~ #1}}
\clearpairofpagestyles % clear default page style
\lohead{~\leftmark} % section name and number on top left

\begin{document}
\chapter{Introduction}
\section{First section}
\lipsum[1-8]
\section{Second section}
\lipsum[1-1]
\section{Third section}
\lipsum[1-2]
\end{document}

However, in the situation that a new section starts on the top of a page, the header still shows the previous section's number and title. What would be the easiest way to adjust the code above to keep the same functionality, but ensure that when a new section starts at the top of a page, the header shows that section's number and title?

1

1 Answer 1

1

You could use \leftfirstmark and remove your redefinition of \leftmark. However, with the shown example, I just would use \automark[section]{section}:

\documentclass[oneside]{scrbook}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[headsepline=true,footsepline=false]{scrlayer-scrpage}% You should use \usepackage to load packages in documents.
\automark[section]{section}
\clearpairofpagestyles % clear default page style
\ihead{\headmark} % head mark as defined using \automark in the page head    
%\ofoot*{\pagemark}% Maybe adding pagination would be a good idea?

\begin{document}
\chapter{Introduction}
\section{First section}
\lipsum[1-8]
\section{Second section}
\lipsum[1-1]
\section{Third section}
\lipsum[1-2]
\end{document}

page 2 and 3 with the expected header

There are other commands, described in the KOMA-Script manual, like: \lefttopmark, \leftfirstmark, \leftbotmark. If you, e.g., would use \lefttopmark:

\documentclass[oneside]{scrbook}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[headsepline=true,footsepline=false]{scrlayer-scrpage}% You should use \usepackage to load packages in documents.
\automark[section]{section}
\clearpairofpagestyles % clear default page style
\ihead{\lefttopmark} % See the KOMA-Script manual for the meaning of \lefttopmark.    
%\ofoot*{\pagemark}% Maybe adding pagination would be a good idea?

\begin{document}
\chapter{Introduction}
\section{First section}
\lipsum[1-8]
\section{Second section}
\lipsum[1-1]
\section{Third section}
\lipsum[1-2]
\end{document}

you'd get:

using \lefttopmark

So there are no marks, that result in: Getting the last mark of the previous page unless the section would start at the very beginning. So you would extra code to handle such cases, e.g.:

\documentclass[oneside]{scrbook}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[headsepline=true,footsepline=false]{scrlayer-scrpage}% You should use \usepackage to load packages in documents.
\automark[section]{section}
\clearpairofpagestyles % clear default page style
\ihead{\lefttopmark} % See the KOMA-Script manual for the meaning of \lefttopmark.    
% \ofoot*{\pagemark}% Maybe adding pagination would be a good idea?
\newpairofpagestyles{sectionpagestyle}{\ihead{\leftfirstmark}}

\makeatletter
\AddtoDoHook{heading/postinit/section}{%
  \ifdim \dimexpr\pagegoal-\pagetotal\relax<3\baselineskip
    \pagebreak[4]%
    \thispagestyle{sectionpagestyle}%
    \typeout{Do it!}%
  \else
    \typeout{\the\pagegoal, \the\pagetotal, \the\baselineskip}%
  \fi
  \@gobble
}
\makeatother


\begin{document}
\chapter{Introduction}
\section{First section}
\lipsum[1-8]
\section{Second section}
\lipsum[1-1]
\section{Third section}
\lipsum[1-6]
\section{Fourth section}
\lipsum[1]

\end{document}

with extra code to detect, that the section will be at the page start

But note: Such code can fail, e.g., if a section head would be followed by a large not breakable box.

BTW: If you want extra space between section number and section title in the heading, don't redefine \sectionmark but \sectionmarkformat, e.g.

\renewcommand*{\sectionmarkformat}{\thesection\autodot\hspace{1.5em}}

See the KOMA-Script manual for more information about \automark, \leftfirstmark, \headmark and \sectionmarkformat. And for more information about page styles with KOMA-Script see, e.g., “How change the head and foot of the page” in the KOMA-Script Wiki also available in German.

Off-topic: In the example I would suggest to use scrreprt (without option) instead of scrbook with option oneside, but maybe there is a not shown reason for using scrbook instead of scrreprt.

1
  • In this solution, the third page correctly shows 1.3 Third Section, because that section starts on top of the page. However, the second page (your page on the left) shows '1.2 second section' in the header, whereas that section only starts halfway in the page. How could I change the code so that the second page shows '1.1 First section'? Because that is the section that is on the top of that page.
    – Sybe
    Commented Apr 3, 2023 at 9:15

You must log in to answer this question.

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