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.


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


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}



[0, 0, 0, 1]


a & b & F \\
0 & 0 & 0 \\
0 & 1 & 0 \\
1 & 0 & 0 \\
1 & 1 & 1 \\

Which when displayed in LaTeX shows the following truth table

Truth table

General rules

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

6 Answers 6


Charcoal, 70 bytes


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


Save this string in a variable to avoid duplication.


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


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.


Print the \endtabular. (The 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\$ Commented 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)))

Try it online!

Outputs like


\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

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


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

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

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


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


Try it online!


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.