Nestable environments with parameters?
Hello again! I've actually got a solution to this problem, but I'm not particularly happy with it and would like to know if there's a better way -- or, for that matter, a more "standard ConTeXt" way -- of solving it. I'm building an environment that I'd like to be able to use in the following way: \setupmyenv[paramA=This] \startmyenv[paramA=That] A bit of text. \startmyenv More text. \stopmyenv \stopmyenv In particular, when it's used this way, I would like the inner environment to use the "This" version of paramA, rather than the "That" version. Moreover, since the parameter-handling code is (in the real example) fairly complicated, I'd like to reuse the same macro to handle it in both the \setupmyenv and the \startmyenv cases. I'm currently doing this in the following way: * \setupmyenv calls \getparameters[\??myenv][#1], where \??myenv was created with \definesystemvariable{myenv}. This creates a set of \@@myenvparamA and related macros containing all the global settings. * \startmyenv calls \copyparameters[\??myenv_][\??myenv][...], thereby copying all of the defaults into \@@myenv_paramA and so forth, which provide "local" values of all of the settings. * \startmyenv then calls \getparameters[\??myenv_][#1], thus overwriting the relevant local settings -- but not the global settings -- with the new values. The two \getparameters commands (and the stuff that surrounds them) is identical aside from "\??myenv" versus "\??myenv_", and thus can be enclosed in a shared macro. Also, since \startmyenv does all this in a group, the local settings on the inner environment don't contaminate the ones in the outer environment. This works, but I'm not especially happy with it -- the \copyparameters gets called every time the environment is started, and this seems rather a lot of excess work when most of the values don't change. It's also a source of programming errors, because I have to remember to update the list each time I change it. Any suggestions for a better solution? I couldn't find any environments in the ConTeXt core that do this sort of nesting.... Also, I'm trying to follow "ConTeXt standard style" as much as possible in this -- I'm hoping to write up a TUGboat article on it eventually -- and, while the trailing underscore works perfectly well to create a "local" version of a "global" variable, I'm wondering if there's a more standard way of doing that.... Thanks! - Brooks
Brooks Moses wrote:
Also, since \startmyenv does all this in a group, the local settings on the inner environment don't contaminate the ones in the outer environment.
If you can get away with destroying the local variables at the end of the expansion of \startmyenv, they you can remove the \copyparameters altogether by using grouping around the \setup command: \def\startmyenv[#1]% {\begingroup \setupmyenv[#1]% ... normal processing here ... \endgroup} Also, if you wanted to change 'only some' options runtime, then you could put those in a temporary namespace, and check their existance specifically instead of copying everything, but that's not what you want, I take it.
Any suggestions for a better solution? I couldn't find any environments in the ConTeXt core that do this sort of nesting....
I think you have found the right solution for the general problem. That is to say: every other thing I can come up with is a special case for optimization, instead of a significantly better approach. ;-) Cheers, Taco
participants (2)
-
Brooks Moses
-
Taco Hoekwater