# spacing in toc with chapters, appendices, and a dotted line after each

I'm trying to format my dissertation, and the formatting guidelines require a dotted line for chapters as well as sections, etc. The challenge is that I have several appendices, and the length of the word "Chapter" is less than the length of the word "Appendix", which is causing issues with getting the spacing. The .cls style was provided to me by another student and appears incompatible with tocloft, so that is not a working solution for me. I can reproduce the problem using the book package by redefining \chapter and \l@chapter accordingly, so the fix is somewhere in these two definitions.

### MWE1

In this MWE, I've changed the definition of \l@chapter is redefined to add the dotted lines, using How to add leaders to Table of Contents without tocloft? as a guide. Notice however, that the spacing of between "Appendix A" and the title is too small. (This isn't strictly a dotted line issue, I'd have to solve it anyways, but (as I will show), they compound.

\documentclass{book}

\makeatletter
\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{\protect\numberline{\@chapapp\hspace{2mm}\thechapter}\hspace{1.5cm}{#1}}%
\else
\fi
\chaptermark{#1}%
\if@twocolumn
\else
\fi}
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \normalfont
\hskip -\leftskip
#1\nobreak
\leaders\hbox{$\m@th \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill%
\nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}
\makeatother

\begin{document}
\tableofcontents

\chapter{one longer title}

\chapter{two}

\chapter{three}

\appendix
\chapter{This needs to be right too}

\end{document}


### MWE2

It looked like the issue was that the space from the start of the line to the text start was hard-coded, so I removed that, and moved more of the content inside the \numberline command.
(Aside - I cannot find any documentation for what \numberline does, anyone know?) This basically shifts the spacing problem to the dots instead of the text - so I next tried doing the opposite, and removed everything from inside \numberline - and it's almost right, but now there is an indent that will not go away. (N.B. - if anyone does not need dots, change {\protect\numberline{}\@chapapp\hspace{2mm}\thechapter\hspace{2mm}{#1}}% to {\protect\numberline{\@chapapp\hspace{2mm}\thechapter\hspace{2mm}{#1}}}% solved the spacing problem. I've tried the normal hack of adding \hspace{-.25in}, and that does not work. I do not see where this indent is defined.

Can anyone help?

\documentclass{book}

\makeatletter

\renewcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@chapter\@schapter}
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
\refstepcounter{chapter}%
\typeout{\@chapapp\space\thechapter.}%
{\protect\numberline{}\@chapapp\hspace{2mm}\thechapter\hspace{2mm}{#1}}%
\else
\fi
\chaptermark{#1}%
\if@twocolumn
\else
\fi}
\renewcommand*\l@chapter[2]{%
\ifnum \c@tocdepth >\m@ne
\vskip 1.0em \@plus\p@
\setlength\@tempdima{1.5em}%
\begingroup
\parindent \z@ \rightskip \@pnumwidth
\parfillskip -\@pnumwidth
\leavevmode \normalfont
\hskip -\leftskip
#1\nobreak
\leaders\hbox{$\m@th \mkern \@dotsep mu\hbox{.}\mkern \@dotsep mu$}\hfill%
\nobreak\hb@xt@\@pnumwidth{\hss #2}\par
\penalty\@highpenalty
\endgroup
\fi}
\makeatother

\begin{document}
\tableofcontents

\chapter{one longer title}

\chapter{two}

\chapter{three}

\appendix
\chapter{This needs to be right too}

\end{document}


So here's what I think is going on.

This line in the definition of \@chapter writes the key argument for the \contentsline command which ends up in the .toc as

{\protect\numberline{\@chapapp\hspace{2mm}\thechapter}\hspace{1.5cm}{#1}}


So, for instance, when it reads in the .toc file, what LaTeX sees is

\contentsline {chapter}{\numberline {Chapter\hspace {2mm}1}\hspace {2.5cm}{one longer title}}{3}%


However, that doesn't get one to the end of the story, because we need to understand \numberline. That prints its argument (here {Chapter\hspace{2mm}1}) in a box. How big is that box? The slightly surprising answer is that it is as big as the temporary variable \@tempdima at the time it is called. (You can find this if you search for \numberline in the main LaTeX sources: texdoc sourc2e is your good friend in these situations.)

So the size of the box is actually given by this line in the definition of \l@chapter, and in particular this line:

\setlength\@tempdima{2.5em}%


Now we can understand the problem. The box in this case is just big enough for "Chapter A" but not for "Appendix A". The massive 1.5cm of space added after the box conceals that to some extent. But remember, LaTeX adds the space after the box not its contents: if they stick out of the box, they get ignored. So it's looking like this.

So what we need to do is adjust the various commands to get it all right. We can fiddle with the space after the box. But to my taste the right thing to do here is to get rid of all the space added after the box, and make the box big enough to hold both "Appendix A" and "Chapter 1". So we end up with something like:

    \addcontentsline{toc}{chapter}%
{\protect\numberline{\@chapapp~\thechapter}{#1}}%


in \@chapter and

\setlength\@tempdima{2.2cm}%


in \l@chapter. That (after running at least twice to give time for our changes to percolate to the .toc file ... another little catch for the unwary!)

• Thank you very much for the explanation, and I'm accepting the answer for this reason thanks for explaining what was going on. To meet the dissertation criterion, the between "1/A" and the title text has to be constant, so this isn't quite what I need. Fortunately in my case, it turned out that adding negative space turned out to work (I apparently hadn't run things enough, although I thought I'd run it several times). In any case, I'll try to use this to find a non-hack solution to the problem. Thank you! – Melissa Key May 4 '20 at 13:43

The \numberline does \hbox to <calculated dimen>{<text>\hss}. Maybe you need not it if you don't want to align the left edge of the titles in the TOC:

{\@chapapp\hspace{2mm}\thechapter\hspace{5mm}{#1}}%