[Tex/LaTex] Header should display section if no subsection available

header-footerkoma-scriptscrpage2

I'm writing a document which uses scrartcl alongside with twoside and want to create a header with scrpage2. As expected the package prints the section on even pages and the subsection on odd pages. So far so good. But when there is no subsection available on an odd page (because the section doesn't have one or it comes later in the document) then the header is left blank.

Is it possible to display the section instead if no subsection is currently available?

An example:

\documentclass[twoside]{scrartcl}

\usepackage[automark,headsepline,komastyle]{scrpage2}
\pagestyle{scrheadings}

\begin{document}

    \section{Section}

    \newpage
    \null
    \newpage

    \subsection{Sub-section}

    \newpage

    \subsection{Sub-section}

\end{document}

The header for pages 2-4 is fine and as expected but because on page 1 there is no subsection the header is left blank. I would like to print the section instead (as on page 2).

Edit: I tried to adapt the solution of this question (which is for scrreprt) to scrartcl but I still get the blank headers. I tried:

\renewcommand*\sectionmark[1]{%
  \markboth
    {\MakeMarkcase{\sectionmarkformat#1}}
    {\MakeMarkcase{\sectionmarkformat#1}}}

Edit2: It works with the above renewcommand but it has to appear after the include of the scrpage2 package. Which makes kinda sense as the sectionmark command might also be renewed in there.

Best Answer

Note that the package scrpage2 is deprecated. The successor is scrlayer-scrpage. Then you can simple use a starred version of \automark to get the desired result:

\usepackage[headsepline]{scrlayer-scrpage}
\automark[section]{section}
\automark*[subsection]{}

Example:

\documentclass[twoside]{scrartcl}

\usepackage[headsepline]{scrlayer-scrpage}
\automark[section]{section}
\automark*[subsection]{}

\usepackage{blindtext}
\begin{document}
\section{Section}
\blindtext[5]
\subsection{Sub-section}
\blindtext[5]
\subsection{Sub-section}
\blindtext[5]
\end{document}  
Related Question