2

In the following MWE, the headers are customized, thanks to the titleps package. They are supposed to display the chapter and the section (depending on the parity of the page):

  1. the corresponding numbers (and, for the chapter, the chapter name), but only if they are numbered,
  2. title.

For the former condition, I defined new commands \chapterheader and \sectionheader involving some tests. The whole machinery works well, except for the two last pages (pages 11 and 12) before the appendix: the numbers disappear from the headers.

It is easy to fix the problem with \cleardoublepage inserted just before \appendix but do you see why this problem by default?

\documentclass{book}
\usepackage{mwe}
\usepackage{etoolbox}
\usepackage{titleps}
%
\newcommand*{\chapterheader}{%
  \ifboolexpr{
    test {\ifnumcomp{\value{secnumdepth}}{>}{-1}} and test {\ifnumcomp{\value{chapter}}{>}{0}}
  }{%
    \MakeUppercase\chaptername{}\ \thechapter.
  }{%
  }%
  \chaptertitle%
}%
\newcommand*{\sectionheader}{%
  \ifboolexpr{
    test {\ifnumcomp{\value{secnumdepth}}{>}{0}} and test {\ifnumcomp{\value{chapter}}{>}{0}}
  }{%
    \thesection.
  }{%
  }%
  \sectiontitle%
}%
%
\newpagestyle{mypagestyle}[]{%
  \sethead[\thepage]%
  []%
  [\chapterheader]%
  {%
    \ifcsempty{\sectiontitle}{%
      \chapterheader%
    }{%
      \sectionheader%
    }%
  }%
  {}%
  {\thepage}%
  \headrule%
}%
%
\newcommand{\dummytext}{%
  \chapter{A chapter}
  \lipsum[1-5]

  \section{A section}
  \lipsum[1-15]
}
%
\begin{document}
%
\pagestyle{mypagestyle}
%
\dummytext
\dummytext
%
% \cleardoublepage
%
\appendix
\dummytext
\dummytext
\end{document}

enter image description here

  • 1
    \appendix sets the chapter counter to 0. Without the \cleardoublepage the command is executed on the page where the section ends. – Ulrike Fischer May 31 '18 at 17:57
  • Indeed, but why is it executed so early (two pages before)? – Denis Bitouzé May 31 '18 at 18:10
  • 1
    That's where the appendix command is. Add \pretocmd\appendix{some text}{}{\fail} to your document and check where "some text" is. – Ulrike Fischer May 31 '18 at 18:13
  • Right. Does it mean \appendix's definition would be better if it would contain a \cleardoublepage or \clearpage (depending on openright or not)? – Denis Bitouzé May 31 '18 at 18:23
  • 1
    Your own code is an example why you can't rely on them. headers are made later and the state of counters can have changed then. Another example: put two or more \section on a page and add to the header \the\value{section} and compare the output with \thesection. So your tests should go inside the mark command. E.g. scrbook has something like \renewcommand{\chaptermark}[1]{\ifnumbered{chapter}{number}{} title} – Ulrike Fischer Jun 1 '18 at 7:03
1

titleps provides a macro for cases like this, which «Adds a command or a variable to the list of “marks” to be emitted at a sectioning command». Just add the following line:

\newtitlemark*{chapter}

Now \value{chapter} will return the correct value. This is not done by default because typically only \thechapter is used.

| improve this answer | |
  • I must admit \newtitlemark looks still mysterious to my eyes :) But, anyway, that works well! The only remaining problem is \chaptername instead of \appendixname in the appendix, but I will investigate a bit and ask a new question if necessary. – Denis Bitouzé Jun 2 '18 at 14:55
  • You say "This is not done by default because typically only \thechapter is used" but how could I rely on \thechapter to test whether a chapter is numbered (value > 0) or not in case of, e.g., appendix chapters where \thechapter typically are case alphabetic characters? – Denis Bitouzé May 29 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.