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
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : [email protected] / 
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
___________________________________________________________________________________

Reply via email to