9

I would like the section to be displayed in the header using \automark. However, if a chapter does not include a section I would like the chapter to be diplayed in the header.

How do I achieve that?

\documentclass[a4paper,headsepline,numbers=noenddot]{scrreprt}

\usepackage[english]{babel}
\usepackage{scrlayer-scrpage}
\usepackage{blindtext}

\renewcommand*{\chapterpagestyle}{empty}

\clearpairofpagestyles
\ihead{\headmark}
\ohead[]{\pagemark}
\automark[]{section}

\begin{document}

\chapter{Chapter 1}

\blindtext[10]

\chapter{Chapter 2}

\blindtext[12]

\section{Section 1}

\blindtext[14]

\chapter{Chapter 3}

\blindtext[10]

\end{document}
Shery
  • 449

2 Answers2

7

In a single-sided document, a \section command doesn't issue a \markright. So, add the following lines in your preamble

\renewcommand*{\sectionmark}[1]{%
    \markright{\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat}{}#1}}%
}

Also delete the line

\automark[]{section}

and load scrlayer-scrpage with the automark option.

MWE:

\documentclass[a4paper,headsepline,numbers=noenddot]{scrreprt}

\usepackage[english]{babel}
\usepackage[automark]{scrlayer-scrpage}
\usepackage{blindtext}

\renewcommand*{\chapterpagestyle}{empty}
\clearpairofpagestyles
\ihead{\headmark}
\ohead{\pagemark}

\renewcommand*{\sectionmark}[1]{%
    \markright{\MakeMarkcase{\ifnumbered{section}{\sectionmarkformat}{}#1}}%
}

\begin{document}

\chapter{Chapter 1}

\blindtext[10]

\chapter{Chapter 2}

\blindtext[12]

\section{Section 1}

\blindtext[14]

\chapter{Chapter 3}

\blindtext[10]

\end{document} 

Output

enter image description here

enter image description here

karlkoeller
  • 124,410
7

There is also a starred version of \automark.

From the scrguien.pdf:

The difference in \automark and \automark* is, that \automark deletes all prior usages of \automark or \automark*, while \automark* changes only the behaviour of the section levels of its arguments.

Using

\usepackage{scrlayer-scrpage}
\automark{chapter}
\automark*{section}
\clearpairofpagestyles
\ihead{\headmark}
\ohead{\pagemark}

the chapter is displayed in the header as long as the first section appears.

enter image description here

enter image description here

Code:

\documentclass[a4paper,headsepline,numbers=noenddot]{scrreprt}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage{scrlayer-scrpage}
\automark{chapter}
\automark*{section}
\clearpairofpagestyles
\ihead{\headmark}
\ohead{\pagemark}

\renewcommand*{\chapterpagestyle}{empty}
\begin{document}
\chapter{Chapter 1}
\blindtext[10]
\chapter{Chapter 2}
\blindtext[12]
\section{Section 1}
\blindtext[14]
\chapter{Chapter 3}
\blindtext[10]
\end{document}
esdd
  • 85,675