Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
3  
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
    
Thank you @Seamus! –  daroczig Jan 25 '11 at 21:19
1  
Although a lot of time has passed, someone who wander here might also find this link helpful. –  quapka Apr 19 at 15:42

8 Answers 8

up vote 31 down vote accepted

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 (-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)
      (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.
share|improve this answer
    
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
    
thank you for your detailed example, I have learned a lot! –  daroczig Jan 25 '11 at 21:19

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

share|improve this answer
    
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

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.

share|improve this answer
    
@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
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 '11 at 19:07
1  
@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

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

share|improve this answer
    
there is a problem with the linewidth in the clipped area, you have to choose another order, first clipping, then drawing –  Herbert Jan 27 '11 at 16:58
    
Yes Herbert, you are right ! I need to (re) draw the arcs or I need to use a scope. –  Alain Matthes Jan 27 '11 at 17:04
    
and by the way, the optional argument usenames for xcolor is obsolete for years ... –  Herbert 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
    
thank you for this alternate solution which looks promising and simple also! –  daroczig Jan 30 '11 at 13:10

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.

share|improve this answer
    
Thanks for this simple solution (+1), I'll have to check it out. –  daroczig Aug 28 '11 at 15:29

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{(45: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}

Venn diagram

share|improve this answer
    
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. –  Trevor Alexander Jan 15 at 2:32

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}
share|improve this answer
1  
This looks pretty cool :) Thanks a lot! –  daroczig Jul 24 '12 at 12:40

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.

share|improve this answer
    
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 at 18:21
    
This is indeed useful, thank you @ur-ben-ari-tishler –  daroczig Dec 9 at 19:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.