[Tex/LaTex] Set header using scrlayer-scrpage

scrlayer-scrpage

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}

Best Answer

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}