2

Scraping through the documentation I managed to make so that \chapter generates a trailing 0 before the chapter number if the chapter number is smaller than 10.

I would like to do the same for table of contents but I couldn't find any help online. Right now I just see

1 First Chapter              5
2 Second Chapter            23
...
10 Tenth Chapter           130

But I want this:

01 First Chapter             5
02 Second Chapter           23
...
10 Tenth Chapter           130

Does anybody have a clue? Thanks in advance!

Fabbio
  • 143

1 Answers1

2

The change of \thechapter should be done within \@chapter not in the \@makechapterhead command, since in \@chapter the \addcontentsline is used.

I assumed the book class for this.

\documentclass{book}
\usepackage{xpatch}


\newcount\loopcntr


\makeatletter
\xpatchcmd{%
  \@chapter%
}{%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
}{%
  \begingroup
  \renewcommand{\thechapter}{\ifnum\value{chapter} <10\relax 0\arabic{chapter}\else\arabic{chapter}\fi}
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
  \endgroup
}{\typeout{Patch successful!}}{\typeout{Patch failed!}}
\makeatother


\begin{document}

\tableofcontents

\loop\ifnum\loopcntr <10\relax
\advance\loopcntr by 1
\chapter{Kapitel \the\loopcntr}

\repeat

\end{document}

enter image description here