[Tex/LaTex] How to draw Venn diagrams (especially: complements) in LaTeX

circlesgnuplotlogictikz-pgfvenn-diagrams

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!

Best Answer

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.