7

I would like to copy the content of an environment so that I can use it somewhere else. My main purpose is to print it inside a tooltip note.

My idea was to use the environ package:

\NewEnviron{foo}
{
  \xdef\bidule{\BODY}
  %more stuff
}

But when I use \bidule I get an error:

 Undefined control sequence ....
 leading text: \end{foo}
 Undefined control sequence \endgroup.
 leading text: \end{foo}
 Missing { inserted.
 leading text:   \bidule
main.tex:19: File ended while scanning text of \errhelp.

If my understanding is correct, I cannot use \def because \BODY won't be defined when I will use \foo. Is there a way to achieve my goal?

1 Answers1

11

You don't want to do a full expansion of \BODY, but just one level:

\expandafter\gdef\expandafter\bidule\expandafter{\BODY}

or

\xdef\bidule{\unexpanded\expandafter{\BODY}}
egreg
  • 1,121,712
  • No, I made a mistake. However, I don't really understand why I want only one level of expansion. Thanks for you answer ;) . – Saroupille Feb 19 '19 at 23:06
  • 2
    @Saroupille Suppose the environment contains \textbf: then \xdef will die horribly. If it contains \mbox{a} you want \bidule to contain \mbox{a}, not \unhbox \voidb@x\hbox{a} – egreg Feb 19 '19 at 23:07
  • @saroupille Do you want to grab the contents of environment foo in \bidule or is \BODY defined somewhere else? – Eric Domenjoud Feb 20 '19 at 09:43
  • I am not sure I understand your question. I just want to have a command (variable) \bidule that contains the content of the environment \foo. The environ package stores this content in the command \BODY. But I think I misunderstood your question. – Saroupille Feb 20 '19 at 10:29
  • @Saroupille My bad. I didn't read carefully your question and did not notice that you already use the environ package. The answer of egreg is all you need. – Eric Domenjoud Feb 20 '19 at 11:17