On 1/4/2025 12:27 AM, Jeong Dal via ntg-context wrote:
Dear Florent, Wolfgang, and Hans,
I modify the code given by Hans as following:
1. Place the function definition ab the beginning of the file. I use if function to check the number of element of a and apply it to the number of columns.
2. And call the functions in the text.
It works! It draw the truth table of the given logic functions.
Thanks a lot.
Best regards,
Dalyoung
\startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(1.5cm)|" a = t end context.starttabulate { p } context.FL() for i=1,#a do context.NC() context(a[i]) end context.NC() context.NR() context.LL() end
function document.truthTable(a) document.MakeHead (a) for i,s in ipairs({true,false}) do for j,t in ipairs({true,false}) do for k,u in ipairs({true,false}) do if s then val=1 else val=0 end context.NC() context(val) context.NC() if t then val=1 else val=0 end context(val) context.NC() if #a == 3 then break end if u then val=1 else val=0 end context(val) context.NC() con=document.logicFa(s,t,u) context(con) context.NC() context.AR() end if #a == 3 then con=document.logicF2(s,t) context(con) context.NC() context.AR() end end end context.HL() context.stoptabulate() end \stopluacode
In the text call the functions as following:
\startplacetable[location=here,reference=tbl:Fxyz] \startluacode function document.logicFa(p,q,r) if ((not p or not q) and r) or (p and (q or not r)) then return "1" else return "0" end end a = { "p", "q", "r","f(p,q,r)"} document.truthTable(a) \stopluacode \stopplacetable
Best use 'local' for val, con, a etc or do this: \startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(1.5cm)|" a = t end context.starttabulate { p } context.FL() for i=1,#a do context.NC() context(a[i]) end context.NC() context.NR() context.LL() end function document.MakeFooter() context.HL() context.stoptabulate() end function document.truthTable(a) document.MakeHead(a) for i,s in ipairs({true,false}) do for j,t in ipairs({true,false}) do for k,u in ipairs({true,false}) do context.NC() context(s and "1" or "0") context.NC() context(t and "1" or "0") context.NC() if #a == 3 then break end context(u and "1" or "0") context.NC() context(document.logicFa(s,t,u)) context.NC() context.AR() end if #a == 3 then context(document.logicF2(s,t)) context.NC() context.AR() end end end document.MakeFooter() end \stopluacode \startplacetable[location=here,reference=tbl:Fxyz] \startluacode function document.logicFa(p,q,r) if ((not p or not q) and r) or (p and (q or not r)) then return "1" else return "0" end end document.truthTable { "p", "q", "r","f(p,q,r)" } \stopluacode \stopplacetable you don't need the intermediate variables 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 -----------------------------------------------------------------