# LaTeX truth tables

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

• Is this challenge require exactly same output or any output that may produce same thing in TeX? – tsh Nov 2 '17 at 10:07
• Any output that produces the same thing in TeX – drobilc Nov 2 '17 at 10:21
• 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? – xnor Nov 2 '17 at 11:04
• Tip: The TeX code seems to work with all spaces and newlines removed. – xnor Nov 2 '17 at 11:11
• 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. – Heimdall Nov 2 '17 at 11:20

# Charcoal, 70 bytes

≔tabularζ\ζ{*θc|c}⸿⪫✂β⁰Ｉθ¹&⁰&F\\⸿\hline⸿Ｅη⁺⪫⁺⮌ＥＩθＩ﹪÷κＸ²λ²⟦ι⟧&¦\\⁰\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).

⪫✂β⁰Ｉθ¹&⁰&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.

Ｅη⁺⪫⁺⮌ＥＩθＩ﹪÷κＸ²λ²⟦ι⟧&¦\\

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.

⁰\endζ

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

• It's kinda impressive that Charcoal is currently the winner, given that this challenge isn't really what it's designed for. – Erik the Outgolfer Nov 2 '17 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

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.

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'

# Python 2, 192168 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, 234229218209205 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!

# 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.

# 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!