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!

6
  • 2
    You probably didn't change \thechapter, but instead fiddled around with \@makechapterhead, correct? \renewcommand{\thechapter}{\ifnum\value{chapter}<10 0\fi\arabic{chapter}} should suffice.
    – Werner
    Commented Sep 8, 2016 at 6:53
  • Could you provide us with a minimal working example (MWE) that replicates the current behaviour? That way we can best suggest alternatives to achieve your desired output.
    – Werner
    Commented Sep 8, 2016 at 6:54
  • 1
  • Thank you @Werner! So tableofcontents refers to \thechapter while makechapterhead just modifies the appearence of the chapter title?
    – Fabbio
    Commented Sep 8, 2016 at 7:03
  • Pretty much. I'm just guessing that's what you did...
    – Werner
    Commented Sep 8, 2016 at 7:04

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .