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
General rules
- This is code-golf, so shortest answer in bytes wins.
- Default Loopholes are forbidden.
6 Answers 6
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 ¦.)
-
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\$Erik the Outgolfer– Erik the Outgolfer2017年11月02日 12:32:42 +00:00Commented Nov 2, 2017 at 12:32
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
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.
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"]
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).
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)])))
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}'
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
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.
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="&")}
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)
cccccinstead ofcc, but leave|calone... And yes, in this table, all spaces and newlines are optional, but I would avoid blank lines. \$\endgroup\$