Yesterday I was reading the etoolbox
documentation where there are defined some new document commands such as:
\Before<Begin/End>Environment{<envir>}{<code>}
%
\At<Begin/End>Environment{<envir>}{<code>}
%
\After<Begin/End>Environment{<envir>}{<code>}
Which I found very useful in dealing fine-tuning macro expansion inside or outside specific environments.
The main question however hit me when I wanted to define a new command, based on the above, that looks like this:
\If<Before/At/After><Begin/End>Environment{<envir>}{<code>}{%
<yes>%
}{%
<no>%
}
The main action of this code is that it checks if a specific control sequence name or macro/code <code>
appears <Before/At/After><Begin/End>
the environment
and then applies the code if it is met the condition stated, and does else otherwise. This can be similar to the standard if-clause conditional very well known inside plain TeX (maybe some TeXpert could put an eye to this macro).
The main reason on why it reads the control sequence name it's that if there is some macro behind a specific environment, it doesn't expand its arguments and keeps reading it as a csname
.
So my question can be expressed as: how this macro can be created? is there some command that simulates its action that I'm not aware of?
I basically couldn't find any documentation or source even remotely related to this application on macro setting.
\begin
and\end
. So you can put a simple if-clause inside the second mandatory argument of\At<Begin/End>Environment{<envir>}{<code>}
.\mymacro
is right beforelistings
, but I do not know how this could be implemented. I'm saying this because if I'd do\ifdefined\mymacro\else...
would be wrong because\mymacro
is always defined.<code>
insideIf<Before/At/After><Begin/End>Environment
has to be verified if it is immediately before/after my environment, without any\par
that separates it. From your third option (I'm so sorry) I didn't understand well though.\Before<Begin/End>Environment{<envir>}{<code>}
I would have assumed that you were asking how <code> could check that it was being run by that command in (for example)\Before{listings}
but I don't think you mean that (so etoolbox probably isn't relevant to your question?)