to pass an argument in a macro
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. With help from the wiki ConTeXt Garden and some time I could do that - write my function userdata.f - write another function to put the values in a table : userdata.tabf - put both of them in a file "/Users/rolandt/context/mesfonctionsluatex.lua" - In a file.tex, I use that macro : \def\tabf#1#2% {\startluacode dofile("/Users/rolandt/context/mesfonctionsluatex.lua") userdata.tabf(#1,#2) \stopluacode} Here are the functions: -- fonction cube -- on utilise l'espace de nom userdata userdata=userdata or {} function userdata.f(x) context(x*x*x) end -- fonction tabf -- which makes a table , compute 6 values, b=first x, c = step userdata=userdata or {} function userdata.tabf(b,c) context.starttable{"*{7}{|l}|"} local b=b local c=c context("\\HL") context("\\VL x ") for i=1,6 do context("\\NC" .. b+(i-1)*c) end context("\\VL".."\\AR") context("\\HL") context("\\VL f(x) ") for i=1,6 do context("\\NC") context(userdata.f(b+(i-1)*c)) end context("\\VL".."\\LR") context("\\HL") context.stoptable() end I am sure it could be much better but it works ! Evidently I need a third argument to be able to choose how many values compute. I failed completely (I don't understand how arguments are passed). I tried things like : context.starttable{"*{%s}{|l}|",a} but ... fatal error :) I would appreciate some help or what to read to improve understanding that kind of things. Best regards, Roland
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. With help from the wiki ConTeXt Garden and some time I could do that - write my function userdata.f - write another function to put the values in a table : userdata.tabf - put both of them in a file "/Users/rolandt/context/mesfonctionsluatex.lua" - In a file.tex, I use that macro : \def\tabf#1#2% {\startluacode dofile("/Users/rolandt/context/mesfonctionsluatex.lua") userdata.tabf(#1,#2) \stopluacode}
When you Lua code is in the same folder as the TeX file you can use \ctxloadluafile{myfile} to load the Lua file.
Here are the functions:
-- fonction cube -- on utilise l'espace de nom userdata userdata=userdata or {}
function userdata.f(x) context(x*x*x) end
-- fonction tabf -- which makes a table , compute 6 values, b=first x, c = step
userdata=userdata or {}
function userdata.tabf(b,c) context.starttable{"*{7}{|l}|"} local b=b local c=c context("\\HL") context("\\VL x ") for i=1,6 do context("\\NC" .. b+(i-1)*c) end context("\\VL".."\\AR") context("\\HL") context("\\VL f(x) ") for i=1,6 do context("\\NC") context(userdata.f(b+(i-1)*c)) end context("\\VL".."\\LR") context("\\HL") context.stoptable() end
\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
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 ___________________________________________________________________________________
On 12/25/2012 9:36 AM, Roland Thiers wrote:
- 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={} ?
There can already be stuff in userdata that you don't want to loose.
- 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)
Depends on if you want to catch errors. Forgetting an argument (or an empty one) when a number is expected results in ,, which triggers a Lua error, while with ,"", you can intercept the empty string and default. Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
Le 25 déc. 12 à 23:24, Hans Hagen a écrit :
On 12/25/2012 9:36 AM, Roland Thiers wrote:
- 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={} ?
There can already be stuff in userdata that you don't want to loose. OK.
- 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)
Depends on if you want to catch errors. Forgetting an argument (or an empty one) when a number is expected results in ,, which triggers a Lua error, while with ,"", you can intercept the empty string and default.
Hans
OK. Thanks ! Roland
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------
participants (3)
-
Hans Hagen
-
Roland Thiers
-
Wolfgang Schuster