5

\RequirePackage is supposed to work before \documentclass and, indeed, the following MCE works like a charm:

\RequirePackage{url}
\documentclass{article}
\begin{document}
\url{foo}
\end{document}

But, in the case of the tocloft package, this doesn't work. Indeed, the following MCE fails to compile with the following error message:

! LaTeX Error: Command \cftdot undefined.

\RequirePackage{tocloft}
\documentclass{article}
\renewcommand{\cftdot}{\ensuremath{\ast}}
\begin{document}
Foo.
\end{document}

What's going on?

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85

1 Answers1

6

Quite at the start of toclosft.sty (lines 36-45) you find

\@ifundefined{chapter}{%
  \@cfthaschapterfalse
  \@ifundefined{section}{%
    \PackageWarning{tocloft}%
      {I don't recognize any sectional divisions so I'll do nothing}
    \renewcommand{\@cftquit}{\endinput}
    }{\PackageInfo{tocloft}{The document has section divisions}}
  }{\@cfthaschaptertrue
    \PackageInfo{tocloft}{The document has chapter divisions}}
\@cftquit

Of course, loading the package before \documentclass means that there are no \sections & Co. Indeed you first get the warning

Package tocloft Warning: I don't recognize any sectional divisions so I'll do
nothing on input line 44.

Then \@cftquit is executed, which has been defined to be \endinput. No further code from the tocloft package is run, and you never get to the definition of \cftdot (which is on line 162).

campa
  • 31,130
  • Indeed, thanks. Too bad tocloft doesn't check for this later, e.g. \AtBeginDocument: it would have been helpful for classes which load packages that need tocloft to be loaded before them, such as etoc. – Denis Bitouzé Oct 07 '19 at 14:44