3

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}
  • 1
    You can always use the lipsum package when in need of dummy text. – Werner Jul 26 '12 at 06:59
  • Great recommendation! I'll leave this code block as is though, as its readability is fine. – Lover of Structure Jul 26 '12 at 07:07
  • 1
    You can't use directly \thesection in the header. Due to the asynchronous page builder the nummer is often not correct at the time the page is build. The information must go through a \mark command and then be inserted with a \leftmark or \markright. What header do you want on chapter pages and on the other pages? – Ulrike Fischer Jul 26 '12 at 07:29
  • @canaaerus Almost like in user canaaerus' solution, except I am looking to have the section title on odd pages (with the chapter title on even pages, like now). – Lover of Structure Jul 26 '12 at 08:19
  • @user14996: Do chapters always start on the odd pages? – bodo Jul 26 '12 at 08:53
  • @canaaerus In the style I've set up, yes (I guess I've forgotten to include this in the sample code). – Lover of Structure Jul 26 '12 at 09:00

2 Answers2

3

You can give more structure to the right mark:

\documentclass{memoir}

\makeatletter
\makeoddhead{myheadings}
  {{\let\decide\@secondoftwo\itshape\rightmark}}
  {}
  {{\let\decide\@firstoftwo\itshape\rightmark}\quad--\quad\thepage}
\makeevenhead{myheadings}
  {\thepage\quad--\quad{\let\decide\@firstoftwo\itshape\rightmark}}
  {}
  {\itshape\leftmark}
\makeheadrule{myheadings}{\textwidth}{\normalrulethickness}

\makepagestyle{chapter}
\makeoddhead{chapter}
  {{\let\decide\@secondoftwo\itshape\rightmark}}
  {}
  {{\let\decide\@firstoftwo\itshape\rightmark}\quad--\quad\thepage}
\makeheadrule{chapter}{\textwidth}{\normalrulethickness}
\makeatother

\providecommand{\decide}[2]{} % in case something goes wrong

\begin{document}

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

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

\chapter{The only chapter}

The \sectionmark command issues \protect\decide{\thesection}{Title of the section} (the argument passed to \sectionmark is always the title). Then we decide at typesetting time what argument to \decide is wanted, by letting it to be \@firstoftwo when we want the section number and to \@secondoftwo when we need the title.

NOTE. I wouldn't put a header on the first page of a chapter under (almost) any circumstances, let alone a header with a rule below it: the information your header carries can be easily drawn from the page itself.

The "almost" refers to some situations where a copyright notice appears on every page: some publishers in Italy adopt this strategy for avoiding unauthorized photocopying of their books and they want a copyright symbol followed by the ISBN number. However this number is in small type and not too obtrusive (albeit ugly).

egreg
  • 1,121,712
  • Great. That was one of my attempts too, but I couldn't get it to work; too tricky for me ;-) – bodo Jul 26 '12 at 09:19
  • Wow, impressive (and, what a headache)! I'll learn a lot from this. As ingenious as this is, is there a way to do this without the explicit call \markright{\protect\decide{1.1}{Section one (Without this macro, there'd be no text here!)}} at the end? – Lover of Structure Jul 26 '12 at 09:26
  • @user14996 I don't think you can dispense with it, but see my added comment to the answer. – egreg Jul 26 '12 at 09:32
  • Thanks. I've accepted user canaaerus's answer as he drew attention to and inquired about my implicit/hidden constraint of having all chapters start on odd pages and because he made me aware of the extramarks package (and has no explicit call to \markright). But your solution is great too. // As for the header on a chapter's first page: I know it's usually not done (though I've seen it), but unless the chapter head is elaborate (not so common), I don't find it ugly at all. Also, if redundancy (of header data) is an argument, it'd also be an argument for numbering only even-numbered pages. – Lover of Structure Jul 26 '12 at 20:13
1

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}
...
bodo
  • 6,228
  • You should never use \thesection directly in a header. (\thechapter is not a problem as long \chapter starts a new page and so you are in control of the page break). – Ulrike Fischer Jul 26 '12 at 07:42
  • @UlrikeFischer: I revised my answer. – bodo Jul 26 '12 at 07:59
  • 1
    With your new code you will run into problems if there is a chapter without a \section on the first page. Then the left mark will come from the last section of the previous chapter. \chapter must clear the marks, so it should use \markboth. You can get the last right mark with the help of the extramarks package. But before you twist the code even more: it is imho simply a bad idea to use headers on the first chapter page. It doesn't look good. – Ulrike Fischer Jul 26 '12 at 08:19
  • @UlrikeFischer: You are certainly right. I'll just replace \markright with \markboth. That shouldn't give any problems as the section command always reinstates the right mark. – bodo Jul 26 '12 at 08:27
  • @UlrikeFischer I differ with you on the aesthetic judgment, preferring consistency. I think the lack of headers as well as centered-bottom page numbers on the first page of a chapter seem to be historical practice, making mechanical typesetting easier. But to everyone his/her own taste. // The help of both of you has been great, but actually I was looking for the section name on odd pages (and the chapter name on even pages), and I just can't figure out how. – Lover of Structure Jul 26 '12 at 08:43
  • @user14996: I don't like the centered page numbers on chapter pages either. But at the top chapter pages are special: they can have a fancy chapter title, perhaps a border, or an epigraph. A header rule with an optional section number looks odd. Consider also the header of the page to the left: It will either contain the previous chapter or be empty. In any case the section number on the right (on the new chapter page) will not belong to it. Do you think this is consistent? – Ulrike Fischer Jul 26 '12 at 08:51
  • @UlrikeFischer I'd say it depends on how fancy the chapter's start is. As for your other points: I think with "ch.sec" numbering next to the page number on both L and R pages it looks nice to me. (Or I'm misunderstanding your point.) What is indeed arguably inconsistent is that chapter and section names in my style (see the original code, except with fixed section numbers/names) would be that chapter/section names would only apply to that one page but not the opposite one. And if you have multiple sections on the same page, it'd be unclear how to name them, though that's a general problem. – Lover of Structure Jul 26 '12 at 08:58
  • @canaaerus Awesome, except now the first page's top left compiles to display "Section two", where I want "Section one" (since "Section two" only starts on the next page (p. 2)). – Lover of Structure Jul 26 '12 at 09:07
  • @user14996: Now it should work completely. Using the extramarks package seems to be the only sensible way. – bodo Jul 26 '12 at 09:10