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 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}
share|improve this question
1  
You can always use the lipsum package when in need of dummy text. –  Werner Jul 26 '12 at 6:59
    
Great recommendation! I'll leave this code block as is though, as its readability is fine. –  Lover of Structure Jul 26 '12 at 7: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 7: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 8:19
    
@user14996: Do chapters always start on the odd pages? –  canaaerus Jul 26 '12 at 8:53
show 1 more comment

2 Answers 2

up vote 1 down vote accepted

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}
...
share|improve this answer
    
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 7:42
    
@UlrikeFischer: I revised my answer. –  canaaerus Jul 26 '12 at 7: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 8: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. –  canaaerus Jul 26 '12 at 8: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 8:43
show 4 more comments

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

share|improve this answer
    
Great. That was one of my attempts too, but I couldn't get it to work; too tricky for me ;-) –  canaaerus Jul 26 '12 at 9: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 9:26
    
@user14996 I don't think you can dispense with it, but see my added comment to the answer. –  egreg Jul 26 '12 at 9: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
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.