# Prepend different stuffs to starred and unstarred \chapter commands

For the unnumbered sections units of documents built with a personal class (which inherits from the book class), I'd like to use:

• not the starred form of the corresponding commands,
• instead the tocvsec2 package's \setsecnumdepth{none} and \resetsecnumdepth switches.

1. no need to use the star form for each sections, subsections, etc. of a unnumbered chapter,
2. no need to use the cryptic \addcontentsline{toc}{...}{...} command for each of the unnumbered sections units supposed to appear in the table of contents,
3. no trouble with the headers of unnumbered chapter and sections (which, with starred form, are the ones of the previous numbered chapter and sections).

This is shown in the two following MCEs: the first one with the traditional star commands (headers problem not solved),

\documentclass{book}
\usepackage{tocvsec2}
\usepackage{lipsum}
\begin{document}
%
\tableofcontents
%
\chapter{Numbered chapter}
\lipsum[1-10]
\section{Numbered section}
\lipsum[1-10]
%
\chapter*{Unnumbered chapter}
\lipsum[1-10]
\section*{Unnumbered section}
\lipsum[1-10]
%
\chapter{Numbered chapter (bis)}
\lipsum[1-10]
\section{Numbered section (bis)}
\lipsum[1-10]
%
\chapter*{Unnumbered chapter (bis)}
\lipsum[1-10]
\section*{Unnumbered section (bis)}
\lipsum[1-10]
%
\end{document}


and the second one with the tocvsec2 package features.

\documentclass{book}
\usepackage{tocvsec2}
\usepackage{lipsum}
\begin{document}
%
\tableofcontents
%
\chapter{Numbered chapter}
\lipsum[1-10]
\section{Numbered section}
\lipsum[1-10]
%
\setsecnumdepth{none}%
%
\chapter{Unnumbered chapter}
\lipsum[1-10]
\section{Unnumbered section}
\lipsum[1-10]
%
\resetsecnumdepth%
%
\chapter{Numbered chapter (bis)}
\lipsum[1-10]
\section{Numbered section (bis)}
\lipsum[1-10]
%
\setsecnumdepth{none}%
%
\chapter{Unnumbered chapter (bis)}
\lipsum[1-10]
\section{Unnumbered section (bis)}
\lipsum[1-10]
%
\end{document}


But, in order to let my personal class be as simple as possible, I'd like to provide both the traditional syntax (with the star form for the unnumbered sections units) and the tocvsec2 package's features. (In fact, it should be enough to redefine the \chapter command.)

All my attempts for this failed, e.g.:

\documentclass{book}
\usepackage{lipsum}
\usepackage{tocvsec2}
%
\makeatletter%
\let\chapter@ORI\chapter%
\renewcommand\chapter{%
\@ifstar{\starred@chapter}{\unstarred@chapter}%
}
\newcommand{\starred@chapter}{\setsecnumdepth{none}\chapter@ORI}%
\newcommand{\unstarred@chapter}{\resetsecnumdepth\chapter@ORI}%
\makeatother%
%
\begin{document}
%
\tableofcontents
%
\chapter{Numbered chapter}
\lipsum[1-10]
\section{Numbered section}
\lipsum[1-10]
%
\chapter*{Unnumbered chapter}
\lipsum[1-10]
\section{Unnumbered section}
\lipsum[1-10]
%
\chapter{Numbered chapter (bis)}
\lipsum[1-10]
\section{Numbered section (bis)}
\lipsum[1-10]
%
\chapter*{Unnumbered chapter (bis)}
\lipsum[1-10]
\section{Unnumbered section (bis)}
\lipsum[1-10]
%
\end{document}


or:

\documentclass{book}
\usepackage{xpatch}
\usepackage{lipsum}
\usepackage{tocvsec2}
%
\makeatletter%
\xpretocmd{\@chapter}{\resetsecnumdepth}{}{}%
\xpretocmd{\@schapter}{\setsecnumdepth{none}}{}{}%
\makeatother%
\begin{document}
%
\tableofcontents
%
\chapter{Numbered chapter}
\lipsum[1-10]
\section{Numbered section}
\lipsum[1-10]
%
\chapter*{Unnumbered chapter}
\lipsum[1-10]
\section{Unnumbered section}
\lipsum[1-10]
%
\chapter{Numbered chapter (bis)}
\lipsum[1-10]
\section{Numbered section (bis)}
\lipsum[1-10]
%
\chapter*{Unnumbered chapter (bis)}
\lipsum[1-10]
\section{Unnumbered section (bis)}
\lipsum[1-10]
%
\end{document}


Do you see a clever way for redefining the unstarred and starred forms of the \chapter command?

-

The trick is to make \chapter* call the original \chapter. However, one has to patch \tableofcontents and the other list making commands because they have \chapter* in them.

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

\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}}

\begin{document}

\tableofcontents

\chapter{Numbered chapter}
\lipsum[1-10]
\section{Numbered section}
\lipsum[1-10]

\chapter*{Unnumbered chapter}
\lipsum[1-10]
\section{Unnumbered section}
\lipsum[1-10]

\chapter{Numbered chapter (bis)}
\lipsum[1-10]
\section{Numbered section (bis)}
\lipsum[1-10]

\chapter*{Unnumbered chapter (bis)}
\lipsum[1-10]
\section{Unnumbered section (bis)}
\lipsum[1-10]

\end{document}


In my opinion, alternating between numbered and unnumbered chapters is bad style. If the introduction should be unnumbered, it should go in the \frontmatter; if it is in the \mainmatter it should be numbered.

Only \backmatter material is not numbered. Or else no chapter is numbered.

-
It works like a charm. I fully agree that "[i]f the introduction should be unnumbered, it should go in the \frontmatter". In such a case, symmetry implies that conclusion is unnumbered as well and hence goes to the \backmatter part. So far so good, except if there are \appendix chapters: IMO, such chapters have (1) to be numbered, (2) to appear after conclusion and here is the clash: "appendix numbered chapters" and "after an unnumbered conclusion" are incompatible requirements with the default \backmatter and \appendix switches. –  Denis Bitouzé Feb 26 at 8:18
@DenisBitouzé The “conclusion” is something I can't understand. My opinion is that it should be the final part of the introduction; why should it be at the end? In any case, the symmetry is preserved by making the introduction into chapter one. –  egreg Feb 26 at 8:51
"[W]hy should it be at the end?" Maybe because, sometimes, the conclusion can be understood only after the whole main matter has been read. And, well, it is rather common to see (and legitimate to provide to the user the possibility to insert) at least one conclusive chapter at the end of a work, isn't it? ;) For instance, for a thesis, in addition to a "normal" conclusion, one could want to gather its research perspectives in a dedicated and unnumbered chapter, before (say) sixty pages of (numbered) appendix chapters. –  Denis Bitouzé Feb 26 at 9:03
@DenisBitouzé Sorry, but I continue to believe that the conclusion should end the introduction. –  egreg Feb 26 at 9:21
For instance, "A Few Notes on Book Design" from Peter Wilson (texdoc memdesign) says "[t]here may be a final chapter in the main matter called Conclusions, or similar" (p. 18) (and, what hits my taste for symmetry, advises the introduction to be in the front matter (p. 72)). Maybe I should ask a new question about the book structure... –  Denis Bitouzé Feb 26 at 10:36