# How to create an environment that embeds an environment inside a macro?

Let's say I have an environment myenvironment and I have the following code :

\makebox[\length][c]{%
\begin{myenvironment}
content
\end{myenvironment}
}


I want to create an environment foo that would do the same but simply with

\begin{foo}{\length}
content
\end{foo}


I know how to pass \length to foo. The problem is the command makebox (or any command that would delimit myenvironment). I attempted this naive approach :

\newenvironment{foo}{%
\makebox[\length][c]{
\begin{myenvironment}
}{%
\end{myenvironment}}
}


which of course doesn't work because you can't have { and } by themselves in a block.

I found this similar question but I am not sure it applies to my problem: How to define an environment that begins with a node? . And I don't really understand the validated answer.

So how would I go about to create the environment foo ? I would prefer a solution without using the package environ. If useful for a solution, I am using the xparse and etoolbox packages.

The trick is using lrbox:

\newsavebox{\foobox}

\newenvironment{foo}
{% start
\begin{lrbox}{\foobox}%
\begin{myenvironment}%
}
{% end
\end{myenvironment}%
\end{lrbox}%
\makebox[\length][c]{\usebox{\foobox}}%
}

• Thanks, this works and now I know how to save stuff inside boxes. Mar 5 '16 at 13:57