You can't use '\prevdepth' in restricted horizontal mode
Dear all, I got the error "You can't use '\prevdepth' in restricted horizontal mode” when I compile the following code. If I remove “\starttabulete… \stoptabulate”, then no errors, but the out put is not the form of table. How can I fix this error? Thank you. Best regards, Dalyoung \starttext \startluacode function makeHead(a) local n = #a context.HL() for i = 1, n do context.NC() context(a[i]) end context.NC()context.AR() context.HL() end \stopluacode %\starttabulate[|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|] \startluacode local a = {"x","y","z","(x\\to y)"} makeHead(a) \stopluacode %\stoptabulate \stoptext
Dear Dalyoung,
I am not sure what the issue is; but as a workaround it seems that using
\ctxlua works:
___________________________________________________
\starttext
\startluacode
function makeHead(a)
local n = #a
context.HL()
for i = 1, n do
context.NC() context(a[i])
end
context.NC()context.AR()
context.HL()
end
\stopluacode
\starttabulate[|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|]
\ctxlua{local a = {"x","y","z","(x\\to y)"}; makeHead(a)}
\stoptabulate
\stoptext
___________________________________________________
Cheers,
Florent
Le ven. 3 janv. 2025 à 15:58, Jeong Dal via ntg-context
Dear all,
I got the error "You can't use '\prevdepth' in restricted horizontal mode” when I compile the following code.
If I remove “\starttabulete… \stoptabulate”, then no errors, but the out put is not the form of table.
How can I fix this error?
Thank you.
Best regards,
Dalyoung
\starttext
\startluacode function makeHead(a) local n = #a context.HL() for i = 1, n do context.NC() context(a[i]) end context.NC()context.AR() context.HL() end \stopluacode
%\starttabulate[|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|] \startluacode local a = {"x","y","z","(x\\to y)"} makeHead(a) \stopluacode %\stoptabulate
\stoptext
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror) archive : https://github.com/contextgarden/context wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
Am 03.01.2025 um 16:53 schrieb Jeong Dal via ntg-context:
Dear all,
I got the error "You can't use '\prevdepth' in restricted horizontal mode” when I compile the following code.
If I remove “\starttabulete… \stoptabulate”, then no errors, but the out put is not the form of table.
How can I fix this error?
Thank you.
Best regards,
Dalyoung
\starttext
\startluacode function makeHead(a) local n = #a context.HL() for i = 1, n do context.NC() context(a[i]) end context.NC()context.AR() context.HL() end \stopluacode
%\starttabulate[|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|] \startluacode local a = {"x","y","z","(x\\to y)"} makeHead(a) \stopluacode %\stoptabulate
\stoptext
Move the tabulate environment into the luacode environment. \startluacode context.starttabulate{ "|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|" } local a = {"x","y","z","(x\\to y)"} makeHead(a) context.stoptabulate() \stopluacode Wolfgang
On 1/3/2025 4:53 PM, Jeong Dal via ntg-context wrote:
Dear all,
I got the error "You can't use '\prevdepth' in restricted horizontal mode” when I compile the following code.
If I remove “\starttabulete… \stoptabulate”, then no errors, but the out put is not the form of table.
How can I fix this error?
Thank you.
Best regards,
Dalyoung
\starttext
\startluacode function makeHead(a) local n = #a context.HL() for i = 1, n do context.NC() context(a[i]) end context.NC()context.AR() context.HL() end \stopluacode
%\starttabulate[|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|] \startluacode local a = {"x","y","z","(x\\to y)"} makeHead(a) \stopluacode %\stoptabulate
\stoptext
\starttext \startluacode function makeHead(p,a) if p == "" then p = string.rep("|k.5",#a) .. "|" end context.starttabulate { p } context.FL() for i=1,#a do context.NC() context(a[i]) end context.NC() context.NR() context.LL() context.stoptabulate() end \stopluacode \startluacode makeHead ( -- "|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(2cm)|", "|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(2cm)|", { "x", "y", "z", "(x\\to y)" } ) \stopluacode \startluacode makeHead ( "", { "x", "y", "z", "(x\\to y)" } ) \stopluacode \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Dear Florent, Wolfgang, and Hans, Thank you for the solutions. All solutions are working well. I add more rows for the last solution to show the truth table of a logical function. I may modify it for 2-variable function. Thanks again. Best regards, Dalyoung \starttext \startluacode function document.logicFa(p,q,r) local p,q,r if ((p or r) and (not q)) then return "1" else return "0" end end function document.logicFb(p,q) local p,q if ((not p) or q) then return "1" else return "0" end end 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() -- context.stoptabulate() end \stopluacode \startluacode a = { "x", "y","z", "\\overbar{y}(x+z)" } document.MakeHead (a) -- "|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(2cm)|”, for i,p in ipairs({true,false}) do for j,q in ipairs({true,false}) do for k,r in ipairs({true,false}) do context.NC() if p then val=1 else val=0 end context(val) context.NC() if q then val=1 else val=0 end context(val) context.NC() if r then val=1 else val=0 end context(val) context.NC() con=document.logicFa(p,q,r) context(con) context.NC() context.AR() end end end context.HL() context.stoptabulate() \stopluacode \stoptext
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
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 -----------------------------------------------------------------
Dear Hans, Thank you for your suggestions. I changed the code without the intermediate variables. I works well. Best regards, Dalyoung
2025. 1. 5. 오후 7:42, Hans Hagen via ntg-context
작성: 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 ----------------------------------------------------------------- ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror) archive : https://github.com/contextgarden/context wiki : https://wiki.contextgarden.net ___________________________________________________________________________________
On 1/6/2025 2:33 PM, Jeong Dal via ntg-context wrote:
Dear Hans,
Thank you for your suggestions.
I changed the code without the intermediate variables.
I works well.
good, so now we're ready for a next improvement: local tf = { true, false } function document.truthTable(a) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) 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 and after that local tf = { "1", "0" } function document.truthTable(a) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) do context.NC() context(s) context.NC() context(t) context.NC() if #a == 3 then break end context(u) 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 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 -----------------------------------------------------------------
Dear Hans, I tested your code.
local tf = { true, false }
function document.truthTable(a) document.MakeHead(a) … …
end document.MakeFooter() end
This part produces the right logical value. But, the next part doesn’t produce the correct value. While the value of each variable is printed correctly, the values of outputs are all 0.
local tf = { "1", "0" }
function document.truthTable(a) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) do context.NC() context(s) context.NC() context(t) context.NC() … ... end end end document.MakeFooter() end
I think that the logical function “document.logicF()"is not working for the array tf={“1”,”0”}. In python, 1 and 0 are treated as true and false. But, I read that only “false” and “nil” are treated as false in Lua, that is, “0” is also treated as true! I checked it using the following simple code: \starttext \startluacode if 0 then context("True") else context("False") end \stopluacode \stoptext The output The output is “True”. Best regards, Dalyoung
On 1/7/2025 3:27 PM, Jeong Dal via ntg-context wrote:
Dear Hans,
I tested your code.
local tf = { true, false }
function document.truthTable(a) document.MakeHead(a) … …
end document.MakeFooter() end
This part produces the right logical value. But, the next part doesn’t produce the correct value. While the value of each variable is printed correctly, the values of outputs are all 0.
local tf = { "1", "0" }
function document.truthTable(a) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) do context.NC() context(s) context.NC() context(t) context.NC() … ... end end end document.MakeFooter() end
I think that the logical function “document.logicF()"is not working for the array tf={“1”,”0”}. In python, 1 and 0 are treated as true and false. But, I read that only “false” and “nil” are treated as false in Lua, that is, “0” is also treated as true! I checked it using the following simple code:
\starttext \startluacode if 0 then context("True") else context("False") end \stopluacode \stoptext
The output The output is “True”.
Ah, i didn't check the function, so how about this: local tf = { true, false } --local tf = { "1", "0" } function document.truthTable(a,f3,f2) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) do context.NC() context(s and "1" or "0") -- context(s) context.NC() context(t and "1" or "0") -- context(t) context.NC() if #a == 3 then break end context(u and "1" or "0") -- context(u) context.NC() context(f3(s,t,u) and "1" or "0") context.NC() context.AR() end if #a == 3 then context(f2(s,t) and "1" or "0") context.NC() context.AR() end end end document.MakeFooter() end and then you can do \startluacode local function logicF3(p,q,r) return ((not p or not q) and r) or (p and (q or not r)) -- return ((p == "0" or q == "0") and r == "1") or (p == "1" and (q == "1" or r == "0")) end local function logicF2(p,q) -- todo return true end document.truthTable ( { "p", "q", "r", "f(p,q,r)" }, logicF3, logicF2 ) \stopluacode so no need to store in document.logicXX and no need for that function to know about "0" and "1" being rendered, (btw, logicF2 was not defined in the example) 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 -----------------------------------------------------------------
Dear Hans, It is nice to input the function as an argument. It is suffice to convey one function at each all. But the definition of the function
local function logicF3(p,q,r) return ((not p or not q) and r) or (p and (q or not r)) -- return ((p == "0" or q == "0") and r == "1") or (p == "1" and (q == "1" or r == "0")) end
Is not working. The outputs are blank(for false) and “?”(for true) Here is a working example. Thank you for enhancing the code. Best regards, Dalyoung \startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|" 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 local tf = { true, false } function document.truthTable(a,func) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) 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(func(s,t,u)) context.NC() context.AR() end if #a == 3 then context(func(s,t)) context.NC() context.AR() end end end document.MakeFooter() end \stopluacode —- We define a logic function before we call the main function. —- by changing the function, we get the result for two variables. \startluacode --statistics.starttiming() local function logicF(p,q,r) if (not q) and (p or r) then return "1" else return "0" end —- return (not q) and (p or r) end document.truthTable({ "x", "y","z","f(x,y,x)"},logicF) \stopluacode \stoptext
On 1/7/2025 10:03 PM, Jeong Dal via ntg-context wrote:
Dear Hans,
It is nice to input the function as an argument. It is suffice to convey one function at each all.
But the definition of the function
local function logicF3(p,q,r) return ((not p or not q) and r) or (p and (q or not r)) -- return ((p == "0" or q == "0") and r == "1") or (p == "1" and (q == "1" or r == "0")) end
Is not working. The outputs are blank(for false) and “?”(for true)
Here is a working example.
Thank you for enhancing the code.
Best regards,
Dalyoung
\startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|" 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
local tf = { true, false }
function document.truthTable(a,func) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) 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(func(s,t,u)) context.NC() context.AR() end if #a == 3 then context(func(s,t)) context.NC() context.AR() end end end document.MakeFooter() end \stopluacode
—- We define a logic function before we call the main function. —- by changing the function, we get the result for two variables.
\startluacode --statistics.starttiming() local function logicF(p,q,r) if (not q) and (p or r) then return "1" else return "0" end —- return (not q) and (p or r) end
document.truthTable({ "x", "y","z","f(x,y,x)"},logicF) \stopluacode
\stoptext
the magic has to come from: context(func(s,t,u) and "1" or "0") context(func(s,t) and "1" or "0") then local function logicF(p,q,r) return (not q) and (p or r) end should work \startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|" 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 local tf = { true, false } function document.truthTable(a,func) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) 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(func(s,t,u) and "1" or "0") context.NC() context.AR() end if #a == 3 then context(func(s,t) and "1" or "0") context.NC() context.AR() end end end document.MakeFooter() end \stopluacode \startluacode local function logicF(p,q,r) return (not q) and (p or r) end document.truthTable({ "x", "y","z","f(x,y,x)"},logicF) \stopluacode ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
Dear Hans, I am sorry that I miss “context(func(set,u) and “1” or “0”) In your code. Correcting it, it works perfectly. By changing the logic function, we can draw truth tables upto 3 variables. After making a truth table, I draw Karnaugh Map of that table. I made a short MetaFun code for drawing Karnaugh map for the class material. In that code, it calculate the truth value using MetaFun function again. Recently, I read about “How to bring the data made by Lua into MetaFun.” So, my next try is to bring the truth value which is made in Lua. Then I omit the truth-value-calculation part from the MetaFun code. If there is a way to draw a diagonal in the \starttabulate ..., then we may use Lua code to do the job. In the wiki, there is an example of diagonal rule. https://wiki.contextgarden.net/TABLE#Diagonal_rules I tried it in \starttabulate … \stoptabulate, but it is not working. And the shape drawn using MetaFun is much better. Anyway, I’ll try it. Thank you again. Best wishes, Dalyoung PS: Here is my code which produces the truth table and its Karnaugh Map. The code for the truth table is cleaned up by Hans, but the MetaFun part will be changed. \startMPdefinitions numeric tVal[][],rlSkip[],rlNum[],uk, wd, ht; string val[],code[],subcode; uk := LineHeight; wd := 1.5uk; ht := uk+5pt; vardef funcOr(expr a,b) = if a + b = 0: out := 0; else: out := 1; fi; out enddef; vardef funcAnd(expr a,b) = if a + b > 1: out := 1; else: out := 0; fi; out enddef; vardef funcNot(expr a) = if a = 0: out:=1; else: out:=0 fi; out enddef; def KmapFrame(expr first,second) = val[0]:=first; val[1]:=second; for k=0,1: rlSkip[k] := length val[k]; rlNum[k] := 2*rlSkip[k]; if rlSkip[k] = 1: code[k] := "01"; else: code[k] :="00011110"; fi; endfor; label.top(textext("$" & first & "$"), (-.7wd,0)); label.top(textext("$" & second & "$"), (-.3wd,.5ht)); % drawing lines draw (-1.2wd,0) -- origin -- ((rlNum[1]+1)*wd,0); draw (0, 1.1ht) -- origin -- (0,-(rlNum[0]+.2)*ht); draw (-wd,ht) --origin; for i=0 upto rlNum[0]-1: subcode := substring (i*rlSkip[0],(i+1)*rlSkip[0]) of code[0]; label.top(textext(subcode), ((-.7wd, -(i+1)*ht))); endfor; for i=0 upto rlNum[1]-1: subcode := substring (i*rlSkip[1],(i+1)*rlSkip[1]) of code[1]; label.top(textext(subcode), ((i+1)*wd,.3ht)); endfor; setbounds currentpicture to boundingbox currentpicture enlarged 2mm; draw boundingbox currentpicture; enddef; \stopMPdefinitions \startbuffer[Kmap_xyz] KmapFrame("x", "yz"); vardef logicFunc(expr a,b,c) = result := funcAnd(funcNot(b), funcOr(a,c)); result enddef; for i=0,1: for j=0 upto rlNum[1]-1: b := scantokens(substring (j*rlSkip[1],j*rlSkip[1]+1) of code[1]); c := scantokens(substring (j*rlSkip[1]+1,j*rlSkip[1]+2) of code[1]); tVal[i][j] := logicFunc(i,b,c); label.top(tVal[i][j], ((j+1)*wd,-(i+1)*ht)); endfor; endfor; draw unitsquare xyscaled (1wd,2ht) shifted (1.5wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625blue ; draw unitsquare xyscaled (1.8wd,1ht) shifted (.6wd,-2.1ht) withpen pencircle scaled 2pt withcolor .625red ; \stopbuffer \starttext \startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|mcw(1cm)",#p-1) .. "|mcw(2cm)|" 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 local tf = { true, false } function document.truthTable(a,func) document.MakeHead(a) for i,s in ipairs(tf) do for j,t in ipairs(tf) do for k,u in ipairs(tf) 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(func(s,t,u) and "1" or "0") context.NC() context.AR() end if #a == 3 then context(func(s,t) and "1" or "0") context.NC() context.AR() end end end document.MakeFooter() end \stopluacode \startluacode --statistics.starttiming() local function logicF(p,q,r) return (p or r) and (not q) end document.truthTable({ "x", "y","z","f(x,y,x)"},logicF) \stopluacode \processMPbuffer[Kmap_xyz] \stoptext
On 1/3/2025 4:53 PM, Jeong Dal via ntg-context wrote:
Dear all,
I got the error "You can't use '\prevdepth' in restricted horizontal mode” when I compile the following code.
If I remove “\starttabulete… \stoptabulate”, then no errors, but the out put is not the form of table.
How can I fix this error?
Thank you.
Best regards,
Dalyoung
\starttext
\startluacode function makeHead(a) local n = #a context.HL() for i = 1, n do context.NC() context(a[i]) end context.NC()context.AR() context.HL() end \stopluacode
%\starttabulate[|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(1cm)|] \startluacode local a = {"x","y","z","(x\\to y)"} makeHead(a) \stopluacode %\stoptabulate
\stoptext
or \starttext \startluacode function document.MakeHead(p,a) if not a then local t = p p = string.rep("|k.5",#p) .. "|" 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() context.stoptabulate() end \stopluacode \startluacode document.MakeHead ( "|mcw(1cm)|mcw(1cm)|mcw(1cm)|mcw(2cm)|", { "x", "y", "z", "(x \\to y)" } ) \stopluacode \startluacode document.MakeHead { "x", "y", "z", "(x \\to y)" } \stopluacode \stoptext ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (5)
-
Florent Michel
-
Hans Hagen
-
Hans Hagen
-
Jeong Dal
-
Wolfgang Schuster