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 have a code that looks like the following:

\documentclass{scrbook}
\usepackage[automark]{scrpage2}
\usepackage{lipsum}


\let\oldchapter\chapter
\newcommand\temphead{}
\newcommand\chaphead{}
\renewcommand\chapter[2][\temphead]{%
    \renewcommand\temphead{#2}%
    \renewcommand\chaphead{#2}%                     
    \oldchapter[#1]{#2}}

\title{My title}

\makeatletter             
\let\titlehead\@title         
\makeatother

\cfoot[\rule{6mm}{0.8pt}\\\pagemark]{\rule{6mm}{0.8pt}\\\pagemark}
\ofoot[]{}
\lehead[]{}
\cehead[]{\titlehead}    
\cohead[]{\chaphead}                                          
\ohead[]{}

\renewcommand*{\chapterpagestyle}{scrplain}


\begin{document}
\pagestyle{scrheadings}

\chapter{My first chapter}

\lipsum
\lipsum


\chapter{My second chapter}

\end{document}

The goal is to display the chapter name on right pages, and the title of the book of left pages.

Now when the page preceding a new chapter is a right page, the chapter displayed is the one of the following chapter. My sample code shows it, as the last page of the first chapter says "My second chapter".

How can I fix this?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

You can add \clearpage or (\cleardoublepage if needed) right before \chapter{My second chapter}.

share|improve this answer
    
For some reason, my full code has a \cleardoublepage but it doesn't fix the issue. I'll try to update the example to reproduce my error. –  ℝaphink May 3 '11 at 12:49
    
It seems my issue was that I used \input instead of \include in my main document to include chapters. Any idea why that would produce this effect? –  ℝaphink May 3 '11 at 12:56
3  
\include contains a \clearpage command in its definition; \input does not add \clearpage. –  Gonzalo Medina May 3 '11 at 13:07
add comment

The normal \chapter contains a \cleardoublepage (which acts like \clearpage in one-sided documents). You need to add a \cleardoublepage before changing the header to flush out the last page of the previous chapter. The second \cleardoublepage in the \oldchapter will no cause any empty pages.

\renewcommand\chapter[2][\temphead]{%
    \cleardoublepage
    \renewcommand\temphead{#2}%
    \renewcommand\chaphead{#2}%
    \oldchapter[#1]{#2}}

This makes your new \chapter work with \input and \include or in the main file.

share|improve this answer
add comment

The issue was that I used \input instead of \include to include chapter files. This caused the \chaphead macro to contain the wrong value. Any explanation as to why this would happen is welcome.

share|improve this answer
1  
\include contains a \clearpage or \clearand so flushes the last page of the previous chapter. \input` does not include one. Simply add a \cleardoublepage in the front of your macro as shown in my answer and you don't have to worry about how you include the \chapter. –  Martin Scharrer May 3 '11 at 13:03
add comment

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.