11
\$\begingroup\$

Write a program or a function that accepts the list of outputs from a logic function and outputs the LaTeX code for its truth table.

The inputs should be labeled as lowercase letters a-z, and the output should be labelled as F. The length of list of inputs will always be shorter than 2^25, which means that number of inputs will always be less than 25, so you can use letters from lowercase alphabet for input names.

Input

A number n of inputs and list of length 2^n of binary numbers which represents the outputs of a logical function.

Output

LaTeX code that produces the truth table for that function. Input and output values should be centered in rows. There must be a line between table header and its values and between inputs and output, so the code should be similar to that below.

\begin{tabular}{c * <NUMBER OF INPUTS>|c}
<INPUTS>&F\\
\hline
<INPUT VECTOR i>&<OUTPUT>\\
\end{tabular}

Example

Input:

2
[0, 0, 0, 1]

Output:

\begin{tabular}{cc|c}
a & b & F \\
\hline
0 & 0 & 0 \\
0 & 1 & 0 \\
1 & 0 & 0 \\
1 & 1 & 1 \\
\end{tabular}

Which when displayed in LaTeX shows the following truth table

Truth table

General rules

asked Nov 2, 2017 at 9:42
\$\endgroup\$
15
  • 3
    \$\begingroup\$ Is this challenge require exactly same output or any output that may produce same thing in TeX? \$\endgroup\$ Commented Nov 2, 2017 at 10:07
  • 2
    \$\begingroup\$ Any output that produces the same thing in TeX \$\endgroup\$ Commented Nov 2, 2017 at 10:21
  • 2
    \$\begingroup\$ Something I find tricky here not knowing TeX that well is that there might be other shorter ways to write the table formatting TeX code, or even some other way (package?) to produce the table. Whatever language I use, TeX golf is part of the challenge. Is there an online interpreter for TeX for convenience, and perhaps to make unambiguous what the exact implementation is? \$\endgroup\$ Commented Nov 2, 2017 at 11:04
  • 1
    \$\begingroup\$ Tip: The TeX code seems to work with all spaces and newlines removed. \$\endgroup\$ Commented Nov 2, 2017 at 11:11
  • 1
    \$\begingroup\$ Anyone who doesn't know how to do it in LaTeX, follow the example output above. If n=5, simple put ccccc instead of cc, but leave |c alone... And yes, in this table, all spaces and newlines are optional, but I would avoid blank lines. \$\endgroup\$ Commented Nov 2, 2017 at 11:20

6 Answers 6

10
\$\begingroup\$

Charcoal, 70 bytes

≔tabularζ\ζ{*θc|c}⸿⪫✂β0Iθ1&0&F\\⸿\hline⸿Eη+⪫+⮌EIθI%÷κX2λ2⟦ι⟧&¦\0円\endζ

Try it online! Link is to verbose version of code. Explanation:

≔tabularζ

Save this string in a variable to avoid duplication.

\ζ{*θc|c}⸿

Print the initial \tabular{*2c|c} line (2 or whatever value the first input q has).

⪫✂β0Iθ1&0&F\\⸿\hline⸿

Get the first q letters from the predefined variable b and insert &s between them, then append the &F\\ and also print \hline on the next line.

Eη+⪫+⮌EIθI%÷κX2λ2⟦ι⟧&¦\\

Loop over the characters in the second input. For each one, its index is converted to binary with length q, the character is concatenated, the result is joined with &s and \\ is appended. The resulting strings are implicitly printed on separate lines.

0\endζ

Print the \endtabular. (The 0 is just a separator as the deverbosifier forgots to insert a ¦.)

answered Nov 2, 2017 at 10:51
\$\endgroup\$
1
  • 3
    \$\begingroup\$ It's kinda impressive that Charcoal is currently the winner, given that this challenge isn't really what it's designed for. \$\endgroup\$ Commented Nov 2, 2017 at 12:32
6
\$\begingroup\$

Python 2, 153 bytes

lambda n,l:r'\tabular{*%dc|c}%s&F\\\hline%s\endtabular'%(n,q(map(chr,range(97,97+n))),r'\\'.join(q(bin(2**n+i)[3:]+x)for i,x in enumerate(l)))
q='&'.join

Try it online!

Outputs like

\tabular{*2c|c}a&b&F\\\hline0&0&0\0円&1&0\1円&0&0\1円&1&1\endtabular

\tabular and \endtabular are used as shorter \begin{tabular} and \end{tabular}, as per this LaTeX golf tip. The *2c is a shorthand to define 2 columns.

answered Nov 2, 2017 at 11:51
\$\endgroup\$
5
\$\begingroup\$

Haskell, (削除) 164 (削除ここまで) 155 bytes

s%f=((:"&")=<<s)++f:"\\\\"
n#r=unlines$("\\tabular{"++('c'<$[1..n])++"|c}"):take n['a'..]%'F':"\\hline":zipWith(%)(mapM id$"01"<$[1..n])r++["\\endtabular"]

Try it online!

unlines -- take a list of strings and join it with NL.
 -- the strings are:
 "\\tabular{"++('c'<$[1..n])++"|c}" -- tabular definition with n times 'c'
 take n['a'..]%'F' -- table header
 "\\hline" -- hline
 zipWith(%)(mapM id$"01"<$[1..n])r -- table content
 ["\\endtabular"] -- end of tabular definition
Table header and content are built via function '%'
s%f= -- take a string 's' and a char 'f'
 ((:"&")=<<s) -- append a "&" to each char in 's'
 ++f:"\\\\" -- and append 'f' and two backslashes
Table header:
take n['a'..] % 'F' -- s: the first n letters from the alphabet
 -- f: char 'F'
Table content:
zipWith(%) -- apply '%' pairwise to
 mapM id$"01"<$[1..n] -- all combinations of '0' and '1' of length n
 r -- and the string 'r' 

Edit: using \tabular instead of \begin{tabular} (stolen from @xnor's answer).

answered Nov 2, 2017 at 11:44
\$\endgroup\$
3
\$\begingroup\$

Python 2, (削除) 192 (削除ここまで) (削除) 168 (削除ここまで) 166 bytes

lambda n,l:r'\begin{tabular}{*%dc|c}%s\end{tabular}'%(n,r'\\'.join(map('&'.join,[map(chr,range(97,97+n))+[r'F\\\hline']]+[bin(2**n+i)[3:]+l[n]for i in range(2**n)])))

Try it online!

Pretty printed version:

Python 2, (削除) 234 (削除ここまで) (削除) 229 (削除ここまで) (削除) 218 (削除ここまで) (削除) 209 (削除ここまで) (削除) 205 (削除ここまで) 203 bytes

n,l=input()
print'\\begin{tabular}{'+'c'*n+'|c}\n'+' & '.join(chr(i+97)for i in range(n)+[-27]),'\\\\\n\hline'
i=0
for r in l:print' & '.join(bin(i)[2:].rjust(n,'0')+`r`),r'\\';i+=1
print'\\end{tabular}'

Try it online!

answered Nov 2, 2017 at 10:20
\$\endgroup\$
2
\$\begingroup\$

Proton, 142 bytes

n=>x=>"\\tabular*#{n}c|c#{j(map(chr,97..97+n))}&F\\\\\hline"+'\\\\'.join(j(bin(i)[2to].zfill(n)+x[i])for i:0..len(x))+"\\endtabular"j="&".join

Try it online!

Output is in golfed LaTeX form; thanks to xnor for that trick!

(削除) This should be able to be golfed to shorter than xnor's Python answer because Proton should in theory never lose to Python lol (in practice I'm bad xD). I may steal some tricks from xnor ;P (削除ここまで)

Managed to now be shorter by making some things into variables, which I just noticed xnor also did :P

And there we go, -6 bytes by using some Proton golfing tricks.

answered Nov 2, 2017 at 12:33
\$\endgroup\$
1
\$\begingroup\$

R, (削除) 196 187 (削除ここまで) 171 bytes

function(m,n){cat("\\tabular{*",n,"c|c}")
write(c(letters[1:n],"F\\\\\\hline",rbind(t(rev(expand.grid(rep(list(0:1),n)))),paste0(m,"\\\\")),"\\endtabular"),1,n+1,sep="&")}

Try it online!

Output similar to the Charcoal answer. expand.grid from this answer.

For the record, using xtable from the eponym package is not (削除) much (削除ここまで) shorter since one has to specify a lot of options to match the spec, in addition to including the package:

R, 187 bytes

function(m,n){u=rbind(apply(expand.grid(rep(list(0:1),n)),1,rev),m)
rownames(u)=c(letters[1:n],"F")
print(xtable(t(u),dig=0,align=c(rep("c",n+1),"|c}")),hl=0,include.r=F)}
library(xtable)

Try it online!

answered May 30, 2018 at 19:04
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.