# Properly capture chapter name

I have a code that looks like the following:

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

\let\oldchapter\chapter
\oldchapter[#1]{#2}}

\title{My title}

\makeatletter
\makeatother

\cfoot[\rule{6mm}{0.8pt}\\\pagemark]{\rule{6mm}{0.8pt}\\\pagemark}
\ofoot[]{}

\renewcommand*{\chapterpagestyle}{scrplain}

\begin{document}

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

-

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

-
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
\include contains a \clearpage command in its definition; \input does not add \clearpage. –  Gonzalo Medina May 3 '11 at 13:07

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
\oldchapter[#1]{#2}}


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

-

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.

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