# How to put both chapter and section name in the header of the page?

I'm working on a one-sided LaTeX document with document class scrreprt and I'm using scrpage2 to customize headers and footers of the pages. At the moment, the respective chapter name is shown in the upper right corner of each page using the following code in the preamble:

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


Now I want to change this such that, when a section begins inside a chapter, the \headmark should change to the format

chapter name - section name

on all pages which contain text of this section until the section finishes. If I change \automark to \automark{section}, then only the section names get displayed but not the chapters names anymore. How can I display both? Is it maybe also possible to put the chapter name in the upper-left corner and the section name in the upper-right corner of each page?

-
Would \rohead{\leftmark{} --- \rightmark} work? – yo' Jan 4 '13 at 12:57
Be carefull. Chapter and Section together could be to much text for on line in the header. – Kurt Jan 4 '13 at 12:59
@tohecz No, this just displays the chapter name twice on each page. I think this is because I have a one-sided document using the option autooneside, therefore there is no difference between \leftmark and \rightmark. – pemistahl Jan 4 '13 at 13:00
@Kurt Don't worry, that's not a problem at the moment. :) – pemistahl Jan 4 '13 at 13:03
Please, compose a full Minimal Working Example, I tried to make one myself and it doesn't show any headers. – yo' Jan 4 '13 at 13:04

This does exactly what you need. The only necessary thing was a proper definition of \sectionmark:

\documentclass{scrreprt}

\usepackage[autooneside]{scrpage2}
\automark{chapter}
\renewcommand\sectionmark[1]{\markright{\MakeMarkcase {\thesection\hskip .5em\relax#1}}}

\usepackage{lipsum}

\begin{document}

\chapter{Cht}
\section{Sct}
\lipsum
\section{SScctt}
\lipsum

\end{document}


This actually shows Section -- Chapter, to show them the other way around, change \leftmark{} --- \rightmark to \rightmark{} --- \leftmark.

-
+1 This works quite well but for pages that start a chapter but do not start a section, the chapter name gets displayed twice. This is ugly. Do you know any way to avoid this? – pemistahl Jan 4 '13 at 13:21
@PeterStahl I added the check for equal marks – yo' Jan 4 '13 at 13:41
I already upvoted your answer -- in the revised version, it should become the accepted one. – lockstep Jan 4 '13 at 13:44
Just now, after a hint by egreg, I made a version that is expandable. – yo' Jan 4 '13 at 13:47
Awesome, your revised solution is far better. The good thing is that the hyphen after the chapter name is not shown if there is no section name to display. The chapter name is now always in the upper-right corner but I think this is better from a design-point-of-view. I will accept this one because I don't have to tell LaTeX it's a two-sided document as in @lockstep's solution. A two-sided setting may lead to problems with other packages later on. Thanks a lot! :) – pemistahl Jan 4 '13 at 14:05

A possible solution is to use the scrreprt class option twoside=semi (with margins like oneside, but different \leftmark and \rightmark).

\documentclass[twoside=semi]{scrreprt}

\usepackage[automark]{scrpage2}

\usepackage{lipsum}

\begin{document}

\chapter{foo}

\section{foobar}

\lipsum[1-12]

\chapter{bla}

\end{document}

-
+1 This exactly produces what I had in mind. It puts the chapter name in the upper-left and the section name in the upper-right corner and nothing gets displayed twice on a single page. Very good. :) Thank you. I will probably accept this solution but will wait for @tohecz to maybe come up with an improvement to his solution. – pemistahl Jan 4 '13 at 13:24
Is it possible to put the chapter name in the upper-right corner only for those pages that contain a chapter but no section? At the moment, the chapter name is always in the upper-left corner. – pemistahl Jan 4 '13 at 13:39
@PeterStahl It may be possible, but is beyond my abilities. (Also, it would be somewhat incosistent.) – lockstep Jan 4 '13 at 13:42

Note, that scrpage2 is depreciated. So here is a suggestion using scrlayer-scrpage.

\documentclass{scrreprt}

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