latex2e icon indicating copy to clipboard operation
latex2e copied to clipboard

Is it actually true that every command `\⟨name⟩` offers now two associated hooks to which code can be added using `\AddToHookWithArguments` and `\AddToHookNextWithArguments`?

Open dbitouze opened this issue 1 year ago • 6 comments

The ltcmdhooks-doc reads:

This file implements generic hooks for (arbitrary) commands. In theory every command \⟨name⟩ offers now two associated hooks to which code can be added using \AddToHook\AddToHookNext, \AddToHookWithArguments, and \AddToHookNextWithArguments

So I naïvely thought e.g. \AddToHookWithArguments{cmd/⟨name⟩/before}{...} might apply to ⟨name⟩=section.

But, IIUC, not every command \⟨name⟩ offers these two associated hooks to which code can be added using \AddToHookWithArguments and \AddToHookNextWithArguments: in particular, not all (or any of the?) commands documented in e.g. LaTeX2e: An unofficial reference manual are concerned and only the ones documented in source2e.pdf are. If so, it is worth IMO to add this precision in the documentation.

dbitouze avatar Oct 21 '24 07:10 dbitouze

It's one of the limitations of command hooks, see texdoc ltcmdhooks-doc, sec. 2.2 "Commands that look ahead":

Some commands are defined in different “steps” and they look ahead in the input stream to find more arguments. If you try to add some code to the cmd/⟨name⟩/after hook of such command, it will not work, and it is not possible to detect that programmatically, so the user has to know (or find out) which commands can or cannot have hooks attached to them. One good example is the \section command. You can add something to the cmd/section/before hook, but if you try to add something to the cmd/section/after hook, \section will no longer work. That happens because the \section macro takes no argument, but instead calls a few internal LaTeX macros to look for the optional and mandatory arguments. By adding code to the cmd/section/after hook, you get in the way of that scanning.

muzimuzhi avatar Oct 21 '24 09:10 muzimuzhi

It's one of the limitations of command hooks, see texdoc ltcmdhooks-doc, sec. 2.2 "Commands that look ahead":

Okay but, in my question on TeX.SE, I added something only to the cmd/section/before hook.

dbitouze avatar Oct 21 '24 09:10 dbitouze

As Ulrike Fischer commented in your TeX.SE question, that's because "\section has no arguments at all."

Looks like the words in sec. 2.2 "Commands that look ahead" can be refined.

muzimuzhi avatar Oct 21 '24 09:10 muzimuzhi

As Ulrike Fischer commented in your TeX.SE question, that's because "\section has no arguments at all."

Okay but, once again, LaTeX2e: An unofficial reference manual contains plenty of commands that, at a first glance, do have arguments (and \section is one of them). Hence how a user could proceed to know which of them do actually have arguments, if he wants to play with \AddToHookWithArguments{cmd/⟨name⟩/before}{...}?

dbitouze avatar Oct 21 '24 09:10 dbitouze

Command hooks are different to other generic hooks. They are not built-in but normally have to be patched into the commands. That makes them more fragile and also more difficult to use. E.g. with french this patching can easily fail as it changes catcodes of various chars. In a class I wouldn't use them to adapt section. Redefine the command properly, then you can also declare and add the code hook explicitly.

u-fischer avatar Oct 21 '24 11:10 u-fischer

@u-fischer In fact, my initial concern was with biblatex's \addbibresource, not with \section: I gave an MCE with the latter in order to make my question less specific.

Anyway, the ltcmdhooks-doc deserves improvements IMO.

dbitouze avatar Oct 21 '24 12:10 dbitouze