How to draw Venn diagrams (especially: complements) in LaTeX
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.
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}
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}