[Tex/LaTex] Overfull hbox (in the middle of page) in ToC with tocloft package and cftchapaftersnum

boxestable of contentswarnings

To customize the appearance of our Table of contents we use the tocloft package.
One point we would like to implement is to underline the chapter entries in the ToC.

However, if we use the \cftchapaftersnum command to specify a horizontal line we always get a weird overfull hbox warning. (Weird, because if I shorten the line, the overfull hbox is shown in the middle of the page.

Small example compiled with MikTeX and pdfLaTeX:

\documentclass[draft]{book} % use draft to show overfull hboxes

\usepackage{tocloft}
% underline chapter entries:
\renewcommand{\cftchapaftersnum}{\hspace{4mm}\rule[-0.5mm]{0.5\textwidth}{.4pt}} 

\begin{document}

\tableofcontents

\chapter{Antelope}
Antelope is a term referring to many even-toed ungulate 
species found all over the world.

\rule[-0.5mm]{0.5\textwidth}{.4pt} 
This \verb|\rule| does not generate a warning!

\section{Impala}
An impala is a medium-sized African antelope.

\emph{fin \dots}

\end{document}

Note that I've placed 0.5\textwidth as ruler width in the example above. This way, my draft PDF shows the black rectangle for the overfull box in the middle of the page (just at the end of the \rule)

What's going on here?

Best Answer

The documentation for tocloft makes this clear.

Normally the section number is typeset within a box of width \cftXnumwidth. Within the box the macro \cftXpresnum is first called, then the number is typeset, and next the \cftXaftersnum macro is called after the number is typeset. The last command within the box is \hfil to make the box contents flushleft.

What this means is that the box is constructed like

\hbox to\cftchapnumwidth{\cftchappresnum 1\cftchapaftersnum\hfil}

Your box is wider than \cftchapnumwidth.

If you change

\renewcommand{\cftchapaftersnum}{\hspace{4mm}\rule[-0.5mm]{0.5\textwidth}{.4pt}}

to

\renewcommand{\cftchapaftersnum}{\hspace{4mm}\rule[-0.5mm]{0.5\textwidth}{.4pt}\hss}

then the infinite shrinkability causes the warning to go away.

Edit: You should probably not be using spacing of 4mm and instead using something proportional to the size of the font. Better still would be to use \cftchapaftersnumb (note the b at the end):

\renewcommand{\cftchapaftersnumb}{\rlap{\rule[-0.5mm]{0.5\textwidth}{.4pt}}}

Using -0.5mm is also probably a bad idea.