3

Consider the following MWE, in particular the lines having to do with the table of contents:

\documentclass[12pt]{report}
\begin{document}

\tableofcontents

\addtocontents{toc}{\protect\contentsline{chapter}{SomethingA}{SomethingB}} % uncommenting the following makes the output of TeXLive 2022 % match the output of previous versions: % \addtocontents{toc}{{}}

% The following is there just to create some content for the ToC. % \chapter{INTRODUCTION} Here is some text. \section{A new look} And here are more words. \chapter{HERE WE GO} Here is some text. \section{A second look} And here are more words. \end{document}

Here is the output. Note that it depends on the version of TeXLive. (All compilations were done on OverLeaf.)

enter image description here

The output of TeXLive 2022 can be fixed by uncommenting the line \addtocontents{toc}{{}}, as indicated. That line doesn't seem to have any effect on the output of TeXLive 2021 (and previous).

Questions

Why is this happening? True, it turns out it's easily fixable, but this behavior is disconcertingly unpredictable. What else will get messed up by whatever changes TeXLive 2022 introduced in how it handles \addtocontents?

  • \contentsline has four arguments now (see https://tex.stackexchange.com/a/590332/ for example). Check the .toc file to see the other entries, they all end with {}. So your manual toc entry should be \addtocontents{toc}{\protect\contentsline{chapter}{SomethingA}{SomethingB}{}}. – Marijn Dec 07 '22 at 17:54
  • @Marijn Thanks! One would have hoped that the changes would have been implemented in a way that doesn't break previous code. – linguisticturn Dec 07 '22 at 18:18
  • 1
    Normally backwards compatibility is preserved as much as possible, but I think in this case \contentsline is considered an 'internal' command that is normally only called by user commands like \chapter (whose implementation has been modified as well of course), and if you use \contentsline yourself then it is more on an 'at your own risk' basis. – Marijn Dec 07 '22 at 18:25

1 Answers1

2

Since October 2020 the LaTeX kernel defines \contentsline with four arguments. This was announced in LaTeX News issue 32:

Added a fourth empty argument to \contentsline

LaTeX’s \addcontentsline writes a \contentsline command with three arguments to the .toc and similar files. hyperref redefines \addcontentsline to write a fourth argument. The change unifies the number of arguments by writing an additional empty brace group. (github issue 370)

The Github issue (written by Ulrike Fischer) describes the reason for this change a bit more clearly:

This changes addcontentsline so that it always writes 4 argument, to improve compability with hyperref.

While in October 2020 the extra argument was added by code that writes \contentline statements, the fourth argument was not actually part of/parsed by \contentsline itself and could still be left out entirely. When it was included as an empty brace group {} it was discarded, which is the normal behavior for empty brace groups anywhere in the code. This behavior was changed in the November 2021 release:

Pick up all arguments to \contentsline

A \contentsline command in the .toc file is always followed by four arguments, the last one being empty except when using the hyperref package. The \contentsline command itself only used the first three arguments and it relied on the fourth being empty (and thus doing no harm). But this assumption is not always correct: e.g., if you at first decide to load hyperref but then later you remove this loading from the preamble. So now all four arguments are picked up, with the fourth being saved away so that it can be used by hyperref. (github issue 633)

Therefore, from this release onwards the fourth argument is mandatory.

This means your manual TOC entry should be as follows in current versions of LaTeX:

\addtocontents{toc}{\protect\contentsline{chapter}{SomethingA}{SomethingB}{}}

For other changes see the LaTeX News overview at https://www.latex-project.org/news/latex2e-news/.

Marijn
  • 37,699
  • Note that LaTeX itself (through \chapter etc) also writes a percent character at the end of \contentsline to prevent unwanted spacing in the TOC. This is however a bit tricky to do manually and in the current situation it does not seem to create unwanted behavior, so I left it out. – Marijn Dec 07 '22 at 18:41
  • Interesting that this only started creating problems with my code with the 2022 release, but not with the 2020 and 2021 releases, even though the fourth argument for \contentsline had been introduced already in the 2020 release. – linguisticturn Dec 07 '22 at 19:22
  • @linguisticturn the argument became mandatory a year later, mid-November 2021 (see edit). By then TeX Live 2021 was already released. So the first version that reflected the change fully is TL2022. Similarly the writing of the extra argument was not included in TL2020 (released in April 2020) but started from TL2021. – Marijn Dec 07 '22 at 20:15