Am 23.12.2012 um 02:58 schrieb Roland Thiers
: Bonjour, I am new in ConTeXt. I love its features. I need some (or a lot of) help. I tried to get a macro which compute some values for a math function.
Le 23 déc. 12 à 18:43, Wolfgang Schuster a écrit :
\startluacode
userdata = userdata or {} userdata.roland = userdata.roland or {}
local roland = userdata.roland
function roland.f(x) context(x*x*x) end
function roland.table(min,max,step) local min = tonumber(min) local max = tonumber(max) local step = tonumber(step) context.starttable({string.format("|*{%d}{l|}",max-min+2)}) context.HL() context.VL() context("x") for i=min,max do context.NC() context(min+(i-1)*step) end context.VL() context.FR() context.HL() context.VL() context("f(x)") for i=min,max do context.NC() roland.f(min+(i-1)*step) end context.VL() context.AR() context.HL() context.stoptable() end
\stopluacode
\define[3]\TableFuntion {\ctxlua{userdata.roland.table("#1","#2","#3")}}
\starttext \TableFuntion{1}{6}{1} \TableFuntion{2}{7}{1} \stoptext
Wolfgang
I read with care (maybe) your code above, Wolfgang (thanks again !) et had a look in "programming in lua" to get a better idea of the object table, new in lua too :-) I have some more questions : - when one create a table doing this : userdata=userdata or {}, is it only to be sure that "userdata" is not actually a variable which exists (with a non nil value) ? Less safely we could write : userdata={} ? - arguments for the user.data.table function are given as strings : " #1" and changed in numbers in the body of the function. I noticed that it was possible to give numbers directly. is there always better to do like this ? (enter strings et use tonumber) - I found the command \define in context garden and saw it's the same than \unexpanded\def , there is a link to the définitions of the TeX primitives but I could'nt find \unexpanded\def (\def was very instructive however). - I changed slightly the code to obtain a range (x between min and max with a step) and to choose a round-way. I failed to find a context way to get rounded numbers, so I did a workaround (a another function) with help from "programming in lua". I would like a more straight (context) way. Here is the code : \startluacode -- userdata = userdata or {} -- userdata["roland"] = userdata["roland"] or {} userdata=userdata or {} userdata["roland"]=userdata["roland"] or {} local roland = userdata["roland"] function roland.f(x) return math.log(x) end function roland.round (number, approx) local power = 10^(approx or 0) context(math.floor(number * power + 0.5) / power) end function roland.table(min,max,step,approx) local min = tonumber(min) local max = tonumber(max) local step = tonumber(step) local nbrcol = tonumber((max-min)*(1/step)+2) local approx = tonumber(approx) context.starttable({string.format("|*{%d}{l|}",nbrcol)}) context.HL() context.VL() context("x") for i=1,(nbrcol-1) do context.NC() context(min+(i-1)*step) end context.VL() context.FR() context.HL() context.VL() context("f(x)") for i=1,(nbrcol-1) do context.NC() local result=roland.f(min+(i-1)*step) roland.round(result,approx) end context.VL() context.AR() context.HL() context.stoptable() end \stopluacode \define[4]\TableFuntion {\ctxlua{userdata.roland.table("#1","#2","#3","#4")}} \starttext \TableFuntion{2}{6}{0.5}{2} \blank[2*big] \TableFuntion{1}{16}{1}{} \blank[2*big] \TableFuntion{2}{4}{2}{5} \blank[2*big] \TableFuntion{0.2}{2}{0.2}{3} \stoptext Best regards, Roland Happy Christmas to all Context users !
___________________________________________________________________________________ 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 ___________________________________________________________________________________