I am wondering if you can "reopen" an environment to extend its definition, similar to how in programming languages like Ruby you can reopen a class/module anywhere in your code and extend its definition.
So for example, say you have this in one part of your document:
\newenvironment{myenv}{
\newcommand\a{}
}{}
Then later on in the document you do this:
\reopenenvironment{myenv}{
\newcommand\b{}
}{}
That way the end result is this:
\newenvironment{myenv}{
\newcommand\a{}
}{}
\reopenenvironment{myenv}{
\newcommand\b{}
}{}
Which should be functionally equivalent to:
\newenvironment{myenv}{
\newcommand\a{}
\newcommand\b{}
}{}
Wondering if anything like that exists in Latex. The reasoning is say you have a class with some generic definitions in it. You may want to reuse a specific diagram in various formats a few times, but it doesn't warrant adding it to the class definition. Instead you would just define it inline in your document and use it where right there where needed.
Another variation of this question (or another way this could potentially be accomplished) is by adding a new conditional block to an existing command. So say we had \a
as in:
\newenvironment{myenv}{
\newcommand\a[1]{
\ifthenelse{\equal{##1}{\string foo}}
{\typeout{foo}}
{}
}
}{}
Then you could extend the definition like this at runtime perhaps:
\reopenenvironment{myenv}{
\reopencommand\a[1]{
\ifthenelse{\equal{##1}{\string bar}}
{\typeout{bar}}
{}
\super{}
}
}{}
That way you can extend it at runtime so you can keep the API:
\begin{myenv}
\a{foo}
\a{bar}
\end{myenv}
Wondering if anything like this exists or if there is a normal way to accomplish this with latex.
I would like to be able to do the following:
\begin{athing}{label=baz}
\stuff
\stuff
\stuff
\stuff
\end{athing}
And then have it usable as:
\begin{myenv}
\a{baz}
\end{myenv}
Maybe this could somehow be done with a local key/value store. That is,
kvstore = {
foo: athing
bar: athing
baz: athing
}
\newcommand\a[1]{
kvstore[##1]
}
Maybe that sort of thing is possible, that would be another solution if it was possible.
So to summarize, I would like to be able to have an API:
myapi{somelabela}
myapi{somelabelb}
...
Such that it can be extended at runtime with more definitions. The 3 approaches outlined here are just initial thoughts, I am new to latex. Thank you so much for your help.
\newcommand\a
which will already generate errors as\a
is a predefined command in latex, so the initial environment does not work so it's hard to test extending it.\AtEndEnvironment
from theetoolbox
package.