# Example: Set operations illustrated with Venn diagrams

Published 2010-03-18 | Author: Uwe Ziegenhagen

This is an expanded version of an example provided by T. Tantau

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

\documentclass{article}
% Set operations illustrated with Venn diagrams
% Author: Uwe Ziegenhagen
% This is an expanded version of an example provided by T. Tantau

\usepackage{tikz}
\begin{document}

% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}

\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}

\tikzset{filled/.style={fill=circle area, draw=circle edge, thick},
outline/.style={draw=circle edge, thick}}

\setlength{\parskip}{5mm}
% Set A and B
\begin{tikzpicture}
\begin{scope}
\clip \firstcircle;
\fill[filled] \secondcircle;
\end{scope}
\draw[outline] \firstcircle node {$A$};
\draw[outline] \secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$A \cap B$};
\end{tikzpicture}

%Set A or B but not (A and B) also known a A xor B
\begin{tikzpicture}
\draw[filled, even odd rule] \firstcircle node {$A$}
\secondcircle node{$B$};
\node[anchor=south] at (current bounding box.north) {$\overline{A \cap B}$};
\end{tikzpicture}

% Set A or B
\begin{tikzpicture}
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$A \cup B$};
\end{tikzpicture}

% Set A but not B
\begin{tikzpicture}
\begin{scope}
\clip \firstcircle;
\draw[filled, even odd rule] \firstcircle node {$A$}
\secondcircle;
\end{scope}
\draw[outline] \firstcircle
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$A - B$};
\end{tikzpicture}

% Set B but not A
\begin{tikzpicture}
\begin{scope}
\clip \secondcircle;
\draw[filled, even odd rule] \firstcircle
\secondcircle node {$B$};
\end{scope}
\draw[outline] \firstcircle node {$A$}
\secondcircle;
\node[anchor=south] at (current bounding box.north) {$B - A$};
\end{tikzpicture}

\end{document}


• #1 Alex Mikhalev, April 1, 2010 at 6:03 p.m.

Thank you, very useful example.

• #2 Muhammad, April 10, 2010 at 1:55 p.m.

Many thanks for such a useful example :-)

• #3 Lennart, April 23, 2010 at 5:45 p.m.

Great examples for clipping! Thanks!

• #4 Bliu, November 10, 2012 at 4:39 p.m.

How can I draw a rectangle arround the circles?

• #5 avenej, June 26, 2013 at 4:23 a.m.

would please,. what or how should it done? 