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

Otared Kavian otared at gmail.com
Thu Jan 5 21:28:43 CET 2012


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



More information about the ntg-context mailing list