* How to define a command...
@ 2012-01-05 20:05 Otared Kavian
2012-01-05 20:11 ` Meer, H. van der
2012-01-05 20:17 ` Peter Münster
0 siblings, 2 replies; 10+ messages in thread
From: Otared Kavian @ 2012-01-05 20:05 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:05 How to define a command Otared Kavian
@ 2012-01-05 20:11 ` Meer, H. van der
2012-01-05 20:21 ` Wolfgang Schuster
2012-01-05 20:28 ` Otared Kavian
2012-01-05 20:17 ` Peter Münster
1 sibling, 2 replies; 10+ messages in thread
From: Meer, H. van der @ 2012-01-05 20:11 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:05 How to define a command Otared Kavian
2012-01-05 20:11 ` Meer, H. van der
@ 2012-01-05 20:17 ` Peter Münster
2012-01-05 20:18 ` Wolfgang Schuster
2012-01-05 20:29 ` Otared Kavian
1 sibling, 2 replies; 10+ messages in thread
From: Peter Münster @ 2012-01-05 20:17 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:17 ` Peter Münster
@ 2012-01-05 20:18 ` Wolfgang Schuster
2012-01-05 20:42 ` Otared Kavian
2012-01-05 21:24 ` Otared Kavian
2012-01-05 20:29 ` Otared Kavian
1 sibling, 2 replies; 10+ messages in thread
From: Wolfgang Schuster @ 2012-01-05 20:18 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:11 ` Meer, H. van der
@ 2012-01-05 20:21 ` Wolfgang Schuster
2012-01-05 20:28 ` Otared Kavian
1 sibling, 0 replies; 10+ messages in thread
From: Wolfgang Schuster @ 2012-01-05 20:21 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:11 ` Meer, H. van der
2012-01-05 20:21 ` Wolfgang Schuster
@ 2012-01-05 20:28 ` Otared Kavian
1 sibling, 0 replies; 10+ messages in thread
From: Otared Kavian @ 2012-01-05 20:28 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:17 ` Peter Münster
2012-01-05 20:18 ` Wolfgang Schuster
@ 2012-01-05 20:29 ` Otared Kavian
1 sibling, 0 replies; 10+ messages in thread
From: Otared Kavian @ 2012-01-05 20:29 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:18 ` Wolfgang Schuster
@ 2012-01-05 20:42 ` Otared Kavian
2012-01-05 21:24 ` Otared Kavian
1 sibling, 0 replies; 10+ messages in thread
From: Otared Kavian @ 2012-01-05 20:42 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 20:18 ` Wolfgang Schuster
2012-01-05 20:42 ` Otared Kavian
@ 2012-01-05 21:24 ` Otared Kavian
2012-01-05 21:36 ` Otared Kavian
1 sibling, 1 reply; 10+ messages in thread
From: Otared Kavian @ 2012-01-05 21:24 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to define a command...
2012-01-05 21:24 ` Otared Kavian
@ 2012-01-05 21:36 ` Otared Kavian
0 siblings, 0 replies; 10+ messages in thread
From: Otared Kavian @ 2012-01-05 21:36 UTC (permalink / raw)
To: mailing list for ConTeXt users
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
___________________________________________________________________________________
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2012-01-05 21:36 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-05 20:05 How to define a command Otared Kavian
2012-01-05 20:11 ` Meer, H. van der
2012-01-05 20:21 ` Wolfgang Schuster
2012-01-05 20:28 ` Otared Kavian
2012-01-05 20:17 ` Peter Münster
2012-01-05 20:18 ` Wolfgang Schuster
2012-01-05 20:42 ` Otared Kavian
2012-01-05 21:24 ` Otared Kavian
2012-01-05 21:36 ` Otared Kavian
2012-01-05 20:29 ` Otared Kavian
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).