[Tex/LaTex] section numbers (\thesection) and names have next page’s values

header-footermemoirpage-breakingsectioning

I have customized my headers to display section numbers and section names.

However: (a) The section number (\thesection) in my header often shows up with the next page's value. (b) The section name for the first page in a chapter needs to be manually set.

In the following example code, (a) the section numbers of pages 1-3 display the next page's section number. (b) Also the first page's section name needs to be manually set with \markright, or it displays as empty text.

Btw, my assumption is that all of my chapters start on odd pages. (I'm currently doing this manually by using \ensuredoublepage, though one could certainly amend the \chapter command.)

This problem seems to stem from the timing of page breaking, sectioning, and writing the header (with the section numbers/names). What is an easy way to fix this?

Note that the example code looks long (in order to fill multiple pages required to demonstrate this effect) but is actually very simple. For understanding the problem, only the code up till the \chapter command is important, and the problem must lie there as well.

\documentclass{memoir}

\makeoddhead{myheadings}{\textit{\rightmark}}{}{\textit{\thesection} {\ } -- {\ } \thepage}
\makeevenhead{myheadings}{\thepage {\ \ } -- {\ \ }\textit{\thesection}}{}{\textit{\leftmark}}
\makeheadrule{myheadings}{\textwidth}{\normalrulethickness}

\makepagestyle{chapter}
\makeoddhead{chapter}{\textit{\rightmark}}{}{\textit{\thesection} {\ } -- {\ } \thepage}
\makeheadrule{chapter}{\textwidth}{\normalrulethickness}

\begin{document}

\pagestyle{myheadings}
  \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
  \renewcommand{\sectionmark}[1]{\markright{#1}}

\markright{Section one (Without this macro, there'd be no text here!)}

\chapter{The only chapter}

This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text. This is introductory text.


\section{Section one}

This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section.

This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section. This is the first section.


\section{Section two}

This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section.

This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section.

This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section. This is the second section.This is the second section. This is the second section.

\section{Section three}

This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section.

This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section.

This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section. This is the third section.This is the third section. This is the third section.

\section{Section four}

This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section.

This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section.

This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section. This is the fourth section.This is the fourth section. This is the fourth section.

\end{document}

Best Answer

Ok, forget my previous answer ;-) I now found this document, which gives good information in 1.3.

The first problem is, that you are using \thesection in \makeoddhead (e.g.). This is expanded at the wrong time, like you suspected yourself. If you put it into some \*mark command, it will work.

The second problem is, that you are putting the chapter name into \leftmark but you use \rightmark on the first page of the chapter. As the above cited document says, \leftmark comes from the last definiton and \rightmark from the first definition on the page. Unfortunately there is no \markleft. So in order to retain the chapter name for the following pages, putting it in \leftmark and later only using \markright for the section number would be ok. Only now on the first page, \rightmark contains the first definition, which is the empty one of \markboth in \chaptermark.

In order to circumvent this. I propose, saving the chapter name and later reissuing it into \rightmark. The following does this in a way, that alternates chapter and section names in the header. Notice that you will need some additional adjustment if chapters start on even pages...

\documentclass{memoir}

\usepackage{extramarks}

\makepagestyle{myheadings}
\makeoddhead{myheadings}{\textit{\leftmark}}{}{\textit{\rightmark} {\ } -- {\ } \thepage}
\makeevenhead{myheadings}{\thepage {\ \ } -- {\ \ }\textit{\rightmark}}{}{\textit{\cchapter}}
\makeheadrule{myheadings}{\textwidth}{\normalrulethickness}

\makepagestyle{chapter}
\makeoddhead{chapter}{\textit{\leftmark}}{}{\textit{\lastrightmark} {\ } -- {\ } \thepage}
\makeheadrule{chapter}{\textwidth}{\normalrulethickness}

\renewcommand\chaptermark[1]{\markboth{#1}{}\def\cchapter{#1}}
\renewcommand\sectionmark[1]{\markboth{#1}{\thesection}}
\pagestyle{myheadings}
\begin{document}
...