# \chaptertitle from titleps ignored after redefinition of \chapter macro

The following MWE shows that, if the \chapter macro is redefined as in a previous question of mine: Prepend different stuffs to starred and unstarred \chapter commands, the \chaptertitle macro from the titleps (or in titlesec?) package is ignored with e.g. \tableofcontents, \listoffigures and \listoftables commands (but is not ignored with ordinary chapters).

\documentclass{book}
\usepackage{lipsum}
\usepackage{xparse,xpatch}
\usepackage{titleps}
\usepackage{tocvsec2}
%
\makeatletter%
% Hack to be sure \resetsecnumdepth* from tocvsec2 behaves well
\@ifpackagelater{tocvsec2}{2014/03/10}{%
}{%
\renewcommand\resetsecnumdepth{%
\@ifstar{%
\setcounter{secnumdepth}{\value{max@secnumdepth}}%
\edef\stack@secnumdepth{\the\c@secnumdepth\relax}%
}{%
\ifx\stack@secnumdepth\@empty%
\PackageWarning{tocvsec2}{There is no previous value for secnumdepth}%
\else%
\afterassignment\gobble@secnumdepth%
\expandafter\c@secnumdepth\expandafter\numexpr\stack@secnumdepth\@nil%
\fi%
}%
}%
}%
\makeatother%
\let\ORIchapter\chapter%
\RenewDocumentCommand\chapter{som}{%
\IfBooleanTF{#1}
{
\setsecnumdepth{none}%
}
{%
\resetsecnumdepth*
}
\IfNoValueTF{#2}
{\ORIchapter{#3}}
{\ORIchapter[#2]{#3}}%
}
% we don't want \chapter* for \tableofcontents and similar lists
\xpatchcmd{\tableofcontents}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoffigures}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIchapter}{}{}
% populate the stack
\AtBeginDocument{\setsecnumdepth{none}}
%
\newpagestyle{liminary}[]{%
[]%
[\chaptertitle]%
{%
\sectiontitle%
}%
{}%
{\thepage}%
}%
%
\pagestyle{liminary}
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\chapter{A 1st chapter}
\lipsum[1-10]
\section{A 1st section of the 1st chapter}
\lipsum[1-10]
\end{document}


If the lines:

\xpatchcmd{\listoffigures}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIchapter}{}{}


are commented, the corresponding \chaptertitle are not ignored (maybe with side effects I didn't notice) but if the line:

\xpatchcmd{\tableofcontents}{\chapter}{\ORIchapter}{}{}


is commented, a "TeX capacity exceeded" error arises:

! TeX capacity exceeded, sorry [input stack size=5000].
\@nameuse #1->\csname #1
\endcsname
l.62 \tableofcontents

!  ==> Fatal error occurred, no output PDF file produced!


Maybe it could be helpful to notice that, with or without:

\xpatchcmd{\printglossary}{\chapter}{\ORIchapter}{}{}


the \printglossary command behaves well w.r.t. \chaptertitle, as shown by the following MWE:

\documentclass{book}
\usepackage{lipsum}
\usepackage{xparse,xpatch}
\usepackage{titleps}
\usepackage{tocvsec2}
\usepackage{glossaries}
\makeglossaries
\newglossaryentry{bar}{%
name={bar},
description={bar}
}
%
\makeatletter%
% Hack to be sure \resetsecnumdepth* from tocvsec2 behaves well
\@ifpackagelater{tocvsec2}{2014/03/10}{%
}{%
\renewcommand\resetsecnumdepth{%
\@ifstar{%
\setcounter{secnumdepth}{\value{max@secnumdepth}}%
\edef\stack@secnumdepth{\the\c@secnumdepth\relax}%
}{%
\ifx\stack@secnumdepth\@empty%
\PackageWarning{tocvsec2}{There is no previous value for secnumdepth}%
\else%
\afterassignment\gobble@secnumdepth%
\expandafter\c@secnumdepth\expandafter\numexpr\stack@secnumdepth\@nil%
\fi%
}%
}%
}%
\makeatother%
\let\ORIchapter\chapter%
\RenewDocumentCommand\chapter{som}{%
\IfBooleanTF{#1}
{
\setsecnumdepth{none}%
}
{%
\resetsecnumdepth*
}
\IfNoValueTF{#2}
{\ORIchapter{#3}}
{\ORIchapter[#2]{#3}}%
}
% we don't want \chapter* for \tableofcontents and similar lists
\xpatchcmd{\tableofcontents}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoffigures}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\printglossary}{\chapter}{\ORIchapter}{}{}
% populate the stack
\AtBeginDocument{\setsecnumdepth{none}}
%
\newpagestyle{liminary}[]{%
[]%
[\chaptertitle]%
{%
\sectiontitle%
}%
{}%
{\thepage}%
}%
%
\pagestyle{liminary}
\begin{document}
\printglossary
\tableofcontents
\listoffigures
\listoftables
\chapter{A 1st chapter}
\lipsum[1-10]
\section{A 1st section of the 1st chapter}
\lipsum[1-10]
\end{document}


Hence my question: is it possible to adjust the redefinition of the \chapter macro as in a previous question of mine: Prepend different stuffs to starred and unstarred \chapter commands in order \chaptertitle from titleps package is not ignored for commands like \tableofcontents, \listoffigures and \listoftables?

• I don't precisely know what's going on, but adding \gdef\chaptertitle{\contentsname} before \ORIchapter in the patch to \tableofcontents seems to work. The problem seems to be in the fact that there are concurrent tries to redefine \chapter Mar 11, 2014 at 9:30
• OK. Is it safe to drop the lines that patch \listoffigures and \listoftables (to make them use \ORIchapter instead of the new \chapter macro)? Mar 11, 2014 at 9:39
• @egreg In fact, it is not enough in the case the glossaries is used (as in the second MWE above): in such a case, the \chaptertitle of \tableofcontents is the glossaries' one: "Glossary". Mar 11, 2014 at 9:47

The titlesec package redefines \chapter at begin document, so you have to delay your own redefinition.

I present the version with the glossary, to show that everything goes well; I removed the hack for tocvsec2 since I updated my distributions.

\documentclass{book}
\usepackage{lipsum}
\usepackage{xparse,xpatch}
\usepackage{tocvsec2}
\usepackage{titleps}

\usepackage{glossaries}
\makeglossaries

\AtBeginDocument{% delay the redefinition when titlesec has done its own
\let\ORIDENISchapter\chapter
\RenewDocumentCommand\chapter{som}{%
\IfBooleanTF{#1}
{
\setsecnumdepth{none}%
}
{%
\resetsecnumdepth*
}%
\IfNoValueTF{#2}
{\ORIDENISchapter{#3}}
{\ORIDENISchapter[#2]{#3}}%
}
}% end of \AtBeginDocument
% we don't want \chapter* for \tableofcontents and similar lists
\xpatchcmd{\tableofcontents}{\chapter}{\ORIDENISchapter}{}{}
\xpatchcmd{\listoffigures}{\chapter}{\ORIDENISchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIDENISchapter}{}{}
% populate the stack
\AtBeginDocument{\setsecnumdepth{none}}
%
\newpagestyle{liminary}[]{%
[]%
[\chaptertitle]%
{%
\sectiontitle%
}%
{}%
{\thepage}%
}%
%
\pagestyle{liminary}

\newglossaryentry{bar}{
name={bar},
description={bar}
}

\begin{document}
\printglossary
\tableofcontents
\listoffigures
\listoftables
\chapter{A 1st chapter}
\lipsum[1-10]
\section{A 1st section of the 1st chapter}
\lipsum[1-10]
\end{document}


The top of page 4 (the empty page after the table of contents) shows the correct title.