2

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}
\pagestyle{scrheadings}
\clearscrheadfoot
\automark[section]{chapter}

\ihead[\leftmark]{\leftmark}
\chead{}
\ohead{\rightmark}

\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...

1
  • 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.
    – Johannes_B
    Nov 2 '14 at 17:59
2

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
\ihead[\leftmark]{\leftmark}
\ohead{\ifstr{\leftmark}{\rightmark}{}{\rightmark}}

\begin{document}
\tableofcontents

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

\addchap{chapter}
\Blindtext[10]
\addsec{section}
\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}
\pagestyle{scrheadings}
\clearscrheadfoot
\ihead[\leftmark]{\leftmark}
\ohead{\ifstr{\leftmark}{\rightmark}{}{\rightmark}}

\begin{document}
\tableofcontents

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

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

\Blinddocument\Blinddocument\Blinddocument
\Blinddocument\Blinddocument\Blinddocument
\end{document}
2
  • I have to say, the starred versions of \ihead and others are very very comfortable.
    – Johannes_B
    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"?
    – Jasper
    Nov 4 '14 at 18:25
2

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.

enter image description here

\documentclass{scrreprt}

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

\ihead[\leftmark]{\leftmark}
\chead{}
\ohead{\rightmark}

\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}
1
  • If you add a hint on the successor scrlayer-scrpage i can delete the comment to the Q.
    – Johannes_B
    Nov 3 '14 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.