[NTG-context] Test whether variable has been set

Peter Rolf indiego at gmx.net
Sun May 27 20:15:47 CEST 2007


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 at 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
> ___________________________________________________________________________________
> 



More information about the ntg-context mailing list