TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a global hook (=macro) that should be appended to whenever an environment is executed. (Here, defined by the \bhook-\ehook pair.) However, I was unable to derive a working solution.




% How to define these macros?

% Usage, result should be equivalent to
% \gappto{\myhook}{hook this!}
\bhook{hook this!}\ehook


Compilation of this code results in an error, as does replacing \bgroup/\egroup with \begingroup/\endgroup or




Is there a way to replace an environment with a command? No, the \NewEnviron from the environ package doesn't work here either. (At least for my special case -- I want to allow pretty much everything inside this environment.)

Is this a deficiency in \gappto, or why do the "usual tricks" not work? Would this be easier to achieve using \def and friends?

EDIT: To further complicate matters, the \bhook and \ehook commands are to be embedded in a custom environment:

share|improve this question
up vote 3 down vote accepted

Arguments to macros must be enclosed in explicit braces if they consist of more than one token (and are not “delimited” in the macro's definition, but \newcommand defined macros always require arguments in braces).

If you really want a syntax

\bhook Hook this!\ehook

you can say


so that the argument to \bhook is effectively delimited and ends at the first \ehook token at the same brace level.

A different approach with environ might be


so that

Hook this!

would be equivalent to the call above.

share|improve this answer
Thank you. The \long\def... works for the given example, but I don't seem to be able to use \bhook and \ehook in the begin and end blocks of a custom environment, such as \NewDocumentEnvironment{testhook}{}{\bhook}{\ehook}. Why? – krlmlr Jul 5 '12 at 10:43
@user946850 The argument to \bhook ends when TeX scans \ehook, which doesn't happen because what's seen is only \end{testhook}: TeX doesn't expand tokens when gathering an argument. – egreg Jul 5 '12 at 10:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.