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

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

so that when I do

My Text Here

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.

  • 1
    What do you want to go into the \marginnote?
    – StrongBad
    Commented 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
    Commented 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 Commented Feb 2, 2016 at 22:50

1 Answer 1


You can capture the contents of an environment using environ:

enter image description here



  % <some stuff here>
  % <other stuff here>


Here is some text.
My Text Here.  
Here is some more text.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .