Re: [NTG-context] Code lua in a table
Hello, I am making progress and I almost get what I want to achieve but I still have two problems : how to color cells not containing numbers with the same color as the others ? how to color in salmon, for example, cells 1 2 ? 3 Thank you Fabrice \usecolors[X11] \startuseMPgraphic{DiagonalRule} rulethickness := \frameddimension{rulethickness}; drawoptions( withpen pencircle scaled rulethickness withcolor \MPcolor{\framedparameter{framecolor}}); pair leftcorner, rightcorner; leftcorner := (rulethickness, \overlayheight-rulethickness); rightcorner := (\overlaywidth-rulethickness, rulethickness); draw leftcorner -- rightcorner; \stopuseMPgraphic \defineoverlay [DiagonalRule] [\useMPgraphic{DiagonalRule}] \define[2]\DiagonalLabel{% \setuptabulate [after={\blank[\frameddimension{offset}]}] \starttabulate [|p|r|] \NC\NC #2 \NC\NR \NC #1 \NC\NC\NR \stoptabulate } \starttext \startluacode function Binom(n,k) if k > n then return "" elseif (n == 0 or k == 0) then return 1 else return math.round((n*Binom(n-1,k-1))/k) end end context.startxtable({"align={middle,lohi}, width=1cm,offset=0.8ex,bodyfont=9pt,framecolor=cyan"}) context.startxrow() context.startxcell({"background=DiagonalRule,background=color,backgroundcolor=thistle2"}) context("\\DiagonalLabel{\\m{n}}{\\m{k}}") context.stopxcell() for j = 0, 7 do context.startxcell({"background=color,backgroundcolor=thistle2"}) context(j) context.stopxcell() end context.startxcell({"background=color,backgroundcolor=thistle2"}) context("\\dots") context.stopxcell() context.stopxrow() for i = 0, 7 do context.startxrow() context.startxcell({"background=color,backgroundcolor=thistle2"}) context(i) context.stopxcell() for j = 0, 8 do context.startxcell() context(Binom(i,j)) context.stopxcell() end context.stopxrow() end context.startxrow() context.startxcell({"background=color,backgroundcolor=thistle2"}) context("\\dots") context.stopxcell() for i = 0, 8 do context.startxcell({"background=color,backgroundcolor=thistle2"}) context("") context.stopxcell() end context.stopxrow() context.stopxtable() \stopluacode \stoptext Le lun. 20 juil. 2020 à 14:55, Hans Hagen a écrit : > On 7/20/2020 2:40 PM, Jeong Dal wrote: > > > I also wonder where is the boundary of ConTeXt. > The boundaries are set and shift by users (the mailing list) and > curiosity (personal). > > Hans > > - >Hans Hagen | PRAGMA ADE >Ridderstraat 27 | 8061 GH Hasselt | The Netherlands > tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl > - > > ___ > 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://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Dear Hans, Thank you for new methods! It would take some time for me to understand them fully. Learning Lua, metafun, lmtx, wiki is always a challenge for me. I also wonder where is the boundary of ConTeXt. I hope that you and all members in this list are well from COVID-19. Thank you again. Best regards, Dalyoung > 2020. 7. 20. 오후 5:36, Hans Hagen 작성: > > On 7/20/2020 7:56 AM, Jeong Dal wrote: >> Dear Fabrice, >> You may split Binom(n,k) function into two functions as following: > > see original mail > > >> Dalyoung > Best stay in a protected namespace ... > > \startluacode > >local function fact (n) >if n <= 0 then >return 1 >else >return n * fact(n-1) >end >end > >local function ncr(n,r) >return fact(n)/(fact(r)*fact(n-r)) >end > >userdata.P = { >fact = fact, >ncr = ncr, >} > >function MP.pascal_ncr(n, r) >mp.print(ncr(n,r)) >end > > \stopluacode > > Watch the last definition. This permits > > % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " > ))"); > > replaced by > >tt := lua.MP.pascal_ncr(n,r); > > which looks nicer. > > \startbuffer[pt1] >numeric n, r, s, u, dx, dy, tt; >path p, q; >pair A, B, start, now; >u := 1.8cm; >A := dir(210)*u; >B := dir(-30)*u; >dy := sind(30)*u; >dx := 2*cosd(30)*u; >for n=0 upto 4: >start := n*dir(210)*u; >for r=0 upto n: >s := n-r; > % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r > & " ))"); > tt := lua.MP.pascal_ncr(n,r); >now := start+r*right*dx; >dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & > decimal r & "} = "& decimal tt & "$"),now); >draw (now+A) -- now -- (now+B); >endfor; >endfor; > \stopbuffer > > Now, in context lmtx we can have a different kind of abstraction. We can do > this: > >function MP.pascal_ncr_x() >mp.print(ncr(mp.scan.pair())) >end > > and then use: > >tt := runscript("MP.pascal_ncr_x()") (n,r) ; > > Of course one can decide to pick to two numerics instead, like > >tt := runscript("MP.pascal_ncr_x()") n r ; > > but i leave that as exercise. > > % tt := runscript mp_pascal_ncr (n,r) ; >tt := pascal_ncr (n,r) ; > > However, we still have the rather verbose runscript here, so we go further, > we register pascal as script: > > \startluacode >metapost.registerscript("pascal_ncr",MP.pascal_ncr_x) > \stopluacode > > And then define an alias at the metafun end: > > \startMPextensions >newinternal mp_pascal_ncr ; mp_pascal_ncr := scriptindex "pascal_ncr" ; > >def pascal_ncr = >runscript mp_pascal_ncr >enddef ; > \stopMPextensions > > The internal permits this: > > tt := runscript mp_pascal_ncr (n,r) ; > > while the additional def permits > >tt := pascal_ncr (n,r) ; > > Now watch out, because we define pascal_ncr here, something > lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded because > that is what mp does (i'll probably cook something for that some day). > > Now, to come back to > > "I couldn’t wikify it at that time because I don’t know >how to. I’ll do it soon." > > looks like you suddenly have an additional challenge, > > Hans > > > > - > Hans Hagen | PRAGMA ADE > Ridderstraat 27 | 8061 GH Hasselt | The Netherlands > tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl > - ___ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
On 7/20/2020 2:40 PM, Jeong Dal wrote: I also wonder where is the boundary of ConTeXt. The boundaries are set and shift by users (the mailing list) and curiosity (personal). Hans - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl - ___ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Hi all, Thank you for your contributions, it allows me to progress. @Hans and Deal The code is impressive but does not correspond to the shape of the triangle that I have to make (by convention). Fabrice Le lun. 20 juil. 2020 à 10:36, Hans Hagen a écrit : > On 7/20/2020 7:56 AM, Jeong Dal wrote: > > Dear Fabrice, > > > > You may split Binom(n,k) function into two functions as following: > > > > see original mail > > > > Dalyoung > Best stay in a protected namespace ... > > \startluacode > > local function fact (n) > if n <= 0 then > return 1 > else > return n * fact(n-1) > end > end > > local function ncr(n,r) > return fact(n)/(fact(r)*fact(n-r)) > end > > userdata.P = { > fact = fact, > ncr = ncr, > } > > function MP.pascal_ncr(n, r) > mp.print(ncr(n,r)) > end > > \stopluacode > > Watch the last definition. This permits > >% tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & > decimal r & " ))"); > > replaced by > > tt := lua.MP.pascal_ncr(n,r); > > which looks nicer. > > \startbuffer[pt1] > numeric n, r, s, u, dx, dy, tt; > path p, q; > pair A, B, start, now; > u := 1.8cm; > A := dir(210)*u; > B := dir(-30)*u; > dy := sind(30)*u; > dx := 2*cosd(30)*u; > for n=0 upto 4: > start := n*dir(210)*u; > for r=0 upto n: > s := n-r; >% tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & > decimal r & " ))"); >tt := lua.MP.pascal_ncr(n,r); > now := start+r*right*dx; > dotlabel.top(textext("$\displaystyle {" & decimal n & > "\choose" & decimal r & "} = "& decimal tt & "$"),now); > draw (now+A) -- now -- (now+B); > endfor; > endfor; > \stopbuffer > > Now, in context lmtx we can have a different kind of abstraction. We can > do this: > > function MP.pascal_ncr_x() > mp.print(ncr(mp.scan.pair())) > end > > and then use: > > tt := runscript("MP.pascal_ncr_x()") (n,r) ; > > Of course one can decide to pick to two numerics instead, like > > tt := runscript("MP.pascal_ncr_x()") n r ; > > but i leave that as exercise. > >% tt := runscript mp_pascal_ncr (n,r) ; > tt := pascal_ncr (n,r) ; > > However, we still have the rather verbose runscript here, so we go > further, we register pascal as script: > > \startluacode > metapost.registerscript("pascal_ncr",MP.pascal_ncr_x) > \stopluacode > > And then define an alias at the metafun end: > > \startMPextensions > newinternal mp_pascal_ncr ; mp_pascal_ncr := scriptindex "pascal_ncr" > ; > > def pascal_ncr = > runscript mp_pascal_ncr > enddef ; > \stopMPextensions > > The internal permits this: > > tt := runscript mp_pascal_ncr (n,r) ; > > while the additional def permits > > tt := pascal_ncr (n,r) ; > > Now watch out, because we define pascal_ncr here, something > lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded > because that is what mp does (i'll probably cook something for that some > day). > > Now, to come back to > > "I couldn’t wikify it at that time because I don’t know > how to. I’ll do it soon." > > looks like you suddenly have an additional challenge, > > Hans > > > > - >Hans Hagen | PRAGMA ADE >Ridderstraat 27 | 8061 GH Hasselt | The Netherlands > tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl > - > > ___ > 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://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Dear Fabrice, You may split Binom(n,k) function into two functions as following: * \startluacode P={} combi = P function P.fact (n) if n <= 0 then return 1 else return n * P.fact(n-1) end end function P.ncr(n,r) return P.fact(n)/(P.fact(r)*P.fact(n-r)) end combi = { fact = fact, ncr = ncr, } \stopluacode ** Your table is actually Pascal’d triangle. Using the above function, I was able to draw Pascal’s triangles. Hans helped me to complete it. I couldn’t wikify it at that time because I don’t know how to. I’ll do it soon. Here is the whole code for Pascal’s triangle in two different ways using Lua, Metafun and ConTeXt. You may enhance the code. \startbuffer[pt1] numeric n,r,s,u,dx,dy,tt; u := 1.8cm; path p, q; pair A,B,start,now; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 4: start := n*dir(210)*u; for r=0 upto n: s := n-r; tt := lua("mp.print(P.ncr(" & decimal n & "," & decimal r & " ))"); now := start+r*right*dx; dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now); draw (now+A) -- now -- (now+B); endfor; endfor; \stopbuffer \startbuffer[pt2] numeric n,r,s,u,dx,dy,tt; u := 1cm; path p, q; pair A,B,start,now; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 8: start := n*dir(210)*u; for r=0 upto n: s := n-r; tt := lua("mp.print(P.ncr(" & decimal n & "," & decimal r & " ))"); now := start+r*right*dx; label(textext("$" & decimal tt & "$"),now); endfor; endfor; \stopbuffer \startluacode P={} combi = P function P.fact (n) if n <= 0 then return 1 else return n * P.fact(n-1) end end function P.ncr(n,r) return P.fact(n)/(P.fact(r)*P.fact(n-r)) end combi = { fact = fact, ncr = ncr, } \stopluacode \starttext \processMPbuffer[pt1] \blank[big] \processMPbuffer[pt2] \stoptext I hope that it helps. Best regards, Dalyoung ___ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
On 7/20/2020 7:56 AM, Jeong Dal wrote: Dear Fabrice, You may split Binom(n,k) function into two functions as following: > see original mail > Dalyoung Best stay in a protected namespace ... \startluacode local function fact (n) if n <= 0 then return 1 else return n * fact(n-1) end end local function ncr(n,r) return fact(n)/(fact(r)*fact(n-r)) end userdata.P = { fact = fact, ncr = ncr, } function MP.pascal_ncr(n, r) mp.print(ncr(n,r)) end \stopluacode Watch the last definition. This permits % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))"); replaced by tt := lua.MP.pascal_ncr(n,r); which looks nicer. \startbuffer[pt1] numeric n, r, s, u, dx, dy, tt; path p, q; pair A, B, start, now; u := 1.8cm; A := dir(210)*u; B := dir(-30)*u; dy := sind(30)*u; dx := 2*cosd(30)*u; for n=0 upto 4: start := n*dir(210)*u; for r=0 upto n: s := n-r; % tt := lua("mp.print(userdata.P.ncr(" & decimal n & "," & decimal r & " ))"); tt := lua.MP.pascal_ncr(n,r); now := start+r*right*dx; dotlabel.top(textext("$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "$"),now); draw (now+A) -- now -- (now+B); endfor; endfor; \stopbuffer Now, in context lmtx we can have a different kind of abstraction. We can do this: function MP.pascal_ncr_x() mp.print(ncr(mp.scan.pair())) end and then use: tt := runscript("MP.pascal_ncr_x()") (n,r) ; Of course one can decide to pick to two numerics instead, like tt := runscript("MP.pascal_ncr_x()") n r ; but i leave that as exercise. % tt := runscript mp_pascal_ncr (n,r) ; tt := pascal_ncr (n,r) ; However, we still have the rather verbose runscript here, so we go further, we register pascal as script: \startluacode metapost.registerscript("pascal_ncr",MP.pascal_ncr_x) \stopluacode And then define an alias at the metafun end: \startMPextensions newinternal mp_pascal_ncr ; mp_pascal_ncr := scriptindex "pascal_ncr" ; def pascal_ncr = runscript mp_pascal_ncr enddef ; \stopMPextensions The internal permits this: tt := runscript mp_pascal_ncr (n,r) ; while the additional def permits tt := pascal_ncr (n,r) ; Now watch out, because we define pascal_ncr here, something lua.MP.pascal_ncr(n,r) won't work because the last part gets expanded because that is what mp does (i'll probably cook something for that some day). Now, to come back to "I couldn’t wikify it at that time because I don’t know how to. I’ll do it soon." looks like you suddenly have an additional challenge, Hans - Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl - ___ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
> On 18 Jul 2020, at 23:35, Otared Kavian wrote: > > > >> On 18 Jul 2020, at 22:25, Fabrice Couvreur >> wrote: >> >> Hi Wolfgang, >> >> It works but why did you ask this question since I finally use context ? >> Are you sure you want to use context(...) here? >> How not to display the .0 ? Or even better, you can return an integer in your Binom function: return math.round((n*Binom(n-1,k-1))/k) Best regards: Otared ___ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
> On 18 Jul 2020, at 22:25, Fabrice Couvreur > wrote: > > Hi Wolfgang, > > It works but why did you ask this question since I finally use context ? > Are you sure you want to use context(...) here? > How not to display the .0 ? Besides what Wolfgang suggested, ou can also use context(math.round(Binom(i,j))) to have integers instead of real numbers. Best regards: Otared ___ 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Fabrice Couvreur schrieb am 18.07.2020 um 22:25: Hi Wolfgang, It works but why did you ask this question since I finally use context ? Are you sure you want to use context(...) here? In your first version you used the context function in the loop to print the output of the Binom but the Binom function used context as well, this could result in context(context(...)). How not to display the .0 ? Format the content of the context function to show only integer values, e.g. context("%d",Binom(...)). For more information search for formatter in cld-mkiv.pdf. 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Small correction : \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end end context.startxtable({"align={middle,lohi}, width=1cm,offset=0.8ex,bodyfont=9pt"}) for i = 0, 9 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end context.stopxrow() end context.stopxtable() \stopluacode \stoptext Le sam. 18 juil. 2020 à 22:25, Fabrice Couvreur a écrit : > Hi Wolfgang, > > It works but why did you ask this question since I finally use context ? > >> Are you sure you want to use context(...) here? >> > How not to display the .0 ? > Fabrice > > Le sam. 18 juil. 2020 à 22:22, Wolfgang Schuster < > wolfgang.schuster.li...@gmail.com> a écrit : > >> Fabrice Couvreur schrieb am 18.07.2020 um 22:09: >> > Hi, >> > I improved the code to be able to display the grid but I cannot display >> > the coefficients. >> > Thank you >> > Fabrice >> > >> > \starttext >> > >> > \startluacode >> > function Binom(n,k) >> > if k > n then >> > return 0 >> > elseif (n == 0 or k == 0) then >> > return 1 >> > else >> > return (n*Binom(n-1,k-1))/k >> > end >> > >> > end >> > >> > context.startxtable({"align={middle,lohi}, >> > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) >> > for i = 1, 10 do >> >context.startxrow({"background=color,backgroundcolor=cyan"}) >> > for j = 1, i do >> >context.startxcell() >> > \ctxlua{context(Binom(i,j))} >> >> Remove \ctxlua: >> >> context(Binom(i,j)) >> >> 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://context.aanhet.net >> archive : https://bitbucket.org/phg/context-mirror/commits/ >> 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Hi Wolfgang, It works but why did you ask this question since I finally use context ? > Are you sure you want to use context(...) here? > How not to display the .0 ? Fabrice Le sam. 18 juil. 2020 à 22:22, Wolfgang Schuster < wolfgang.schuster.li...@gmail.com> a écrit : > Fabrice Couvreur schrieb am 18.07.2020 um 22:09: > > Hi, > > I improved the code to be able to display the grid but I cannot display > > the coefficients. > > Thank you > > Fabrice > > > > \starttext > > > > \startluacode > > function Binom(n,k) > > if k > n then > > return 0 > > elseif (n == 0 or k == 0) then > > return 1 > > else > > return (n*Binom(n-1,k-1))/k > > end > > > > end > > > > context.startxtable({"align={middle,lohi}, > > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) > > for i = 1, 10 do > >context.startxrow({"background=color,backgroundcolor=cyan"}) > > for j = 1, i do > >context.startxcell() > > \ctxlua{context(Binom(i,j))} > > Remove \ctxlua: > > context(Binom(i,j)) > > 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://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Fabrice Couvreur schrieb am 18.07.2020 um 22:09: Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))} Remove \ctxlua: context(Binom(i,j)) 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))} context.stopxcell() end context.stopxrow() end context.stopxtable() \stopluacode \stoptext Le sam. 18 juil. 2020 à 20:32, Wolfgang Schuster < wolfgang.schuster.li...@gmail.com> a écrit : > Fabrice Couvreur schrieb am 18.07.2020 um 20:05: > > Hello, > I want to display the values of Pascal's triangle in a table. > Unfortunately my knowledge of Lua is not sufficient to run this code. > Thank you. > Fabrice > > \starttext > > \startluacode > function Binom(n,k) >if k > n then > return 0 >elseif (n == 0 or k == 0) then > return 1 >else > return (n*Binom(n-1,k-1))/k > end > context(Binom(n,k)) > > Are you sure you want to use context(...) here? > > end > > context.startxtable({"align={middle,lohi}, > width=1.2cm,offset=0.8ex,bodyfont=9pt"}) > context.startxrow({"background=color,backgroundcolor=green"}) > for i = 0, 10 do > for j = 0, i do > context.startxcell() > context(Binom(i,j)) > context.stopxcell() > end > end > context.stopxrow() > > context.stopxtable() > > 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://context.aanhet.net > archive : https://bitbucket.org/phg/context-mirror/commits/ > 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___
Re: [NTG-context] Code lua in a table
Fabrice Couvreur schrieb am 18.07.2020 um 20:05: Hello, I want to display the values of Pascal's triangle in a table. Unfortunately my knowledge of Lua is not sufficient to run this code. Thank you. Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end context(Binom(n,k)) Are you sure you want to use context(...) here? end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) context.startxrow({"background=color,backgroundcolor=green"}) for i = 0, 10 do for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end end context.stopxrow() context.stopxtable() 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://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net ___