# titleps' headers lose their numbers just before \appendix

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}
%
\ifboolexpr{
test {\ifnumcomp{\value{secnumdepth}}{>}{-1}} and test {\ifnumcomp{\value{chapter}}{>}{0}}
}{%
\MakeUppercase\chaptername{}\ \thechapter.
}{%
}%
\chaptertitle%
}%
\ifboolexpr{
test {\ifnumcomp{\value{secnumdepth}}{>}{0}} and test {\ifnumcomp{\value{chapter}}{>}{0}}
}{%
\thesection.
}{%
}%
\sectiontitle%
}%
%
\newpagestyle{mypagestyle}[]{%
[]%
{%
\ifcsempty{\sectiontitle}{%
}{%
}%
}%
{}%
{\thepage}%
}%
%
\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}


• \appendix sets the chapter counter to 0. Without the \cleardoublepage the command is executed on the page where the section ends. May 31 '18 at 17:57
• Indeed, but why is it executed so early (two pages before)? May 31 '18 at 18:10
• That's where the appendix command is. Add \pretocmd\appendix{some text}{}{\fail} to your document and check where "some text" is. 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)? May 31 '18 at 18:23
• 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} Jun 1 '18 at 7:03

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.
• 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. 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? May 29 '20 at 17:33