making tables with lua
![](https://secure.gravatar.com/avatar/ecffc5c1eb6692198d091ee98e921822.jpg?s=120&d=mm&r=g)
Dear all, I used code which generates the table as following: \startluacode local NC, NR, HL, VL = context.NC, context.NR, context.HL, context.VL context.starttabulate { "|c|c|c|c|c|c|c|c|c|c|" } HL() for i=1, 6, 1 do for j=1,10 ,1 do k= i % 3 if k==1 then VL() context(10*(i - k)/3+j) else VL() end end VL() NR() if k==0 then HL() end end context.stoptabulate() \stopluacode But it has a fixed column definition as "|c|c|c|c|c|c|c|c|c|c|" and the number of columns. I wonder it is possible to use the variable for "j" to control the number of columns and the column setting. Of course, I may put similar codes which are different only in the number of columns and column settings when I need a table. But I have many similar tables whose number of columns are different only. If such a method is possible, I'd like to put such a code in \startbuffer…\stopbudffer and call it to generate tables of various number of columns given at each time. I also don't know whether it is possible to convey a number from ConTeXt to the variable in Lua code. I write this email because I saw a hope in recent discussion "Re: Fwd: Need help with \definetabulate" which has a method to vary the column setting. \definetabulate [whatever] [|l|r|] \definetabulate [whatever][else] [|l|c|r|] It may be a lazy man's question with no need for others. Thank you for reading. Best regards, Dalyoung
![](https://secure.gravatar.com/avatar/d6e7462bd4209f9c4cd08271985a50df.jpg?s=120&d=mm&r=g)
Hello,
my experience is that the best way is to create the table first, then to call Lua function to typeset the table.
You may choose whether the table will be typeset with 'tabulate' family functions or 'TABLE' family; I may recommend you the latter as it gives you much more control of the final look than the former; 'TABLE' is much better anyway.
Once you have such functions prepared, you just select to type:
printTabulate(mytab) -- via 'tabulate'
or
printTABLE(mytab) -- via 'TABLE'
If you insist on 'tabulate' family, you could use the following snippet for your start:
----
\starttext
\startluacode
local tab = {{1,2},{3,4}}
printTabulate = function(tab)
context.starttabulate{"|" .. string.rep("c|", #tab[1])}
context.HL()
for _, r in ipairs(tab) do
for _, c in ipairs(r) do
context.VL()
context(c)
end
context.VL()
context.NR()
context.HL()
end
context.stoptabulate()
end
printTabulate(tab)
\stopluacode
\stoptext
----
You can write a similar function(s) to typeset via 'TABLE'.
Best regards,
Lukas
On Wed, 17 Oct 2012 03:13:39 +0200, Jeong Dal
Dear all,
I used code which generates the table as following:
\startluacode local NC, NR, HL, VL = context.NC, context.NR, context.HL, context.VL context.starttabulate { "|c|c|c|c|c|c|c|c|c|c|" } HL() for i=1, 6, 1 do for j=1,10 ,1 do k= i % 3 if k==1 then VL() context(10*(i - k)/3+j) else VL() end end VL() NR() if k==0 then HL() end end context.stoptabulate() \stopluacode
But it has a fixed column definition as "|c|c|c|c|c|c|c|c|c|c|" and the number of columns. I wonder it is possible to use the variable for "j" to control the number of columns and the column setting. Of course, I may put similar codes which are different only in the number of columns and column settings when I need a table. But I have many similar tables whose number of columns are different only. If such a method is possible, I'd like to put such a code in \startbuffer…\stopbudffer and call it to generate tables of various number of columns given at each time. I also don't know whether it is possible to convey a number from ConTeXt to the variable in Lua code.
I write this email because I saw a hope in recent discussion "Re: Fwd: Need help with \definetabulate" which has a method to vary the column setting.
\definetabulate [whatever] [|l|r|]
\definetabulate [whatever][else] [|l|c|r|]
It may be a lazy man's question with no need for others.
Thank you for reading.
Best regards,
Dalyoung
-- Ing. Lukáš Procházka [mailto:LPr@pontex.cz] Pontex s. r. o. [mailto:pontex@pontex.cz] [http://www.pontex.cz] Bezová 1658 147 14 Praha 4 Tel: +420 244 062 238 Fax: +420 244 461 038
![](https://secure.gravatar.com/avatar/49e63acb01f2ca80efce7eed08310ce8.jpg?s=120&d=mm&r=g)
On 17-10-2012 03:13, Jeong Dal wrote:
Dear all,
I used code which generates the table as following:
\startluacode local NC, NR, HL, VL = context.NC, context.NR, context.HL, context.VL context.starttabulate { "|c|c|c|c|c|c|c|c|c|c|" } HL() for i=1, 6, 1 do for j=1,10 ,1 do k= i % 3 if k==1 then VL() context(10*(i - k)/3+j) else VL() end end VL() NR() if k==0 then HL() end end context.stoptabulate() \stopluacode
But it has a fixed column definition as "|c|c|c|c|c|c|c|c|c|c|" and the number of columns.
context.starttabulate { string.rep("|c",10+1) } ----------------------------------------------------------------- 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
-
Jeong Dal
-
Procházka Lukáš Ing. - Pontex s. r. o.