[Tex/LaTex] Macro for Automating Truth Tables

logicmacrosprogrammingtables

I used this truth table when writing a proof.enter image description here
Unfortunately I made a tiny typo; The first time around, I misplaced an additional ʹ symbol, which just happened to make the output exactly wrong. It ended up taking me forever to understand why nothing after that seemed to add up the way I knew it should.

It led me to wonder. Is it possible to build some kind of macro that will allow me to generate accurate truth tables according to a given input? In other words: I'd like it to work something like:

\truthtable{A, B, ( A \oplus B )', (A) \oplus (B')}

As opposed to having to draw the entire thing manually, potentially making errors. How would you even begin to program something like this in LaTeX?

Here's the full manual MWE for the table pictured above:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%hdashline
\usepackage{array}
\usepackage{arydshln}
\setlength\dashlinedash{0.2pt}
\setlength\dashlinegap{1.5pt}
\setlength\arrayrulewidth{0.3pt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
\begin{table}[htbp!]
\centering
\caption{}
\label{tab}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
\begin{tabular}{@{}cccc@{}}
\toprule%%–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
$A$ & $B$ & $(A \oplus B)'$ & $(A) \oplus (B')$ \\
\midrule%%–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
0 & 0 & 1 & 1 \\ \hdashline%%··········································
0 & 1 & 0 & 0 \\ \hdashline%%··········································
1 & 0 & 0 & 0 \\ \hdashline%%··········································
1 & 1 & 1 & 1 \\ \bottomrule%––––––––––––––––––––––––––––––––––––––––––
\end{tabular}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{table}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

Best Answer

This is something quickly written. Does not steer the Mars rover, in fact it is just a one-liner. If you make your code copyable I will add all the table options, if needed. (In fact, I made zero efforts to make the output "pretty", nor to suppress spaces or empty lines.) Also one can use a loop to create all the rows. I added explanations to the output.

\documentclass{article}
\usepackage{pgf}
\newcounter{step}
\newcommand{\myrow}[2]{ #1 & #2 & 
 \pgfmathparse{not(int(mod(#1+#2,2)))}\pgfmathresult & 
 \pgfmathparse{int(mod(#1+not(#2),2))}\pgfmathresult\\
 }
\setcounter{step}{0}
\def\tabcontent{\stepcounter{step}\ifnum\value{step}<5
\pgfmathtruncatemacro{\myA}{(\value{step}-1)/2}%
\pgfmathtruncatemacro{\myB}{mod(\value{step}-1,2)}%
\edef\temp{\noexpand\myrow{\myA}{\myB}}\temp%
\tabcontent\fi}
\begin{document}
In the first example (Table~\ref{tab:First}), explicit macros
\verb|\myrow{A}{B}| are used for each row. The \verb|\myrow| macro takes two
arguments, which are $A$ and $B$ in your application. 

\begin{table}[!h]
\centering
\begin{tabular}{c@{}cccc@{}}
 ~$A$~ & ~$B$~ & $(A \oplus B)'$ & $(A) \oplus (B')$ \\
 \hline
 \myrow{0}{0}
 \myrow{0}{1}
 \myrow{1}{0}
 \myrow{1}{1}
\end{tabular} 
\caption{First example.}
\label{tab:First}
\end{table}

The important point is that the other entries of the remaining columns can be
computed with \texttt{pgf} via \verb|\pgfmathparse{not(int(mod(#1+#2,2)))}| and
\verb|\pgfmathparse{int(mod(#1+not(#2),2))}|, respectively. I strongly suspect
that other packages like \texttt{xint} allow you to do similar things. However,
I am most familiar with \texttt{pgf}.

In the second example (Table~\ref{tab:Second}), a loop produces the content
of the table. Since the \& character is notoriously nasty, this loop is realized
as a recursive macro. Alternatives to this recursion include the \verb|\gappto|
macro that comes with the \texttt{etoolbox} package.
\begin{table}[!h]
\centering
\begin{tabular}{c@{}cccc@{}}
 ~$A$~ & ~$B$~ & $(A \oplus B)'$ & $(A) \oplus (B')$ \\
 \hline
 \tabcontent
\end{tabular} 
\caption{Second example.}
\label{tab:Second}
\end{table}
\end{document}

enter image description here

Related Question