Truth Tables in LaTeX

Typesetting truth tables has never been easy. LaTeX is the gold standard for displaying logic and mathematics, but tables are awkward to edit at best. Tables are much simpler in Microsoft Word, but displaying formulas is a horrible experience.1 Here is my current workflow.

The text that I’m using this semester is Introduction to Formal Logic with Philosophical Applications by Russell Marcus. Instead of arrows and the ampersand, it uses the horseshoe, triple bar, and dot. So, I add the following lines to my LaTeX preamble to simplify entering the symbols.2

\newcommand{\lneg}{\mathord{\sim}}
\renewcommand{\land}{\bullet}
\newcommand{\lif}{\supset}
\newcommand{\liff}{\equiv}

Then, I enter the truth table in either Excel or Numbers. For example, this would be a simple one line table determining the truth value of a formula for a given valuation:

Numbers truth table

Copy the cells that you want included in the truth table. Go to Tables Generator, and select “LaTeX Tables” from the top menu bar. Below the top menu bar is a drop-down menu bar. Click on “File” then “Paste table data…” and paste the table data. Table Generator will generate a nicely formatted LaTeX table:

\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{llllllll}
P & Q & R & P & \lif & (\lneg Q & \land & R) \\
1 & 1 & 1 & 1 & 0 & 0 & 0 & 1
\end{tabular}
\end{table}

I delete the first four lines and the last line, leaving just the table data and the lines declaring the tabular environment:

\begin{tabular}{cccccccc}
P & Q & R & P & \lif & (\lneg Q & \land & R) \\
1 & 1 & 1 & 1 & 0 & 0 & 0 & 1
\end{tabular}

At this point, typesetting will fail because the symbols need to be in math mode. So, I’ve found two options. The first is to put all the commands for the symbols in math mode:

\begin{tabular}{cccccccc}
P & Q & R & P & \(\lif\) & (\(\lneg\) Q & \(\land\) & R) \\
1 & 1 & 1 & 1 & 0 & 0 & 0 & 1
\end{tabular}

The second option is to change “tabular” to “array” and put the entire table into math mode:

\[
\begin{array}{cccccccc}
P & Q & R & P & \lif & (\lneg Q & \land & R) \\
1 & 1 & 1 & 1 & 0 & 0 & 0 & 1
\end{array}
\]

Arrays are centered on the page. If you would prefer them printed at the left margin, add “fleqn” to the document class options: \documentclass[fleqn]{article} Since the array is in math mode, the letters will be italicized. I use the newtxmath font package, and it has a “frenchmath” option that sets the math font to non-italic. Other math fonts may have a similar option.

Finally, whichever option is used, we need to add two lines. Adding a vertical line character to the table or array formatting options will place a vertical line between the valuation section and the rest of the truth table. Adding the booktabs package to the preamble will allow us to separate the sentence from the rest of the truth table.

This gives us the final version,

\[
\begin{array}{ccc|ccccc}
P & Q & R & P & \lif & (\lneg Q & \land & R) \\ \midrule
1 & 1 & 1 & 1 & 0 & 0 & 0 & 1
\end{array}
\]

which produces this:

Truth Table


  1. Apple’s Pages now allows users to add formulas with LaTeX. It’s looking like a good solution for those who like more traditional word processors. 
  2. The AMS LaTeX packages already include a command called “\lor” for entering the vee or wedge. 

Venn Diagrams with LaTeX and TikZ

I spent the weekend working on Venn diagrams in LaTeX using TikZ to use when teaching categorical logic. I thought it was a much better use of my time than mowing the lawn, and one never knows when a good Venn diagram will be needed.

The Initial Diagram

First, add the following to the preamble:

usepackage{tikz}nusetikzlibrary{shapes,backgrounds}

The next step is to define the three circles in the body of the document. This only needs to to be done once; they can be called repeatedly in the same document. I named them in a way that I could remember which was the subject term, middle term, and predicate term:

defsub{(0,0) circle (1.5cm)}ndefmid{(-60:2cm) circle (1.5cm)}ndefpred{(0:2cm) circle (1.5cm)}

Once the circles have been defined, they can be drawn and labeled. I prefer to use a heavier line than the default, that can be called as an option to tikzpicture.

begin{tikzpicture}[thick]nn  begin{scope}nn    % Draw the circlesn    draw sub;n    draw pred;n    draw mid;nn    % Label the circlesn    draw (-2,-0) node {$S$};n    draw (1,-4) node {$M$};n    draw (4,0) node {$P$};nn  end{scope}nnend{tikzpicture}

This produces the following:

Venn Diagram with Labels

Shading

Universal Negations

Shading is simple with TikZ, and, in most cases, relatively intuitive. To shade an entire circle, use the “fill” command. For example, “fill sub” fills the entire subject circle. To fill the intersection of the subject and predicate circles, we need to tell TikZ to ignore everything that is outside the predicate circle, then fill the subject circle. This is done with the “clip” command. This code shades the intersection of S and P:

begin{tikzpicture}[thick]n  begin{scope}n    begin{scope} %Shade intersection of S and Pn      clip pred;n      fill[gray] sub;n    end{scope}nn    draw sub;n    draw pred;n    draw mid;nn    draw (-2,-0) node {$S$};n    draw (1,-4) node {$M$};n    draw (4,0) node {$P$};n  end{scope}nend{tikzpicture}

Universal Affirmations

Shading everything that is one circle, but not in another is a bit trickier. It involves using something called the “even odd rule” in TikZ. Since I don’t quite understand how the rule works, this involved a bit of trial-and-error on my part. If there is a more efficient way, then please leave a comment. This code shades the portion of the subject circle that is not in the predicate circle:

begin{tikzpicture}[thick]n  begin{scope}[even odd rule]% Shade S without Pn    clip pred (-1.5,-1.5) rectangle (1.5,1.5);n    fill[gray] sub;n  end{scope}nn  draw sub;n  draw pred;n  draw mid;nn  draw (-2,-0) node {$S$};n  draw (1,-4) node {$M$};n  draw (4,0) node {$P$};nend{tikzpicture}

To shade S without M:

begin{scope}[even odd rule]% Shade *S* without Mn  clip mid (-1.5,-1.5) rectangle (1.5,1.5);n  fill[gray] sub;nend{scope}

M without S:

begin{scope}[even odd rule]% Shade M without Sn  clip sub (-0.5,-3.3) rectangle (2.5,0);n  fill[gray] mid;nend{scope}

M without P:

begin{scope}[even odd rule]% Shade M without Pn  clip pred (-0.5,-3.3) rectangle (2.5,0);n  fill[gray] mid;nend{scope}

P without M:

begin{scope}[even odd rule]% Shade P without Mn  clip mid (-0,-1.5) rectangle (3.5,1.5);n  fill[gray] pred;nend{scope}

Finally, to shade P without S:

begin{scope}[even odd rule]% Shade P without Sn  clip sub (0,-1.5) rectangle (3.5,1.5);n  fill[gray] pred;nend{scope}

Particulars

I and O sentences require an “x” to be placed in some region of the diagram. To do that, just draw a node at the desired location. Here are the locations of the seven sections in the diagram:

draw (-0.5,0.3) node {1};ndraw (1,0.3) node {2};ndraw (2.5,0.3) node {3};ndraw (0.2,-0.9) node {4};ndraw (1,-0.6) node {5};ndraw (1.8,-0.9) node {6};ndraw (1,-2) node {7};

Sections in a Venn Diagram

Examples

Here are some examples to show how full arguments are diagrammed.

Celarent (EAE-1)

Celarent is a classic first-figure argument that uses both a universal negation and a universal affirmation.

begin{tikzpicture}[thick]nnbegin{scope}n    draw (-2,-0) node {$A$};n    draw (1,-4) node {$B$};n    draw (4,0) node {$C$};nnbegin{scope}[even odd rule]% Shade S without Mn            clip mid (-1.5,-1.5) rectangle (1.5,1.5);n        fill[gray] sub;n        end{scope}nnbegin{scope} %Shade intersection of M and Pn  clip pred;n  fill[gray] mid;nend{scope}nndraw sub;ndraw pred;ndraw mid;nend{scope}nnend{tikzpicture}

This produces the following:

Celarent

Disamis (IAI-3)

Disamis is a good example of a universal affirmation with a particular affirmation.

Disamis

It was made with this code:

begin{tikzpicture}[thick]nn  begin{scope}n    draw (-2,-0) node {$A$};n    draw (1,-4) node {$B$};n    draw (4,0) node {$C$};nn    begin{scope}[even odd rule]% Shade M without Sn      clip sub (-0.5,-3.3) rectangle (2.5,0);n      fill[gray] mid;n    end{scope}nn    draw (1,-.6) node {X};nn    draw sub;n    draw pred;n    draw mid;n  end{scope}nnend{tikzpicture}n  n  caption{Disamis}n  label{fig:disamis}nend{figure}

Scaling

TikZ graphics can be scaled easily with an option to the tikzpicture environment. This doubles the size of the picture:

begin{tikzpicture}[thick,scale=2]

This reduces the size by half:

begin{tikzpicture}[thick,scale=.5]

Unfortunately, this won’t scale the text at the nodes, which will make the labels look ridiculously out of proportion. This can be fixed by adding an option to also scale the nodes. So, this produces a double-size image with proper labels:

begin{tikzpicture}[thick,scale=2, every node/.style={transform shape}]