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}]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s