1

I want to have some commentary text below the chapter name when in the table of contents. Following Chapter in ToC without page number I adapted this to create a \fakesection command to suppress the page number. It looks great (see rendering below) but it will not coexist with the hyperref package. The second pass of pdflatex fails like this:

[Loading MPS to PDF converter (version 2006.09.02).]
))) (/usr/share/texlive/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
(./test.out) (./test.out) (./test.toc
! Argument of \contentsline has an extra }.
<inserted text> 
                \par 
l.3 \contentsline
                  {chapter}{\numberline {2}EFGH}{5}{chapter.2}%
?

If I have just one chapter, this is the result instead:

[Loading MPS to PDF converter (version 2006.09.02).]
))) (/usr/share/texlive/texmf-dist/tex/latex/hyperref/nameref.sty
(/usr/share/texlive/texmf-dist/tex/generic/oberdiek/gettitlestring.sty))
(./test.out) (./test.out) (./test.toc)
Runaway argument?
! File ended while scanning use of \contentsline.
<inserted text> 
                \par 
l.23 \tableofcontents

?

Here's an MWE and the rendering of the TOC:

\documentclass[12pt]{book}
\usepackage{tocloft}
\usepackage{lipsum}
% Uncommenting this will result in ! Argument of \contentsline has an extra }.
%\usepackage[pdfusetitle,colorlinks]{hyperref}

\newcommand\fakesection[2][]{% \ifx&#1&% \fakesection[#2]{#2}% \else \section*{#2}% \sectionmark{#1}% \addtocontents{toc}{\protect\contentsline{section}{#1}{}}% \fi }

\renewcommand\cftchapdotsep{\cftdotsep}% \renewcommand\cftsecdotsep{200} \makeatletter \renewcommand\cftsecpagefont{@gobble} \makeatother

\begin{document} \tableofcontents

\chapter{ABCD} \fakesection{The First Four Letters} \lipsum[1-5] \chapter{EFGH} \fakesection{The Next Four Letters} \lipsum[6-10] \chapter{IJKL} \fakesection{Halfway Through} \lipsum[11-15] \chapter{MNOP} \fakesection{The Middle of the Alphabet} \lipsum[16-20] \chapter{QRST} \fakesection{Over the Hill of Letters} \lipsum[21-25] \chapter{UVWX} \fakesection{Almost Done} \lipsum[26-30] \chapter{YZ} \fakesection{The Two Stragglers} \lipsum[31-35] \end{document}

enter image description here

Frotz
  • 378
  • 2
  • 12
  • 1
    contentsline should have four arguments, and remove the \renewcommand\cftsecpagefont{\@gobble} (or put something in argument three that the @gobble can handle.) – Ulrike Fischer Mar 26 '21 at 22:52
  • This did the trick. Would you please do this again as an answer so you can get proper credit? – Frotz Mar 28 '21 at 05:41

1 Answers1

3

\contentsline should have four arguments, as hyperref requires this (it stores the destination name in the fourth argument). In a current LaTeX \contentsline has always four arguments, so it doesn't harm to use four arguments even if hyperref is not involved.

Also remove the \renewcommand\cftsecpagefont{\@gobble} or put something in the third argument of \contentsline that the \@gobble can actually handle.

Ulrike Fischer
  • 327,261