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&%
\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}

\renewcommand\cftsecpagefont{\@gobble}(or put something in argument three that the @gobble can handle.) – Ulrike Fischer Mar 26 '21 at 22:52