7

The code \AddToHook{env/myenv/begin} will run the code after the env group but before the beginning code is executed. How to run some code after the being code is executed?

See the following example

\documentclass{article}

\newenvironment{myenv}%
  {begin myenv\par}%
  {end myenv}
\AddToHook{env/myenv/before}{before hook\par}
\AddToHook{env/myenv/begin}{begin hook\par}
\AddToHook{env/myenv/end}{\par end hook}
\AddToHook{env/myenv/after}{\par after hook}

\begin{document}
\begin{myenv}
    This is a test
\end{myenv}
\end{document}

This will result in

before hook
begin hook
begin myenv
This is a test
end hook
end myenv
after hook

Is there a way to make the begin hook after begin myenv?

According to the answers, I'd like to expand on this question with the following additions:

  1. For a custom env, e.g., myenv defined above.
  2. For a predefined env, e.g., equation.
  3. For a predefined env with star, e.g., equation*.
6
  • 3
    Declare your own hook xyz/myenv/begin and use it in your definition with \UseHook. Then you can add code to this hook Commented May 6 at 11:43
  • @UlrikeFischer Looks like an answer (with the explanation of the idea)
    – Joseph Wright
    Commented May 6 at 12:08
  • I have add some more information on the questions. For a user defined env, the answers of @UlrikeFischer and @JohnKormylo all work well. And for a env like equation, @JohnKormylo's answer works. But how to do this for a star env, such as equation*?
    – xyz
    Commented May 7 at 5:30
  • The star * is part of the environment name, hence the underlying command name. \csletcs{oldequation*}{equation*} works, in which \csletcs needs etoolbox package. Commented May 7 at 5:50
  • To avoid an X-Y question, what kind of code do you want to add to the beginning of equation* env content? Commented May 7 at 6:05

2 Answers 2

11

Generic hooks can only be added at "generic" places that make sense for all environments. If you want a specific hook in a special environment declare a new hook and then use it:

\documentclass{article}
\NewHook{xyz/myenv/begin}
\newenvironment{myenv}%
  {\UseHook{xyz/myenv/begin}%
   begin myenv\par}%
  {end myenv}
\AddToHook{env/myenv/before}{before hook\par}
\AddToHook{env/myenv/begin}{begin hook\par}
\AddToHook{env/myenv/end}{\par end hook}
\AddToHook{env/myenv/after}{\par after hook}
\AddToHook{xyz/myenv/begin}{\par just before begin code hook!}

\begin{document}
\begin{myenv}
    This is a test
\end{myenv}
\end{document}

enter image description here

3
  • What if I would like to add a hook to an exist environment, such as equation?
    – xyz
    Commented May 6 at 13:03
  • Then ask the author of the environment for a suitable hook ... Commented May 7 at 8:44
  • AFAICS, this doesn't answer the question “Is there a way to make the begin hook after begin myenv?” But it is just a matter of moving \UseHook{xyz/myenv/begin} after begin myenv. Commented May 8 at 9:56
3

This is easier to do using the old school approach than using predefined hooks.

\documentclass{article}

\newenvironment{myenv}% original environment
  {begin myenv\par}%
  {\par end myenv}
\let\oldmyenv=\myenv% renamve original environment
\let\endoldmyenv=\endmyenv
\renewenvironment{myenv}% create new version
  {\oldmyenv inside myenv\par}%
  {\par inside myenv
  \endoldmyenv}

\begin{document}
\begin{myenv}
    This is a test
\end{myenv}
\end{document}
1
  • If this work depends on the concrete environment and its definition and the material you want to add. Commented May 7 at 8:46

You must log in to answer this question.

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