[NTG-context] How to define a command...

2012-01-05 Thread Otared Kavian
Hi all,

I need to define numerical coefficients (essentially at random) and then use 
them with commands, say like \CoeffAlpha, \CoeffBeta, and so on.
More precisely, how can I define the command \RandomCoeff, so that 
\RandomCoeff{Beta}{1}{10} yields a command named \CoeffBeta, which is a random 
number chosen between 1 and 10?

I tried to use the following approach, but could not make it work:

%%% begin random-coeff.tex
\setuprandomize[2012] % set a seed 

\ctxlua{CoeffAlpha = math.random(1,10) ;}
\def\CoeffAlpha{\ctxlua{tex.print(CoeffAlpha)}}

%
\define[3]\RandomCoeff{%
\ctxlua{a = math.random(#2,#3)}
%   \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
work as expected...
}

\starttext
\CoeffAlpha

\RandomCoeff{Beta}{1}{10}

\ctxlua{tex.print(a)}

%\CoeffBeta

\stoptext
%%% end random-coeff.tex

Thanks in advance for any suggestion or hint.
Best regards: OK

___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Meer, H. van der
Reccently I did something random in ctxlua. I post it in the hope it will be 
useful.

Hans van der Meer

\startluacode
-- Define our namespace as hvdm 
hvdm = hvdm or {} 

-- Return random series of numbers 1..n depending on the number of 
arguments
function hvdm.randomseries (n, straight)
local done = {}
for i = 1, n do
done[i] = i
end
if straight == false then
return table.concat(done,,)
end
local result = 
for i = n, 2, -1 do
local r = math.random(i)
result = result .. done[r] .. ,
table.remove(done,r)
end
result = result .. done[1]
return result
end
\stopluacode
\def\RandomSeed#1{\directlua{math.randomseed(#1)}}
\def\RandomValue{\directlua{tex.print(math.random())}}
\def\RandomRange#1{\directlua{tex.print(math.random(#1))}}
\def\RandomSeries[#1]#2{\ctxlua{tex.print(hvdm.randomseries(#2,#1))}%


On 5 jan. 2012, at 21:05, Otared Kavian wrote:

 Hi all,
 
 I need to define numerical coefficients (essentially at random) and then use 
 them with commands, say like \CoeffAlpha, \CoeffBeta, and so on.
 More precisely, how can I define the command \RandomCoeff, so that 
 \RandomCoeff{Beta}{1}{10} yields a command named \CoeffBeta, which is a 
 random number chosen between 1 and 10?
 
 I tried to use the following approach, but could not make it work:
 
 %%% begin random-coeff.tex
 \setuprandomize[2012] % set a seed 
 
 \ctxlua{CoeffAlpha = math.random(1,10) ;}
 \def\CoeffAlpha{\ctxlua{tex.print(CoeffAlpha)}}
 
 %
 \define[3]\RandomCoeff{%
   \ctxlua{a = math.random(#2,#3)}
 % \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
   }
 
 \starttext
 \CoeffAlpha
 
 \RandomCoeff{Beta}{1}{10}
 
 \ctxlua{tex.print(a)}
 
 %\CoeffBeta
 
 \stoptext
 %%% end random-coeff.tex
 
 Thanks in advance for any suggestion or hint.
 Best regards: OK
 
 ___
 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  : http://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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Peter Münster
On Thu, Jan 05 2012, Otared Kavian wrote:

 \define[3]\RandomCoeff{%
   \ctxlua{a = math.random(#2,#3)}
 % \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...

\setuprandomize[2012]
\define[3]\RandomCoeff{%
  \expandafter\def\csname 
Coeff#1\endcsname{\ctxlua{tex.print(math.random(#2,#3))}}}
\starttext
\RandomCoeff{Alpha}{1}{10}
\RandomCoeff{Beta}{2}{20}
\CoeffAlpha
\CoeffBeta
\stoptext

-- 
   Peter
___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Wolfgang Schuster

Am 05.01.2012 um 21:17 schrieb Peter Münster:

 On Thu, Jan 05 2012, Otared Kavian wrote:
 
 \define[3]\RandomCoeff{%
  \ctxlua{a = math.random(#2,#3)}
 %\csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
 
 \setuprandomize[2012]
 \define[3]\RandomCoeff{%
  \expandafter\def\csname 
 Coeff#1\endcsname{\ctxlua{tex.print(math.random(#2,#3))}}}

\setvalue{Coeff#1}{…}

Wolfgang
___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Wolfgang Schuster

Am 05.01.2012 um 21:11 schrieb Meer, H. van der:

 Reccently I did something random in ctxlua. I post it in the hope it will be 
 useful.
 
 Hans van der Meer
 
 \startluacode
   -- Define our namespace as hvdm 
   hvdm = hvdm or {} 


Don’t use a global namespace, you can use “userdata”.

userdata  = userdata  or { }
userdata.hvdm = userdata.hvdm or { }

Wolfgang
___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Otared Kavian
Thanks Hans, in another problem I was looking for  commands such as 
\RandomRange, \RandomSeries: I am going to use your ideas!
It is incredible that on this list one gets solutions even for problems one 
didn't dare to ask :-)

Best regards: OK

On 5 janv. 2012, at 21:11, Meer, H. van der wrote:

 Reccently I did something random in ctxlua. I post it in the hope it will be 
 useful.
 
 Hans van der Meer
 
 \startluacode
   -- Define our namespace as hvdm 
   hvdm = hvdm or {} 
 
   -- Return random series of numbers 1..n depending on the number of 
 arguments
   function hvdm.randomseries (n, straight)
   local done = {}
   for i = 1, n do
   done[i] = i
   end
   if straight == false then
   return table.concat(done,,)
   end
   local result = 
   for i = n, 2, -1 do
   local r = math.random(i)
   result = result .. done[r] .. ,
   table.remove(done,r)
   end
   result = result .. done[1]
   return result
   end
 \stopluacode
 \def\RandomSeed#1{\directlua{math.randomseed(#1)}}
 \def\RandomValue{\directlua{tex.print(math.random())}}
 \def\RandomRange#1{\directlua{tex.print(math.random(#1))}}
 \def\RandomSeries[#1]#2{\ctxlua{tex.print(hvdm.randomseries(#2,#1))}%
 
 
 On 5 jan. 2012, at 21:05, Otared Kavian wrote:
 
 Hi all,
 
 I need to define numerical coefficients (essentially at random) and then use 
 them with commands, say like \CoeffAlpha, \CoeffBeta, and so on.
 More precisely, how can I define the command \RandomCoeff, so that 
 \RandomCoeff{Beta}{1}{10} yields a command named \CoeffBeta, which is a 
 random number chosen between 1 and 10?
 
 I tried to use the following approach, but could not make it work:
 
 %%% begin random-coeff.tex
 \setuprandomize[2012] % set a seed 
 
 \ctxlua{CoeffAlpha = math.random(1,10) ;}
 \def\CoeffAlpha{\ctxlua{tex.print(CoeffAlpha)}}
 
 %
 \define[3]\RandomCoeff{%
  \ctxlua{a = math.random(#2,#3)}
 %\csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
  }
 
 \starttext
 \CoeffAlpha
 
 \RandomCoeff{Beta}{1}{10}
 
 \ctxlua{tex.print(a)}
 
 %\CoeffBeta
 
 \stoptext
 %%% end random-coeff.tex
 
 Thanks in advance for any suggestion or hint.
 Best regards: OK
 
 ___
 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  : http://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  : http://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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Otared Kavian
Thanks Peter,

I had tried \expandafter\xdef, \edef, \gdef, but not \expandafter\def… 
Thanks for your quick answer: that's exactly what I was looking for.

Best regards: OK

On 5 janv. 2012, at 21:17, Peter Münster wrote:

 On Thu, Jan 05 2012, Otared Kavian wrote:
 
 \define[3]\RandomCoeff{%
  \ctxlua{a = math.random(#2,#3)}
 %\csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
 
 \setuprandomize[2012]
 \define[3]\RandomCoeff{%
  \expandafter\def\csname 
 Coeff#1\endcsname{\ctxlua{tex.print(math.random(#2,#3))}}}
 \starttext
 \RandomCoeff{Alpha}{1}{10}
 \RandomCoeff{Beta}{2}{20}
 \CoeffAlpha
 \CoeffBeta
 \stoptext
 
 -- 
   Peter
 ___
 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  : http://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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Otared Kavian
Thanks for Wolfgang: your solution, that is
\setuprandomize[2012]
\define[3]\RandomCoeff{%
\setvalue{Coeff#1}{\ctxlua{tex.print(math.random(#2,#3))}}}

works indeed and it is somehow simpler than using \expandafter\csname, and as a 
matter of fact, after reading your message I searched in the ConTeXt manuals 
written by Hans, and found that \setvalue is defined on page 93 of 
module-01.pdf as 
\def\setvalue#1{\expandafter \def\csname#1\endcsname}
so it is better to use it.

Best regards: OK

On 5 janv. 2012, at 21:18, Wolfgang Schuster wrote:

 
 Am 05.01.2012 um 21:17 schrieb Peter Münster:
 
 On Thu, Jan 05 2012, Otared Kavian wrote:
 
 \define[3]\RandomCoeff{%
 \ctxlua{a = math.random(#2,#3)}
 %   \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
 
 \setuprandomize[2012]
 \define[3]\RandomCoeff{%
 \expandafter\def\csname 
 Coeff#1\endcsname{\ctxlua{tex.print(math.random(#2,#3))}}}
 
 \setvalue{Coeff#1}{…}
 
 Wolfgang


___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Otared Kavian
Hi again Wolfgang and Peter,

The solution you provided to my problem is great but has a small issue…
Considering the definition

\define[3]\RandomCoeff{%
\setvalue{Coeff#1}{\ctxlua{tex.print(math.random(#2,#3))}}
}

after saying 
\RandomCoeff{Alpha}{1}{10}
one gets \CoeffAlpha, but if I use several times \CoeffAlpha, it takes a new 
value each time, an effect which is undesirable in what I am willing to do.
Is there a way to somehow « freeze » the value of \CoeffAlpha once it is 
defined?

Best regards: OK
PS: here is the complete example:

\define[3]\RandomCoeff{%
\setvalue{Coeff#1}{\ctxlua{tex.print(math.random(#2,#3))}}
}

\starttext
\RandomCoeff{Alpha}{1}{10}
\RandomCoeff{Beta}{10}{100}

\CoeffAlpha

\CoeffBeta

\CoeffAlpha

\CoeffBeta

\stoptext

On 5 janv. 2012, at 21:18, Wolfgang Schuster wrote:

 
 Am 05.01.2012 um 21:17 schrieb Peter Münster:
 
 On Thu, Jan 05 2012, Otared Kavian wrote:
 
 \define[3]\RandomCoeff{%
 \ctxlua{a = math.random(#2,#3)}
 %   \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
 
 \setuprandomize[2012]
 \define[3]\RandomCoeff{%
 \expandafter\def\csname 
 Coeff#1\endcsname{\ctxlua{tex.print(math.random(#2,#3))}}}
 
 \setvalue{Coeff#1}{…}
 
 Wolfgang


___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___


Re: [NTG-context] How to define a command...

2012-01-05 Thread Otared Kavian
Sorry for the noise…
I found the answer to my last question in module-01.pdf, pages 92 and 93: the 
right definition is
\define[3]\RandomCoeff{%
\setevalue{Coeff#1}{\ctxlua{tex.print(math.random(#2,#3))}}
}
that is one has to use \setevalue instead of \setvalue (that is \edef instead 
of \def).

Best regards:  OK

On 5 janv. 2012, at 22:24, Otared Kavian wrote:

 Hi again Wolfgang and Peter,
 
 The solution you provided to my problem is great but has a small issue…
 Considering the definition
 
 \define[3]\RandomCoeff{%
   \setvalue{Coeff#1}{\ctxlua{tex.print(math.random(#2,#3))}}
   }
 
 after saying 
   \RandomCoeff{Alpha}{1}{10}
 one gets \CoeffAlpha, but if I use several times \CoeffAlpha, it takes a new 
 value each time, an effect which is undesirable in what I am willing to do.
 Is there a way to somehow « freeze » the value of \CoeffAlpha once it is 
 defined?
 
 Best regards: OK
 PS: here is the complete example:
 
 \define[3]\RandomCoeff{%
   \setvalue{Coeff#1}{\ctxlua{tex.print(math.random(#2,#3))}}
   }
 
 \starttext
 \RandomCoeff{Alpha}{1}{10}
 \RandomCoeff{Beta}{10}{100}
 
 \CoeffAlpha
 
 \CoeffBeta
 
 \CoeffAlpha
 
 \CoeffBeta
 
 \stoptext
 
 On 5 janv. 2012, at 21:18, Wolfgang Schuster wrote:
 
 
 Am 05.01.2012 um 21:17 schrieb Peter Münster:
 
 On Thu, Jan 05 2012, Otared Kavian wrote:
 
 \define[3]\RandomCoeff{%
\ctxlua{a = math.random(#2,#3)}
 %  \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not 
 work as expected...
 
 \setuprandomize[2012]
 \define[3]\RandomCoeff{%
 \expandafter\def\csname 
 Coeff#1\endcsname{\ctxlua{tex.print(math.random(#2,#3))}}}
 
 \setvalue{Coeff#1}{…}
 
 Wolfgang
 
 

___
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  : http://foundry.supelec.fr/projects/contextrev/
wiki : http://contextgarden.net
___