Expl3 – Why Cannot Hooks Be Added to Command Names with Underscore?

expl3hooks

Consider the following example, which tries to add some code to a command's hook. If I replace g__mytest_tl to simply mytest then everything works fine. Does this mean that the hook system do not support command names with underscore?

\documentclass{article}

\begin{document}

\ExplSyntaxOn

\tl_new:N \g__mytest_tl

\hook_gput_code:nnn { cmd/g__mytest_tl/after } { test }
  {
    Test~text
  }

\tl_use:c { g__mytest_tl }

\ExplSyntaxOff

\end{document}

Best Answer

If you type h at the error message you will get more info:

! LaTeX hooks Error: Generic hooks cannot be added to '\g__mytest_tl'.

For immediate help type H <return>.
 ...                                              
                                                  
l.66   }
        
? h

You tried to add a hook to '\g__mytest_tl', but LaTeX was unable to patch the
command because it is a private expl3 macro.

Your command starts with \g__ and that marks it as private. A generic cmd-hook is a tool to change commands from another package and so by design it doesn't touch private commands.

If the variable is from your package you don't need the slower hook system you can simply append the code:

\documentclass{article}
\begin{document}

\ExplSyntaxOn

\tl_new:N \g__mytest_tl
\tl_gput_right:Nn\g__mytest_tl  { Test~text }
\tl_use:c { g__mytest_tl }

\ExplSyntaxOff

\end{document}