Collect command arguments in an environment

The inquisitive user, who examines the code of the lrbox environment, will find that, stripped of considerable complication (which arises from the internal workings of LaTeX), the environment consists of:

\newenvironment{lrbox}[1]{%
  \setbox#1=\vbox\bgroup
}{%
  \egroup
}
Which (even though it doesn't work as written here) might cause optimism that one might redefine a command as an environment using some elaboration of:
\newcommand{\fred}[1]{...}
\newenvironment{fredenv}{%
  \fred\bgroup
}{%
  \egroup
}
Sadly, it doesn't work like that: not even
\fred\bgroup ... \egroup
works. Putting stuff on the argument stack crucially requires real braces.

To use an environment to collect the argument of a command, one really needs to store the body of the environment separately. Several packages use such a technique, but the amsmath package makes it half-visible, as an internal command. Use it as:

\usepackage{amsmath}
...
\makeatletter
\newenvironment{fredenv}%
  {\collect@body \fred}%
  {}
\makeatother
With this definition, the body of the fredenv will be passed as an argument to the command \fred.

Note, however, that there's a tendency for stray spaces to appear in the argument. Judicious use of \ignorespaces before the argument, and \unskip after the argument, in the macro \fred, may be valuable.

If you want a command \jim{arg1}{arg-to-collect}, the obvious

...
{\collect@body{\jim{arg1}}}
...
doesn't work; the argument of \collect@body has to be a single token. You achieve what you need by:
\makeatletter
\newcommand{\jimtemp}{}
\newenvironment{jimenv}[1]%
  {%
    \renewcommand\jimtemp{\jim{#1}}
    \collect@body \jimtemp}%
  {}
\makeatother

Unfortunately, while the construction may be useful in some instances, it does not provide a means of avoiding restrictions on the use of \verb in command arguments: the whole of the environment's body passes through the argument of a command embedded in \collect@body.

amsmath.sty
Distributed as part of the AMSLaTeX bundle, macros/latex/required/amslatex (zip, browse)

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=cmdasenv