In learning how to use custom environments, I would like to try to do the following. Here is my setup:



In the ???whatgoeshere??? block, I would like to plugin the value of \mycaption. So for example say this is how it's used:

  \mycaption{Hello world}

The question is, how you plug that into the \caption{...}. Something like:


  \if @mycaption is filled out


Basically, I want to take the contents of any command used inside of an environment, and place it somewhere within the newenvironment command. So


  % place the first `a` here, if it was used in the environment

  % place all of the b's here

  % place all of the c's here

  % place the rest of the a's here

And it would be used like:

  \a{My first item}
  \a{My second item}
  \a{My third item...}

But the output (because of the \newenvironment definition) would be:

My first item
My second item
My third item...

Wondering how to accomplish this sort of behavior. This way, the usage of the myexample wouldn't care about the order of the operations, you just put \a \b, etc. wherever. Then the \newenvironment code would place them in the appropriate positions.

Not even sure how to get started in placing the values in different positions. Once that makes sense then it should probably be straightforward to figure out the details of placing the first/nth-one wherever.

  • 1
    As always on this site, please provide a full minimal example. Besides that, I do not understand what it is you're trying to do – daleif Mar 22 '18 at 13:57
  • This seems related tex.stackexchange.com/questions/14390/… – Lance Pollard Mar 22 '18 at 14:01
  • You just make macros that store the data, and then typeset it at the end part of the env. The hard part is of course to figure out a good way to store this, does all the \b and \c data come before the second \b is typeset? You could probably use lists from the etoolbox package to implement this, looping over the a list and determining when you are typesetting the first element and at what post loop over the b and c lists. It can probably be done neatly in expl3 – daleif Mar 22 '18 at 14:14
  • That sounds interesting, would love to understand more how to implement what you're describing. – Lance Pollard Mar 22 '18 at 14:17
  • @daleif this seems along the lines of what you're describing ctan.math.illinois.edu/macros/latex/contrib/getitems/… – Lance Pollard Mar 22 '18 at 14:26

Here is a starting point (this site is not a do it for me service), see the etoolbox manual for details. Here I'm not even using an environment (the memoir class is irrelevant, it is just the default class in my editor)



    do something after first item


\aaa{Test 1}
\aaa{Test 2}
\aaa{Test 3}


  • Beautiful! This is very helpful, thank you. There is a lot of clever stuff going on there. – Lance Pollard Mar 22 '18 at 14:34
  • @LancePollard for notive LaTeX programmers etoolbox and xparse are packages worth taking a loot at. – daleif Mar 22 '18 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.