Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
\pagestyle{scrheadings}
\clearscrheadfoot
\automark{chapter}
\rohead{\headmark}

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?

share|improve this question
    
Would \rohead{\leftmark{} --- \rightmark} work? –  yo' Jan 4 '13 at 12:57
1  
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

2 Answers 2

up vote 7 down vote accepted

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

\documentclass{scrreprt}

\usepackage[autooneside]{scrpage2}
\pagestyle{scrheadings}
\clearscrheadfoot
\automark{chapter}
\renewcommand\sectionmark[1]{\markright{\MakeMarkcase {\thesection\hskip .5em\relax#1}}}
\rohead{\ifnum\expandafter\pdfstrcmp\botmark=0 \rightmark\else\leftmark{} --- \rightmark\fi}

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

share|improve this answer
    
+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
1  
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}
\pagestyle{scrheadings}
\clearscrheadfoot
\lehead{\leftmark}
\rehead{\rightmark}
\lohead{\leftmark}
\rohead{\rightmark}

\usepackage{lipsum}

\begin{document}

\chapter{foo}

\section{foobar}

\lipsum[1-12]

\chapter{bla}

\end{document}
share|improve this answer
    
+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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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