# How do I wrap a macro definition in an environment?

I'm creating a style file, and want to store the body text of the abstract environment so that I can put it on my custom titlepage. I could have required that my users enter the abstract as

\customabstract{Abstract text goes here}


which I would have implemented as something like

\newcommand{\customabstract}[1]{\gdef\abstracttext{#1}}


However, I would prefer that the user is able to use environment notation,

\begin{customabstract}Abstract text goes here\end{customabstract}


To do this, I thought I could define something like

\newenvironment{customabstract}{\gdef\abstracttext\bgroup}{\egroup}


But this definition makes \abstracttext expand to \bgroup, which is not what I wanted. I tried to solve the problem by \expandafter,

\newenvironment{customabstract}{\expandafter\gdef\expandafter\abstracttext\bgroup}{\egroup}


but this results in a runaway error.

Is there any way to acheive what I want? Answers that helps me understand the inner workings of TeX (instead of just referring to a custom package) are preferred.

• See the environ package – daleif Oct 20 '15 at 10:55

You can't do

\gdef\abstracttext\bgroup...\egroup


because the braces around the replacement text of a macro must be explicit. Besides, TeX never does expansion when absorbing the replacement text of a macro, so \end{customabstract} would not be interpreted and, even if implicit delimiters were allowed, you'd get an incomplete definition anyhow.

The usual solutions for this are as follows:

1. making a box that's saved and then delivered at the appropriate spot; this is how the AMS classes do, for instance;

2. using the environ package, which is more flexible, because it allows formatting the text at point of use, rather than at point of call.

Here's an implementation of the second idea.

\usepackage{environ}

\NewEnviron{customabstract}{\global\let\abstracttext\BODY}


Explanation. With environ, the environment's contents is temporarily saved as the replacement text of \BODY`.

• Thanks for a thorough answer, which explains why my approach was not working. The environ package seems nice, I did not know about it before. – akvilas Oct 20 '15 at 11:51