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.
https://tex.stackexchange.com/questions/255503/alignment-of-toc-with-exotic-numbering
tex.stackexchange.com
crawl=CC-MAIN-2019-43
149320
application/http; msgtype=response
sha1:FV5VCFVL374E37WGAIMSACXYFOBCJ67W
<urn:uuid:9b63266d-ca79-46f2-b0b7-44b337d6acd9>
2019-10-20T01:27:41Z
151.101.129.69
text/html
sha1:LKTEJ4FMPVADVAQ7I74SKY6BUKPQCFDW
<urn:uuid:835fe94a-9ddc-4c27-9a27-2bcc28e769b3>
https://tex.stackexchange.com/questions/255503/alignment-of-toc-with-exotic-numbering
response
<urn:uuid:94fe5020-0764-4a84-8303-d2dc4420f4a7>
isPartOf: CC-MAIN-2019-43
publisher: Common Crawl
description: Wide crawl of the web for October 2019
operator: Common Crawl Admin (info@commoncrawl.org)
hostname: ip-10-67-67-173.ec2.internal
software: Apache Nutch 1.16 (modified, https://github.com/commoncrawl/nutch/)
robots: checked via crawler-commons 1.1-SNAPSHOT (https://github.com/crawler-commons/crawler-commons)
format: WARC File Format 1.1
conformsTo: http://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/