# New environment with marginnote

I'm trying to make a new environment that uses marginnote like so:

\newenvironment{myname}
{ <some stuff> \marginnote{ }
{ } <other stuff> }


so that when I do

\begin{myname}
My Text Here
\end{myname}


I will see "My Text Here" in a marginnote.

The code, written as I have it, won't work though, since we have these awkward open and close braces for marginnote inside of the open/close braces for the code to be executed at the beginning/end of the new environment. Thanks for the help.

• What do you want to go into the \marginnote? Feb 2, 2016 at 21:28
• What's the purpose of the environment at all? What is somestuff supposed to do? Grabbing the environment body is no option here, I think. Perhaps an argument to the environment is of more use
– user31729
Feb 2, 2016 at 21:29
• I'd like to put into the margin some Python code using the minted package. I'll be doing this over and over again with custom options, hence the desire for a new environment Feb 2, 2016 at 22:50

You can capture the contents of an environment using environ:

\documentclass{article}

\usepackage{environ,marginnote}

\NewEnviron{myenv}{%
% <some stuff here>
\marginnote{\BODY}%
% <other stuff here>
}

\begin{document}

Here is some text.
\begin{myenv}
My Text Here.
\end{myenv}
Here is some more text.

\end{document}

• \ignorespacesafterend shouldn't be necessary Feb 2, 2016 at 23:21
• @egreg: In this context, or in general?
– Werner
Feb 2, 2016 at 23:31
• With \NewEnviron. I believe it's explained in the manual. Feb 2, 2016 at 23:35
• @egreg: I see now; there is a default [<final code>] set.
– Werner
Feb 2, 2016 at 23:59