0

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

\newenvironment{myenv}{
  \let\mycaption\myns@mycaption

  \begin{figure}[!h]
  \caption{???whatgoeshere???}
  \end{figure}
}{}

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

\begin{myenv}
  \mycaption{Hello world}
\end{myenv}

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

\newenvironment{myenv}{
  \let\mycaption\myns@mycaption

  \begin{figure}[!h]
  \if @mycaption is filled out
  \caption{@mycaption}
  \fi
  \end{figure}
}{}

Update

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

\newenvironment{myexample}{
  \let\a\...
  \let\b\...
  \let\c\...

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

  % place all of the b's here
  \b:all

  % place all of the c's here
  \c:all

  % place the rest of the a's here
  \a:n+1
}{}

And it would be used like:

\begin{myexample}
  \a{My first item}
  \a{My second item}
  \a{My third item...}
  \b{Foo}
  \c{1}
  \b{Bar}
  \c{2}
  \c{3...}
\end{myexample}

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

My first item
Foo
Bar
1
2
3...
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
1

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)

\documentclass{memoir}
\usepackage{etoolbox}
\newtoggle{first}
\toggletrue{first}

\newcommand\aaaList{}
\newcommand\aaa[1]{\listadd\aaaList{#1}}

\newcommand\typesetAitem[1]{
  #1\par
  \iftoggle{first}{
    do something after first item
    \togglefalse{first}
  }{}
  \par
}
\newcommand\typesetA{\forlistloop\typesetAitem\aaaList}

\begin{document}

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

\typesetA

\end{document}
  • 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.