120

What I am up to is to write some exercises dealing with logical formulas for my students, like:

A \cup B

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:

enter image description here

\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:

enter image description here

Could anyone please help me out plotting/defining some expressions dealing with complements? A nice example could be:

$\overline{A \cap B}$

That should look like: (image from Wikipedia)

enter image description here

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!


UPDATE (25/01/2011): added details based on answers.

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:

enter image description here

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!

2
  • 5
    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, 2011 at 19:58
  • 8
    Although a lot of time has passed, someone who wander here might also find this link helpful.
    – quapka
    Apr 19, 2014 at 15:42

9 Answers 9

56

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).

use even odd rule

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}

use clipping

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}

fill with white

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.

Explanation for direction of the clipping path

  • Use another \clip again to get rid of the half circle being filled.
3
  • 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, 2011 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, 2011 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, 2011 at 19:59
42

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}

enter image description here

4
  • Thank you @Stefan for posting (+1), this is really great indeed.
    – daroczig
    Aug 28, 2011 at 15:27
  • How are the color values combined in the overlap? It doesn't seem like red+green+blue should make deep blue. Jan 15, 2014 at 2:32
  • 3
    @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. May 5, 2015 at 2:37
  • Is it possible to draw such diagrams without having to give exact coordinates?
    – clel
    Jun 7, 2022 at 13:15
17

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}

enter image description here

1
  • 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, 2011 at 21:21
17

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}

Intersection

3
  • and by the way, the optional argument usenames for xcolor is obsolete for years ...
    – user2478
    Jan 27, 2011 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 ? Jan 28, 2011 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, 2014 at 23:16
13

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));

result

And you don't have to use a language to draw Venn diagrams. Inkscape can also deal with them.

6
  • @Leo Liu: and thanks also for this alternate solution!
    – daroczig
    Jan 25, 2011 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. Jan 27, 2011 at 16:59
  • 1
    @Altermundus: There are some extensions to enable Inkscape to use LaTeX labels. See wiki.inkscape.org/wiki/index.php/ExtensionsRepository.
    – Leo Liu
    Jan 27, 2011 at 19:07
  • 1
    @Leo (+1) The venn.mp macro might be useful too.
    – chl
    Jan 27, 2011 at 20:24
  • @Leo And for Asymptote, see e.g. asymptote.sourceforge.net/doc/LaTeX-usage.html.
    – chl
    Jan 27, 2011 at 20:27
11

There's a venn package on CTAN:

"Creating Venn diagrams with MetaPost."

Example code (from README):

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.

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

User defined constants:

    \const{HalfCanvas}{1.5}% half of canvas width or height
    \const{InitAngleD}{30}% initial angle 
    \const{MainR}{1}% main radius
    \const{ChildR}{2}% child radius

enter image description here

\documentclass{beamer}
\usepackage{pstricks-add}
\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 
    \const{MainR}{1}% main radius
    \const{ChildR}{2}% child radius
    %
    % internal used constants
    \const{AngleAD}{InitAngleD}%
    \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;\AngleAD){A}
        \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}
0
8

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.

3
  • 2
    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, 2014 at 18:21
  • 1
    This is indeed useful, thank you @ur-ben-ari-tishler
    – daroczig
    Dec 9, 2014 at 19:04
  • 2
    The link attached in this answer is no longer useful. It is broken.
    – Cragfelt
    Mar 2, 2020 at 0:37
6

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .