# section and subsection in headline

I am currently working on my bachelor thesis. I use the package srclayer-scrpage for headlines. As I don't need to print my thesis, it is a one-sided document. I want the current section or subsection name on the right side. I can use either \automark{subsection} or \automark{section} but in case of \automark{subsection}, a section with no subsections will be marked with the latest subsection and in case of \automark{section} no subsection will be marked. Have you any ideas to combine section and subsections in the headline?

Here is a small example of my code:

\documentclass[11pt, a4paper, german]{scrartcl}

\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}
\cfoot{\pagemark}
\automark{subsection}

\begin{document}
\section{first section}
This is a section with subsections, so using $\backslash$automark\{subsection\} will not cause any problems.
\subsection{first subsection}first subsection
\newpage \subsection{second subsection} second subsection
\newpage \section{second section} This is a section with no subsections, so in the headline will appear \glqq second subsection\grqq \ instead of \glqq second section\grqq. How can I fix this?
\end{document}


Maybe you want to use

\automark{section}
\automark*{subsection}


Example:

\documentclass[
%11pt, a4paper,% default
ngerman
]{scrartcl}
\usepackage{babel}
%\usepackage[utf8]{inputenc}% needed for older TeX distributions
\usepackage{amsmath}
\usepackage{amsthm}
\automark{section}
\automark*{subsection}
This is a section with subsections, so using $\backslash$automark\{subsection\} will not cause any problems.