# Is there a way to replace an environment with a command?

Under certain conditions (triggered by selection of a package option) I would like to change specific environments into margin notes. However marginnote is not itself an environment, but a command that takes its contents as an argument. So what I want to do is replace

\begin{someenvironment}
...
\end{someenvironment}


with

\marginnote{
...
}


but I clearly cannot do this with something like

\renewenvironment{someenvironment}
{\marginnote{ }
{ }}


What is the correct way to replace an environment with a command that takes the environment's contents as an argument?

-

The environ package may be of help here. You define an environment using

\NewEnviron{<env>}{%
% environment contents
}


and the contents of the environment is available in the macro \BODY. Here's a minimal example:

\documentclass{article}
\usepackage{marginnote}% http://ctan.org/pkg/marginnote
\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron{someenvironment}{%
\marginnote{\BODY}% Place contents of <someenvironment> in a \marginnote
}
\begin{document}
Here is some text with a margin note.
\begin{someenvironment}
This is a margin note.
\end{someenvironment}
\end{document}
​


\NewEnviron has a macro-like look to it, so the usage should be fairly intuitive.

-
Thanks, that worked, thigh it ended up begin a bit more complicated in my case. I'm redefining an already defined environment, and end up with a lot of floats, so I had to \usepackage[maxfloats=36]{morefloats} and \let\existingenv ... \NewEnviron{existingenv}.... –  raxacoricofallapatorius Feb 21 '12 at 22:25