1

I am writing a document and I have made it, so that on every even page, the header shows the current chapter, and on every odd page it shows the current section. But how can I show either the current chapter or the current section in the header, depending on the fact that the current page already belongs to a section or not?

\documentclass[a4paper, 12pt, twoside]{scrreprt}                            
\usepackage{geometry}                           
\usepackage[utf8]{inputenc}             
\usepackage[ngerman]{babel}              
\usepackage[T1]{fontenc}        
\usepackage{blindtext}  
\usepackage[headsepline]{scrlayer-scrpage}
\pagestyle{scrheadings}
\clearpairofpagestyles                          
\rohead{\chaptername :\ \rightmark}     
\lehead{\chaptername :\ \leftmark}      
\automark[section]{chapter}                                         
\ofoot[\pagemark]{\pagemark}

\begin{document}
\chapter{1}
    \blindtext
    \chapter{2}
    \section{2.1}
    \blindtext
    \chapter{3}
    \blindtext
    \section{3.1}
    \blindtext
\end{document}
Johannes_B
  • 24,235
  • 10
  • 93
  • 248
Emerald
  • 11

3 Answers3

2

You can use the additive star version \automark*:

\documentclass[a4paper, 12pt, twoside]{scrreprt}                            
\usepackage{geometry}                           
\usepackage[utf8]{inputenc}             
\usepackage[ngerman]{babel}              
\usepackage[T1]{fontenc}        
\usepackage{blindtext}  
\usepackage[headsepline]{scrlayer-scrpage}
\automark[chapter]{chapter}
\automark*[section]{}% Add odd side marks without removing previous marks
\renewcommand*{\chaptermarkformat}{\chapapp~\thechapter:\enskip}% Put "chapter" oder "appendix" in front of the chapter number in running head
\clearpairofpagestyles
\ohead{\headmark}                         
\ofoot*{\pagemark}

\begin{document}
\chapter{Erstes Kapitel}
    \Blindtext\Blindtext
\chapter{Zweites Kapitel}
\section{Erster Abschnitt des zweiten Kapitels}
    \Blindtext\Blindtext
\chapter{Drittes Kapitel}
    \Blindtext\Blindtext
\section{Erster Abschnitt des dritten Kapitels}
    \Blindtext\Blindtext
\end{document}

The first \automark[chapter]{chapter} activates automatic running heads and defines a \chaptermark to set left and right mark and resets \sectionmark etc. The second \automark*[section]{} does only define \sectionmark to set the right mark (odd syntax: left argument → right mark; right argument → left mark). So you have chapter on the left and right pages until you have a \section that changes the running head of right pages to be the section.

Schweinebacke
  • 26,336
  • Sorry, I'm away and so couldn't react to your comment directly. You are naturally right that the starred version of \automark make is easier to reset only \sectionmark. – Ulrike Fischer Jan 15 '17 at 16:56
1

You should redefine \sectionmark so that it also sets the left mark. In KOMA-syntax e.g. like this (a simple \renewcommand\sectionmark[1]{\markboth{#1}{#1}} should work too, but looses some of the KOMA internals):

\documentclass[a4paper, 12pt, twoside]{scrreprt}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{blindtext}
\usepackage[headsepline]{scrlayer-scrpage}
\pagestyle{scrheadings}
\clearpairofpagestyles
\renewcommand\chaptermarkformat{Kapitel~}
\rohead{\leftmark}
\lehead{\leftmark}
\automark{chapter}
\automark*{section}

\ofoot[\pagemark]{\pagemark}

\begin{document}
\chapter{Erstes Kapitel}
    \Blindtext\Blindtext
\chapter{Zweites Kapitel}
\section{Erster Abschnitt des zweiten Kapitels}
    \Blindtext\Blindtext
\chapter{Drittes Kapitel}
    \Blindtext\Blindtext
\section{Erster Abschnitt des dritten Kapitels}
    \Blindtext\Blindtext
\end{document}
Ulrike Fischer
  • 327,261
0

You can check whether \rightmark expands to something printable (using this answer).

\rohead{\setbox0=\hbox{\rightmark\unskip}\ifdim\wd0=0pt\chaptername\ \leftmark\else Abschnitt \rightmark\fi}
\lehead{\chaptername\ \leftmark}      

enter image description here enter image description here enter image description here

\documentclass[a4paper, 12pt, twoside]{scrreprt}                            
\usepackage{geometry}                           
\usepackage[utf8]{inputenc}             
\usepackage[ngerman]{babel}              
\usepackage[T1]{fontenc}        
\usepackage{blindtext}  
\usepackage[headsepline]{scrlayer-scrpage}
\pagestyle{scrheadings}
\clearpairofpagestyles                          
\rohead{\setbox0=\hbox{\rightmark\unskip}\ifdim\wd0=0pt\chaptername\ \leftmark\else Abschnitt \rightmark\fi}
\lehead{\chaptername\ \leftmark}      
\automark[section]{chapter}                                         
\ofoot[\pagemark]{\pagemark}

\begin{document}
\chapter{Erstes Kapitel}
    \Blindtext\Blindtext
\chapter{Zweites Kapitel}
\section{Erster Abschnitt des zweiten Kapitels}
    \Blindtext\Blindtext
\chapter{Drittes Kapitel}
    \Blindtext\Blindtext
\section{Erster Abschnitt des dritten Kapitels}
    \Blindtext\Blindtext
\end{document}
gernot
  • 49,614
  • This is not reliable -- the section number can change on the previous page. See e.g. \chapter{Erstes Kapitel} blub \newpage blub \newpage blub \par \vspace{0.8\textheight}\par blub \\blub \section{Erster Abschnitt des ersten Kapitels}. – Ulrike Fischer Jan 13 '17 at 16:09
  • @UlrikeFischer Right, thanks; I have modified (corrected?) my answer. – gernot Jan 13 '17 at 16:25