Re: [NTG-context] Test whether variable has been set
Hi Peter, Thanks for your reply. I think I may have misunderstood the variable section on the wiki. I been using \setvariable to both declare and assign variables from my component files. When I should have been declaring them in the environment file first, right? \setvariable[namespace][key=DefaultValue], right? So how do I test for a (Default)Value? Sorry, I'm just beginning TeX programming I don't understand how to do this. Elliot
On Sun, 27 May 2007, Elliot Clifton wrote:
Hi Peter,
Thanks for your reply. I think I may have misunderstood the variable section on the wiki. I been using \setvariable to both declare and assign variables from my component files. When I should have been declaring them in the environment file first, right? \setvariable[namespace][key=DefaultValue], right? So how do I test for a (Default)Value? Sorry, I'm just beginning TeX programming I don't understand how to do this.
Elliot, You can use \doif{\getvalue{set}{var}}{value} for simple string comparisons. See http://wiki.contextgarden.net/System_Macros for basic flow control features of ConTeXt. It may also be possble to see if variables have changed using setups http://wiki.contextgarden.net/Inside_ConTeXt#Using_setups_for_namespaces but I have never personally used that. Aditya
Aditya Mahajan wrote:
On Sun, 27 May 2007, Elliot Clifton wrote:
Hi Peter,
Thanks for your reply. I think I may have misunderstood the variable section on the wiki. I been using \setvariable to both declare and assign variables from my component files. When I should have been declaring them in the environment file first, right? \setvariable[namespace][key=DefaultValue], right? So how do I test for a (Default)Value? Sorry, I'm just beginning TeX programming I don't understand how to do this.
Elliot,
You can use \doif{\getvalue{set}{var}}{value} for simple string comparisons. See http://wiki.contextgarden.net/System_Macros for basic flow control features of ConTeXt.
It may also be possble to see if variables have changed using setups http://wiki.contextgarden.net/Inside_ConTeXt#Using_setups_for_namespaces but I have never personally used that.
these test if the variable has been set (defined test): \doifelsevariable#1#2 \doifvariable#1#2 \doifnotvariable#1#2 (is not the same as test for empty) there is also: \getvariabledefault#1#2#3 with #3 being a default value for when undefined Hans -- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Elliot Clifton schrieb:
Hi Peter,
Thanks for your reply. I think I may have misunderstood the variable section on the wiki. I been using \setvariable to both declare and assign variables from my component files. When I should have been declaring them in the environment file first, right?
As long as you can garantee, that your variables are declared *before* they are read, things are ok. But an environment or extra file is IMO the cleanest way. Reading an undefined variable results in the \empty token. This can be problematic, if you expect a number; e.g. \dimexpr\getvariable{foo}{width}\relax. Anyway, it's no good idea to use something that is not defined. So you have to - define a variable, before you use it or - use the \doifnotvariable,\doifvariable and \doifelsevariable macros before you try to read a (maybe undefined) variable. I can't say much about the second one. Search the ConTeXt sources to get an idea. The \doifnothing,\doifsomething and \doifelsenothing macros just test, if the variable is empty or not. A variable can be defined with an empty value (\setvariable[foo][bar=]). I use this for automatic size calculations. If no default value is given, then the size is calculated in my macro.
\setvariable[namespace][key=DefaultValue], right? So how do I test for a (Default)Value?
\doifsamestring{\getvariable{foo}{bar}} {your_default_value} {equal}% \doifsamestringelse{\getvariable{foo}{bar}} {your_default_value} {equal} {unequal}% probably not the fastest variant. If I remember it right, I had some problems with \doif comparison (catcodes?).
Sorry, I'm just beginning TeX programming I don't understand how to do this.
No problem. If it's not in the WIKI, this is the place to ask. Best, Peter
Elliot ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : https://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________
participants (4)
-
Aditya Mahajan
-
Elliot Clifton
-
Hans Hagen
-
Peter Rolf