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:
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:\newenvironment{lrbox}[1]{% \setbox#1=\vbox\bgroup }{% \egroup }
Sadly, it doesn't work like that: not even\newcommand{\fred}[1]{...} \newenvironment{fredenv}{% \fred\bgroup }{% \egroup }
works. Putting stuff on the argument stack crucially requires real braces.\fred\bgroup ... \egroup
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:
With this definition, the body of the\usepackage{amsmath} ... \makeatletter \newenvironment{fredenv}% {\collect@body \fred}% {} \makeatother
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
doesn't work; the argument of... {\collect@body{\jim{arg1}}} ...
\
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
.
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=cmdasenv