LaTeX truth tables

Charcoal, 70 bytes

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

⪫✂β⁰Iθ¹&⁰&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﹪÷κX²λ²⟦ι⟧&¦\\

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


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.


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

Tags:

Math

Code Golf