4

While answering this question : French section numbering using bis, ter, etc, I realized that when you add (using \addcontentsline) some content to ToC with "non conventionnal numbering" (in my case 2 bis, 2 ter, etc), the title of the section is almost left-aligned with the usual section titles, but not exactly aligned.

For instance, the following MWE

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\begin{document}
\tableofcontents

\section{First section header}
\lipsum[1]
\section{Second section header}
\lipsum[2]
\section*{Third section header}
\addcontentsline{toc}{section}{\numberline{2 bis} Bis section test}
\end{document}

produces enter image description here

I though this could be managed by redefining \l@section. Indeed, in the LaTeX Companion, it is mentioned that the third argument of \@dottetocline was the width of the box containing the section number. However, adding the following code to my preamble does not change anything.

\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{2.5em}}
\makeatother

Any idea of why this happens, and what could be done to fix this ?

  • Thanks for following up on this. It's nice to finally have the sectioning working the way I want it to! – grfrazee Jul 16 '15 at 1:45
3

When writing content to the ToC, you should be aware that macros could be expanded to reveal their inner workings. This expansion could lead to problems. Here's what happening in your instance, when looking at the .toc file:

\contentsline {section}{\numberline {0.1}First section header}{1}
\contentsline {section}{\numberline {0.2}Second section header}{1}
\contentsline {section}{\hbox to\@tempdima {2 bis\hfil }Bis section test}{1}

Note how the manually-written .toc entry has \hbox to\@tempdima{..} rather than \numberline, even though you specified \numberline. As such, when the .toc file is read in for printing (with a call to \tableofcontents), \@tempdima may have a different meaning that what it should when the traditional \numberline is called.

One solution is provided through \protection from expansion:

enter image description here

\documentclass{book}
%\usepackage[explicit]{titlesec}
\usepackage{lipsum}

\begin{document}
\tableofcontents

\section{First section header}
\lipsum[1]
\section{Second section header}
\lipsum[2]
\section*{Third section header}
\addcontentsline{toc}{section}{\protect\numberline{2 bis}Bis section test}
\end{document}

The .toc file now resembles:

\contentsline {section}{\numberline {0.1}First section header}{1}
\contentsline {section}{\numberline {0.2}Second section header}{1}
\contentsline {section}{\numberline {2 bis}Bis section test}{1}

For adjustment of the \numberline width in general, consider reading Roman numerals become “too wide” in table of contents.

  • Finally, the issue was the space left before #1 in \addcontentsline. But I was unaware of the risks of expansion, thanks for your quick response. – mvienney Jul 15 '15 at 19:18
1

If you don't use the titlesec package or use the titlesec package without the explicit option your redefinition of \l@section works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.