# keep rightmark clear until section starts

I'd like to have the chapter title in the upper left and the section title in the upper right corner of my one-sided document. The upper right corner should only be populated if there actually is a section on that side. My problem can be reproduced with this MWE:

\documentclass{scrreprt}

\usepackage{lipsum}

\usepackage{scrpage2}
\automark[section]{chapter}

\begin{document}
\chapter{chapter}
\lipsum \lipsum

\section{section}
\lipsum

\end{document}


The \ohead{\rightmark} is somewhat by design because I don't want the section title to appear on the first page of a new chapter.

The real problem occurs on page 2, where the header reads "1. chapter .... 1. chapter". How can I get an blank upper right corner?

Sorry for the probably bad title of the question, but if I could make it clearer, google would probably help to solve the problem...

• Hi and welcome, good example, i'll have a look at this. But please note, that scrpage2 development has stopped. It's current status is deprecated. The successor is scrlayer-scrpage. Nov 2 '14 at 17:59

Note that scrpage2 is depreciated. The successor is scrlayer-scrpage.

You can use \ifstr from the scrbase package:

\ohead{\ifstr{\leftmark}{\rightmark}{}{\rightmark}}


The package scrbase is loaded by the KOMA-Script class and the command is explained in the KOMA-Script documentation scrguien.pdf:

\ifstr{<string>}{<string>}{<then instructions>}{<else instructions>}

Both <string> arguments are expanded and afterwards compared. If the expansions are the same, the <then instructions> will be executed, otherwise the <else instructions>.

This will also work in the table of contents or with \addchap and \addsec.

\documentclass{scrreprt}
\usepackage{blindtext}

\usepackage[autooneside=false]{scrlayer-scrpage}
\automark[section]{chapter}
\clearpairofpagestyles

\begin{document}
\tableofcontents

\chapter{chapter}
\Blindtext[10]
\section{section}
\Blindtext

\Blindtext[10]
\Blindtext

\Blinddocument\Blinddocument\Blinddocument
\Blinddocument\Blinddocument\Blinddocument
\end{document}


Note that there is a star version of \ihead in the new KOMA-Script version 3.14 which allows to use \ihead*{\leftmark} instead of \ihead[\leftmark]{\leftmark}. Unfortunaly, this version is not yet on CTAN.

The suggestion with the depriacted scrpage2:

\documentclass{scrreprt}
\usepackage{blindtext}

\usepackage{scrpage2}
\automark[section]{chapter}

\begin{document}
\tableofcontents

\chapter{chapter}
\Blindtext[10]
\section{section}
\Blindtext

\Blindtext[10]
\Blindtext

\Blinddocument\Blinddocument\Blinddocument
\Blinddocument\Blinddocument\Blinddocument
\end{document}

• I have to say, the starred versions of \ihead and others are very very comfortable. Nov 3 '14 at 18:22
• I accepted this answer because it seems to be sematically cleaner to me. Could you please include a small note on \ifstr? I guess it works like "if #1 == #2 then #3 else #4"? Nov 4 '14 at 18:25

With your current setup, it's sufficient to patch \chaptermark At the Beginning of the Document to call \@mkboth with only the left mark used; keeping the right mark empty:

\usepackage{etoolbox}
\makeatletter
\AtBeginDocument{%
\patchcmd{\chaptermark}% <cmd>
{\@mkboth}% <search>
{\chaptermkboth}% <replace>
{}{}% <success><failure>
}
\newcommand{\chaptermkboth}[2]{\@mkboth{#1}{}}
\makeatother


There are other uses of \@mkboth inside \chaptermark, but the first seems to be the most important in this case.

\documentclass{scrreprt}

\usepackage{lipsum,etoolbox}
\usepackage{scrpage2}
\automark[section]{chapter}

\makeatletter
\AtBeginDocument{%
\patchcmd{\chaptermark}% <cmd>
{\@mkboth}% <search>
{\chaptermkboth}% <replace>
{}{}% <success><failure>
}
\newcommand{\chaptermkboth}[2]{\@mkboth{#1}{}}
\makeatother

\begin{document}

\chapter{chapter}
\lipsum \lipsum

\section{section}
\lipsum

\end{document}

• If you add a hint on the successor scrlayer-scrpage i can delete the comment to the Q. Nov 3 '14 at 7:03