Very behind on taking care of the Herries Press packages (PDF) which I’m maintaining. Apologies to all who have emailed with suggestions, comments, and questions, and to whom I’ve unfortunately not been able to respond.

Funny request today: how can I remove subsection numbers from the Table of Contents without removing their entries altogether? LaTeX’s tocdepth counter controls which entries will appear in the ToC, but has nothing to say about their formatting. Adjusting LaTeX’s ToC formatting yourself is of course possible, but the tocloft package makes things a bit easier.

Still, in this case it doesn’t provide an easy interface, since who in their right mind would want to number a subsection but not include that number in the ToC? (You can’t fight stylistic decisions like this sometimes—just go with the flow.) Even with tocloft, it’s still necessary to write some internal code — and caveat emptor as always in such a case because you never know when the package might change its internal commands behind your back!

In this case, the trick is to read the tocloft documentation and notice that it provides hooks for inserting material before and after the number of a section entry in the ToC. For subsections, these commands, respectively, are \cftsubsecpresnum and \cftsubsecaftersnum. E.g., write

\renewcommand\cftsubsecaftersnum{!}

to have a bang after every subsection number. (Presumably, a style or spacing change would be more common here.)

Aha! The intrepid LaTeX programmer at this point might suggest something like this:

\def\cftsubsecpresnum#1\cftsubsecaftersnum{}

That is, when the pre-hook before the number executes, read everything up to and including the after-hook, and do nothing with it. (If the definition was {#1} instead, then it would typeset as usual.)

Well, give it a try like I did, and you’ll discover it fails miserably. Back to the drawing board—time to read the package source. The issue stems from the fact that LaTeX uses one command only to typeset each line of a ToC, so this command must be completely general whether it’s a chapter or section or subsection that it’s dealing with.

Internally, the number in the ToC is typeset using something like

\@cftbsnum « the number itself » \@cftasnum

where \@cftnbsum and \@cftasnum are \let equal to their appropriate high level definitions \cft…presum and \cft…aftersnum as necessary for the context. Therefore, any look-ahead-and-gobble command must be defined specifically to look ahead for the internal version of the hook rather than the high-level name for it given in the documentation.

Et voila:

\usepackage{tocloft}
\makeatletter
\def\cftsubsecpresnum#1\@cftasnum{}
\makeatother

I still don’t think this is very useful, but it’s a nice trick.


Hooks are funny in this way. If you define them to be defined by commands with an argument, such as

\cftsubsecsnumhook{ « the actual number here » }

it’s hard to do parsing on it that involves scanning ahead for the contents of the number. On the other hand, in this case where the hook has the form where it’s surrounded by a pre-hook and a post-hook, it’s hard to grab the whole argument and do something with it since the internal post-hook might not be very user-accessible, as in the example above.

I don’t know if there’s a way to structure hooks in any better way, however. One possibility I can think of would be to have a generic ‘end hook’ token that could always be read until, such as

\cftsubsecpresnum « the actual number here » \cftendhook \cftsubsecaftersnum

where \cftendhook was simply a no-op. There would be some subtle issues with nesting that would be somewhat painful to work around.

Maybe it would be better to have the number braced as an argument and instead of having \cftsubsecpresnum be a no-op by default it could be an identity function: (\def\cftsubsecpresnum#1{#1})

\cftsubsecpresnum { « the actual number here » } \cftsubsecaftersnum

Does anyone know of any best practices here?