2

A user of one of my classes exceptionally wants to modify something that is hard-coded in a \cs_new_protected:Nn function. I can give her the means to redefine this function using \cs_set_protected:Nn (between \ExplSyntaxOn and \ExplSyntaxOff); but it would be more practical to rely on a hook. Is this possible?

7
  • You can but if you are defining the command better to add a specific hook rather than rely on the generic hook mechanism to patch it. Commented Jun 13 at 8:15
  • My command is wonderful: apart from this exceptional request, there's no need to modify it ;) So what's the trick to patching it using the generic hook mechanism? Commented Jun 13 at 8:26
  • It should "just work" shouldn't? as long as you do it inside ExplSyntaxOn but haven't tried, perhaps I misunderstand the question. Commented Jun 13 at 8:30
  • 1
    well it depends on what must be changed inside the function. No generic hook can replace e.g. a string in the middle of some complicated code. Commented Jun 13 at 8:37
  • 2
    As you are using \cs_new_protected:Nn, this is presumably a code-level function: that really means you need a documented API to extend it
    – Joseph Wright
    Commented Jun 13 at 8:40

1 Answer 1

8

It should just work with no special things needed:

enter image description here

\documentclass{article}

\ExplSyntaxOn
\cs_new_protected:Nn\myfoo_test:n{aaa-#1-bbb}
\NewDocumentCommand\docversion{}{\myfoo_test:n}
\ExplSyntaxOff


\ExplSyntaxOn
\AddToHook{cmd/myfoo_test:n/before}{BAR~ADDED~HERE~}
\ExplSyntaxOff
\begin{document}


\docversion{abc}

\end{document}

But you shouldn't be patching L3 level commands in this way (and certainly not internal __ commands) If you are defining the command and want to allow an extension point you can add that as part of the definition, either a hook or a socket or simply a command initially empty that the user can redefine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .