ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Re: Test whether variable has been set
@ 2007-05-27 14:58 Elliot Clifton
  2007-05-27 15:55 ` Aditya Mahajan
  2007-05-27 18:15 ` Peter Rolf
  0 siblings, 2 replies; 6+ messages in thread
From: Elliot Clifton @ 2007-05-27 14:58 UTC (permalink / raw)
  To: ntg-context

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Test whether variable has been set
  2007-05-27 14:58 Test whether variable has been set Elliot Clifton
@ 2007-05-27 15:55 ` Aditya Mahajan
  2007-05-27 16:15   ` Hans Hagen
  2007-05-27 18:15 ` Peter Rolf
  1 sibling, 1 reply; 6+ messages in thread
From: Aditya Mahajan @ 2007-05-27 15:55 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Test whether variable has been set
  2007-05-27 15:55 ` Aditya Mahajan
@ 2007-05-27 16:15   ` Hans Hagen
  0 siblings, 0 replies; 6+ messages in thread
From: Hans Hagen @ 2007-05-27 16:15 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Test whether variable has been set
  2007-05-27 14:58 Test whether variable has been set Elliot Clifton
  2007-05-27 15:55 ` Aditya Mahajan
@ 2007-05-27 18:15 ` Peter Rolf
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Rolf @ 2007-05-27 18:15 UTC (permalink / raw)
  To: mailing list for ConTeXt users

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

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Test whether variable has been set
  2007-05-27 11:15 Elliot Clifton
@ 2007-05-27 13:26 ` Peter Rolf
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Rolf @ 2007-05-27 13:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi Elliot,

Elliot Clifton schrieb:
> Hi,
> 
> How can test whether a variable has been set? I couldn't find any
> reference to this in the wiki?
>
What do you mean by "set"?

I use

\doifnothing{\getvariable{foo}{bar}}
  {foo:bar contains nothing.\par}%
\doifsomething{\getvariable{foo}{bar}}
  {foo:bar contains something.\par}%
\doifelsenothing{\getvariable{foo}{bar}}
  {foo:bar contains nothing.\par}
  {foo:bar contains something.\par}%

to test, if a defined(!) variable contains something.

If you want to test if a variable is defined, you can use

\doifnotvariable{foo}{bar}
  {foo:bar is undefined.\par}%
\doifvariable{foo}{bar}
  {foo:bar is defined.\par}%
\doifelsevariable{foo}{bar}
  {foo:bar is defined.\par}
  {foo:bar is undefined.\par}%

Never used these macros, as I always define default values for my variables.


HTH, Peter


> TIA,
> 
> 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
> ___________________________________________________________________________________
> 

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Test whether variable has been set
@ 2007-05-27 11:15 Elliot Clifton
  2007-05-27 13:26 ` Peter Rolf
  0 siblings, 1 reply; 6+ messages in thread
From: Elliot Clifton @ 2007-05-27 11:15 UTC (permalink / raw)
  To: ntg-context

Hi,

How can test whether a variable has been set? I couldn't find any
reference to this in the wiki?

TIA,

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
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-05-27 18:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-27 14:58 Test whether variable has been set Elliot Clifton
2007-05-27 15:55 ` Aditya Mahajan
2007-05-27 16:15   ` Hans Hagen
2007-05-27 18:15 ` Peter Rolf
  -- strict thread matches above, loose matches on Subject: below --
2007-05-27 11:15 Elliot Clifton
2007-05-27 13:26 ` Peter Rolf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).