# How to draw Venn diagrams (especially: complements) in LaTeX

What I am up to is to write some exercises dealing with logical formulas for my students, like: And the students should draw these formulas on Venn diagrams. At the end of the lesson, I really would like to print the correct answer for them. I found a great resource on a forum thread at latex-community.org, which helped me a lot to make up some Venn diagrams with tikz, but have some problems with visualizing complements, like ~A.

A simple, modified version of the TeX file found on the forum linked above, can be seen below, which produces the following expression: \documentclass{letter}
\usepackage{tikz}
\def\firstcircle{(90:1.75cm) circle (2.5cm)}
\def\secondcircle{(210:1.75cm) circle (2.5cm)}
\def\thirdcircle{(330:1.75cm) circle (2.5cm)}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip \secondcircle;
\fill[cyan] \thirdcircle;
\end{scope}
\begin{scope}
\clip \firstcircle;
\fill[cyan] \thirdcircle;
\end{scope}
\draw \firstcircle node[text=black,above] {$A$};
\draw \secondcircle node [text=black,below left] {$B$};
\draw \thirdcircle node [text=black,below right] {$C$};
\end{tikzpicture}
\end{document}


Which looks like: Could anyone please help me out plotting/defining some expressions dealing with complements? A nice example could be: That should look like: (image from Wikipedia) I do not insist on the red color :)

I would like to use the simplest possible solution, as I would like to mass generate the exercises with the help of R. So any suggestion dealing with gnuplot, R or any other opensource packages is welcome. Thank you!

Thank you @Leo Liu, you helped me a lot! I modified a bit the code you suggested to be able to color the area outside of the two circles also (in the H universe), but have no idea how to set a background to that polygon also. The code:

\begin{tikzpicture}[fill=gray]
% left hand
\scope
\clip (-2,-2) rectangle (2,2)
(1,0) circle (1);
\fill (0,0) circle (1);
\endscope
% right hand
\scope
\clip (-2,-2) rectangle (2,2)
(0,0) circle (1);
\fill (1,0) circle (1);
\endscope
% outline
\draw (0,0) circle (1) (0,1)  node [text=black,above] {$A$}
(1,0) circle (1) (1,1)  node [text=black,above] {$B$}
(-2,-2) rectangle (3,2) node [text=black,above] {$H$};
\end{tikzpicture}


And the image generated: I will also look for even odd rule in the near future which does not make sense for me at the moment but looks really simple and promising!

• Not super relevant for drawing complements, but for venn diagrams in tikz you could check out: texample.net/tikz/examples/venn-diagram – Seamus Jan 25 '11 at 19:58
• Although a lot of time has passed, someone who wander here might also find this link helpful. – quapka Apr 19 '14 at 15:42

There are several ways to draw Venn diagrams. The simplest for $\overline{A \cap B}$ may be:

\tikz \fill[even odd rule] (0,0) circle (1) (1,0) circle (1);


The key to this question is even odd rule in TikZ (based on PostScript and PDF). Moreover, you can also use \clip to fill the complement of a set, without using even odd rule:

\begin{tikzpicture}[fill=gray]
% left hand
\scope
\clip (-1,-1) rectangle (2,1)
(1,0) circle (1);
\fill (0,0) circle (1);
\endscope
% right hand
\scope
\clip (-1,-1) rectangle (2,1)
(0,0) circle (1);
\fill (1,0) circle (1);
\endscope
% outline
\draw (0,0) circle (1)
(1,0) circle (1);
\end{tikzpicture} Here, we find out that TikZ is lack of a \unfill command which is provided by MetaPost, thus we must use an extra rectangle to clip the path.

For updated question:

Well, I must say that this will be easier, if you fill $A \cap B$ with white color:

\begin{tikzpicture}
\filldraw[fill=gray] (-2,-2) rectangle (3,2);
\scope % A \cap B
\clip (0,0) circle (1);
\fill[white] (1,0) circle (1);
\endscope
% outline
\draw (0,0) circle (1)
(1,0) circle (1);
\end{tikzpicture} However, it is not so easy to fill such a area using clipping (warning: it's somewhat difficult to use, only for fun):

\begin{tikzpicture}[fill=gray]
% left hand
\scope
\clip (-2,-2) rectangle (0.5,2)
(1,0) circle (1);
\clip (-2,-2) rectangle (0.5,2);
\fill (-2,-2) rectangle (3,2);
\endscope
% right hand
\scope
\clip (0.5,-2) rectangle (3,2)
(0,0) circle (1);
\clip (0.5,-2) rectangle (3,2);
\fill (-2,-2) rectangle (3,2);
\endscope
% outline
\draw (-2,-2) rectangle (3,2);
\draw (0,0) circle (1)
(1,0) circle (1);
\end{tikzpicture}


Hints:

• The result using multiple path in one \clip command depends on the direction of the path. • Use another \clip again to get rid of the half circle being filled.
• Thank you @Leo Liu, that really looks simple and perfect! I need some time to read about this solution and understand the syntax, I will be back to accept the answer after some experiment! – daroczig Jan 25 '11 at 17:30
• I have just realized, that the example is not correct, as the area outside of both circles also should be colored. I added more details to my question (with example image). – daroczig Jan 25 '11 at 18:13
• @daroczig: I updated the answers, one is easy to use and the other using clipping is more powerful. – Leo Liu Jan 25 '11 at 19:59

An example for Venn diagrams with transparency by Till Tantau and Kjell Magne Fauske, from the TikZ Example gallery:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\begin{document}
\pagestyle{empty}
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(60:2cm) circle (1.5cm)}
\def\thirdcircle{(0:2cm) circle (1.5cm)}
\begin{tikzpicture}
\begin{scope}[shift={(3cm,-5cm)}, fill opacity=0.5]
\fill[red] \firstcircle;
\fill[green] \secondcircle;
\fill[blue] \thirdcircle;
\draw \firstcircle node[below] {$A$};
\draw \secondcircle node [above] {$B$};
\draw \thirdcircle node [below] {$C$};
\end{scope}
\end{tikzpicture}
\end{document} • Thank you @Stefan for posting (+1), this is really great indeed. – daroczig Aug 28 '11 at 15:27
• How are the color values combined in the overlap? It doesn't seem like red+green+blue should make deep blue. – bright-star Jan 15 '14 at 2:32
• @Trevor Alexander: I guess that is because of the order -first red is mixed with the white backgrund, and then green is put on top of that. Finally blue is mixed on top of that color to give the deep blue. – hpekristiansen May 5 '15 at 2:37

run it with xelatex if you need a pdf

\documentclass{minimal}

\usepackage{pstricks}

\begin{document}

\begin{pspicture}(6,4)
\psset{linewidth=1.5pt}
\psframe[fillcolor=red!30,fillstyle=solid](6,4)
\psclip{\pscircle(2,2){1.5}}
\pscircle[fillcolor=white,fillstyle=solid](4,2){1.5}
\endpsclip
\pscircle(4,2){1.5}\pscircle(2,2){1.5}
\end{pspicture}

\end{document} • Thank you very much @Herbert! I am not familiar with pstricks and XeTeX yet, but these look promising. I will look after based on your suggestion. – daroczig Jan 25 '11 at 21:21

A solution with the new package tkz-euclide ( based on TikZ )

\documentclass{scrartcl}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-euclide}
\usetkzobj{all}
\definecolor{fondpaille}{cmyk}{0,0,0.1,0}
\color{Maroon}
\tkzSetUpColors[background=fondpaille,text=Maroon]

\begin{document}

\begin{tikzpicture}
\tkzDefPoint(0,0){A}
\tkzDefPoint(4,0){B}
\begin{scope}
\tkzClipCircle(A,B) \tkzClipCircle(B,A)
\tkzFillCircle[color=blue!20](A,B)
\end{scope}
\tkzDrawCircle(A,B)
\tkzDrawCircle(B,A)
\end{tikzpicture}

\end{document} • and by the way, the optional argument usenames for xcolor  is obsolete for years ... – user2478 Jan 27 '11 at 19:28
• @ Herbert Why ? I have this option in the xcolor documentation and I use some color's names from this option. I never see a warning with this option but I suppose that you are right. Where can I find this information ? – Alain Matthes Jan 28 '11 at 6:26
• @AlainMatthes how can I add a label (A, B) to each of the circles and a label (H) to the universe? – cacamailg Sep 12 '14 at 23:16

If you use MetaPost or Asymptote, there will be a different method: buildcycle.

For example, Asymptote:

size(200);
defaultpen(black+1);

pair A = (0,0), B = (1,0);
path inter = buildcycle(arc(A,1,-90,90), arc(B, 1,90,270));
path outer = box((-2,-2), (3,2));

fill(outer, mediumgray); unfill(inter);
// or use:
//     fill(outer ^^ inter, evenodd+mediumgray);
draw(outer ^^ circle(A,1) ^^ circle(B,1)); And you don't have to use a language to draw Venn diagrams. Inkscape can also deal with them.

• @Leo Liu: and thanks also for this alternate solution! – daroczig Jan 25 '11 at 21:20
• @Leo How to manage the font with Inskape ? I think it's not a good solution with LaTeX. The best solutions and very well integrated are pstricks and tikz. Tools like geogebra or texgraph are fine but you can not adjust the size of your figures automatically in conjunction with the text. – Alain Matthes Jan 27 '11 at 16:59
• @Altermundus: There are some extensions to enable Inkscape to use LaTeX labels. See wiki.inkscape.org/wiki/index.php/ExtensionsRepository. – Leo Liu Jan 27 '11 at 19:07
• @Leo (+1) The venn.mp macro might be useful too. – chl Jan 27 '11 at 20:24
• @Leo And for Asymptote, see e.g. asymptote.sourceforge.net/doc/LaTeX-usage.html. – chl Jan 27 '11 at 20:27

User defined constants:

    \const{HalfCanvas}{1.5}% half of canvas width or height
\const{InitAngleD}{30}% initial angle \documentclass{beamer}
\usepackage[nomessages]{fp}

\def\const#1#2{%
\expandafter\FPeval\csname#1\endcsname{round(#2:3)}%
\pstVerb{/#1 \csname#1\endcsname\space def}%
}

\usepackage[active,tightpage]{preview}
\PreviewBorder=12pt
\PreviewEnvironment{pspicture}

\def\init{%
%
% user defined constants
\const{HalfCanvas}{1.5}% half of canvas width or height
\const{InitAngleD}{30}% initial angle
%
% internal used constants
\const{AngleBD}{InitAngleD+120}%
\const{AngleCD}{InitAngleD+240}%
}

\def\hold{\psgrid\pause}

\begin{document}
\begin{frame}
\begin{pspicture}(-3,-3)(3,3)
\init
\pnode(\MainR;\AngleBD){B}
\pnode(\MainR;\AngleCD){C}

\psset{linestyle=none,fillstyle=solid,style=gridstyle,opacity=0.999}\hold
\pscircle[fillcolor=blue](C){!ChildR}\hold

\begin{psclip}{\pscircle[fillcolor=green](B){!ChildR}}
\pscircle[fillcolor=red](C){!ChildR}
\end{psclip}\hold

\begin{psclip}{\pscircle[fillcolor=red](A){!ChildR}}
\pscircle[fillcolor=blue](B){!ChildR}
\end{psclip}\hold

\begin{psclip}{\pscircle[fillstyle=none](A){!ChildR}}
\pscircle[fillcolor=green](C){!ChildR}
\end{psclip}\hold

\psset{fillstyle=none}
\begin{psclip}{\pscircle(A){!ChildR}\pscircle(C){!ChildR}}
\pscircle[fillstyle=solid,fillcolor=white](B){!ChildR}
\end{psclip}\hold

\psset{linestyle=solid}
\pscircle(A){!ChildR}\hold
\pscircle(B){!ChildR}\hold
\pscircle(C){!ChildR}
\end{pspicture}
\end{frame}
\end{document}


There's a venn package on CTAN:

"Creating Venn diagrams with MetaPost."

draw_venn_two(true,false,true,false) shifted (2in,1in);


draws a diagram with the outer box shaded, with the first circle but not the second unshaded, with the intersection of the two shaded, and with the second circle but not the first unshaded. That is, this is a picture of the complement of the symmetric difference of A and B.

• Thanks for this simple solution (+1), I'll have to check it out. – daroczig Aug 28 '11 at 15:29
• The "package" shouldn't be called "package". There is no documentation for it. – buhtz Jan 28 '16 at 10:24

Here is a nice introduction I found to drawing Venn diagrams:

http://users.ju.edu/hduong/math220/venn_diagrams.pdf

I put it here, since this question is one of the first results google gives when looking for "how to draw Venn diagrams" (naturally), and also other places refer here, but I feel none of the answers give an introduction to drawing Venn diagrams for someone who doesn't know anything about using TikZ (as I was when I reached here). This introduction does explain the basics rather well.

• I would suggest actually including what you can from the linked post in a full-fledged answer, rather than just linking to an outside source (thereby avoiding potential future link rot). – Werner Dec 9 '14 at 18:21
• This is indeed useful, thank you @ur-ben-ari-tishler – daroczig Dec 9 '14 at 19:04
• The link attached in this answer is no longer useful. It is broken. – Cragfelt Mar 2 at 0:37

There is a simple package to Venn diagrams, maybe somebody likes this: package venndiagram. I'm using it, it's enough for me.