[Tex/LaTex] exactly a hook in etoolbox

etoolboxhookspackage-writing

I understand well the \apptocmd and related tools of etoolbox package. However, I still not understand what \appto<hook>{<code>} and related do, even reading three time the handbook ?

Should package programmer define hook as a macro without argument (like \myhook) ? In this case, what is the difference with command patched by \apptocmd and related ?

Best Answer

The definition of \appto is

% etoolbox.sty, line 891:
\newrobustcmd{\appto}[2]{%
  \ifundef{#1}
    {\edef#1{\unexpanded{#2}}}
    {\edef#1{\expandonce#1\unexpanded{#2}}}}

The first argument should be a parameterless macro. Why not using \apptocmd? Because it is much less efficient and it may even not succeed if the replacement text has “non standard” tokens (for instance, characters with a different category code than usual).

Apart from this, the only other difference between \appto and \apptocmd is that the latter preserves the status of a \long (parameterless) macro, whereas \appto will always change it to non-\long.