# How to set the section "range" onto the head? scrpage2, scrbook, titlesec

I want to get the "range" of the sections of one page into the headline. I use scrpage2 scrlayer-scrpage for changing the headline and scrbook as documentclass. I try to write a MWE:

\documentclass[a4paper,12pt,twoside]{scrbook} %KOMA-version: 3.12 (overleaf)
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[forceoverwrite]{scrlayer-scrpage}

%some stuff here

\begin{document}

\chapter{Sample chapter}
\section{Sample section}
\lipsum[1-2]
\section{section 2}
\lipsum[3-4]
\section{section 3}
\lipsum[5-6]
\section{section 4}
\lipsum[7-8]
\section{section 5}
\lipsum[1-2]

\end{document}


So if for example sections 1 to 3 are on one page then on that page should the "range" be 1-3 and so on (next page 3-5), similar to bibles.

I tried creating a command: (that was under scrpage2, so it's outdated)

\newcounter{sectionone}
\newcounter{sectiontwo}
\newcounter{recentpage}
\ifnum\recentpage=\thepage%
\setcounter{sectionone}{\value{section}}%
\else%
\setcounter{sectiontwo}{\value{section}}%
\setcounter{recentpage}{\value{page}}%
\fi}


\lehead{\thesectionone--\thesectiontwo}


but it didn't work as i wanted. I am not new to LaTeX, but new to "that" programming.

Is there a better solution? Maybe a package or so? Would be great to find one...

Thank you

It is also OK if there is just the number of the first section (on the left side) and the number of the last section (on the right side).

The successor of scrpage2 is scrlayer-scrpage. With this package and an uptodate KOMA-Script version you can use something like

\documentclass[12pt]{scrbook}
\renewcommand\thesection{\arabic{section}}

\usepackage[manualmark]{scrlayer-scrpage}
\renewcommand\chaptermark[1]{\markboth{}{}}
\renewcommand\sectionmark[1]{\markright{\thesection}}
\clearpairofpagestyles

\ifstr{\rightfirstmark}{}{\ifstr{\rightbotmark}{}{}{%
\ifstr{\rightbotmark}{1}{1}{1\enskip--\enskip\rightbotmark}}}%
{\ifstr{\rightbotmark}{1}{1}{%
\ifstr{\righttopmark}{\rightbotmark}{\rightbotmark}{%
\righttopmark\enskip--\enskip\rightbotmark}}}
}

\ofoot*{\pagemark}

\usepackage{blindtext}% dummy text
\begin{document}
\chapter{Sample chapter}
\section{Sample section}
\blindtext
\section{section 2}
\Blindtext
\section{section 3}
\Blindtext
\section{section 4}
\section{section 5}
\end{document}


Result:

But not there is a problem if a section starts on top of a new page:

To avoid this you could change \ohead* to

\ohead*{%
\ifstr{\rightfirstmark}{}{\ifstr{\rightbotmark}{}{}{1}}{\rightfirstmark}%
\ifstr{\rightfirstmark}{\rightbotmark}{}{\ifstr{\rightbotmark}{1}{}{%
\enspace--\enspace\rightbotmark}}
}


Result:

But then you have only sections in the header that start on the current page (see page 2 in the MWE).

I don't know a possibility to get 2-3 in the header of page 2 and 4-5 in the header of page 4 in this MWE.

If you are using an older KOMA-Script version than 3.16 add the following lines before setting \ohead:

\makeatletter
\providecommand*{\rightfirstmark}{\expandafter\@rightmark\firstmark\@empty\@empty}
\providecommand*{\rightbotmark}{\expandafter\@rightmark\botmark\@empty\@empty}
\providecommand*{\righttopmark}{\expandafter\@rightmark\topmark\@empty\@empty}
\makeatother


Additionally you can not use the starred versions of \ohead etc. with a KOMA-Script version before 3.14. So you have to remove the stars and to copy the code of the mandantory argument in the optional argument of \ohead etc.: \ohead[<code>]{<code>]. Alternatively you can use \ohead{<code>} and change the page style of the chapter pages to scrheadings using

\renewcommand*\chapterpagestyle{scrheadings}

• But that's a great way. Just one problem: It doesn't work here. I always get the error, that it cannot define \newpagestyle. I don't know why and I have no idea where I can search... Aug 22 '15 at 14:04
• titlesec also defines \newpagestyle. You could use \usepackage[manualmark,forceoverwrite]{scrlayer-scrpage} but note that it is not recommanded to use titlesec together with a KOMA-Script class.
– esdd
Aug 22 '15 at 15:49
• sadly I have to say that overleaf doesn't have KOMA 3.16. It just has 3.12. Is there a way to solve the problem in version 3.12? Aug 23 '15 at 15:02
• @sirjofri I have added remarks for older KOMA-Script versions.
– esdd
Aug 23 '15 at 19:30
• That's great. I will try it out tomorrow. Here it is midnight... Aug 23 '15 at 22:19