8

I need to draw a Venn diagram for$ X - (Y \cap Z), but I can only figure out how to get $X - (Y \cup) Z)? Can anyone help?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\begin{document}

\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={(6cm,0cm)}]
        \begin{scope}[even odd rule]% first circle without the second
            \clip \thirdcircle (-3, -3) rectangle (3,3);
            \clip \secondcircle (-3, -3) rectangle (3,3);
        \fill[yellow] \firstcircle;


        \end{scope}
        \draw \firstcircle node {$X$};
        \draw \secondcircle node {$Y$};
        \draw \thirdcircle node {$Z$};
    \end{scope}
  \end{tikzpicture}
0

5 Answers 5

7

Here is a different approach. We fill the second and third circles with white, then fill the first circle with yellow. Finally, we color the intersection of the three circles white.

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes,backgrounds}

\begin{document}
\def\firstcircle{(0,0) circle (1.5 cm)}
\def\secondcircle{(0: 2 cm) circle (1.5 cm)}
\def\thirdcircle{(60:2 cm) circle (1.5 cm)}

\begin{tikzpicture}   

%Define the circles for the Venn diagram for three sets.

\draw[fill=white] \secondcircle;
\draw[fill=white] \thirdcircle;
\draw[fill=yellow] \firstcircle;

% Label the sets.

\node[left] at (0, 0) {\(X\)};
\node[above] at (60: 2 cm) {\(Y\)};
\node[right] at (0: 2 cm) {\(Z\)};    

%Color the intersection of all three circles white.

\begin{scope}
  \clip \firstcircle;
  \clip \secondcircle;
  \draw[fill=white] \thirdcircle;
\end{scope}

%Draw the circles.

\draw \firstcircle;
\draw \secondcircle;
\draw \thirdcircle;

\end{tikzpicture}

which produces the diagram

Venn_diagram_for_X - (Y intersection Z)

6

Just for fun!

\documentclass[border=10pt,pstricks]{standalone}
\begin{document}
\begin{pspicture}(-3,-4)(6,6)
\psclip[linestyle=none]{%
        \pscircle[fillstyle=solid,fillcolor=pink](0,0){3}}
    \psclip{%
        \pscircle[fillstyle=solid,fillcolor=pink](3;60){3}}
        \pscircle[fillstyle=solid,fillcolor=white](3,0){3}
    \endpsclip
\endpsclip
\pscircle(0,0){3} \rput(-1.5,-0.5){$X \setminus (Y \cap Z)$}
\pscircle(3,0){3} \rput(1.5,3.5){$Y$}
\pscircle(3;60){3} \rput(3.5,-0.5){$Z$}
\end{pspicture}
\end{document}

enter image description here

1
  • Very nice and I have give you a positive score. Can you edit, please, your code to have {Y} in mathmode (i.e. {$Y$})? Thank you.
    – Sebastiano
    Sep 14, 2019 at 15:05
4

There is a package that specializes on Venn diagrams: venndiagram. One needs to "hack" it a bit since the intersection you are after seems not be in the predefined set, but this is what \setpostvennhook is for. This hack defines the interiors and the contours of the circles. They have the names A, B and C, but their labels can be set to X, Y and Z, say.

\documentclass[border=10pt]{standalone}
\usepackage{venndiagram}
\makeatletter% https://tex.stackexchange.com/a/499947
\tikzset{interior/.style={insert path={
 (\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname) circle[radius=\@venn@radius-\pgflinewidth/2]}},
 contour/.style={insert path={
 (\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname) circle[radius=\@venn@radius]}}}
\makeatother
\begin{document}
\begin{venndiagram3sets}[labelA=$X$,labelB=$Y$,labelC=$Z$]
\fillA
\setpostvennhook{
\clip[interior=B];
\fill[white,interior=C];
\draw[contour=A];}
\end{venndiagram3sets}
\end{document}

enter image description here

ADDENDUM: Since this is a repeating theme, I am trying to collect some standard tricks here, and also add some stuff to the venndiagram repertoire.

  1. The reverse clip trick allows one to clip on everything outside a given path.
  2. The even odd clip trick allows one to apply the even odd rule to clips.
  3. I added the styles interior of, which produce a path that contains the interior of either A, B, or C, contour of which allows one to redraw the contour of either A, B, or C, and common of, which allows one to retrieve the intersection of two of the sets. The latter requires the calc library.

Using these tricks, one can avoid overpainting anything in white. We first clip on everything outside the intersection of B and C,

\clip[common of=B and C,reverse clip];

and then fill the interior of A,

\fill[\VennShade,interior of=A];

Complete MWE. (Notice that some care is taken not to add any hard coded distances, the idea being that you produce several diagrams and have common settings.)

\documentclass[border=10pt]{standalone}
\usepackage{venndiagram}
\usetikzlibrary{calc,backgrounds}
\makeatletter% https://tex.stackexchange.com/a/499947
\tikzset{interior of/.style={insert path={
 (\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname) circle[radius=\@venn@radius-\pgflinewidth/2]}},
 contour of/.style={insert path={
 (\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname)
 circle[radius=\@venn@radius]}},
 midpoint of/.style={insert path={
 (\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname)
 }},
 common of/.style args={#1 and #2}{insert path={
 let \p1=($(\csname @venn@#2x\endcsname,\csname @venn@#2y\endcsname)-(\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname)$),
  \n1={veclen(\y1,\x1)/2},\n2={sqrt(\@venn@radius*\@venn@radius-\n1*\n1)},
  \n3={atan2(\y1,\x1)},\n4={atan2(\n2,\n1)} in 
 ($(\csname @venn@#1x\endcsname,\csname @venn@#1y\endcsname)+(\n3+\n4:\@venn@radius)$)
 arc(\n3+\n4:\n3-\n4:\@venn@radius) arc(\n3+180+\n4:\n3+180-\n4:\@venn@radius) 
 -- cycle
 %\pgfextra{\typeout{\n2,\n3,\n4,\@venn@radius}}
 }},
 reverse clip/.style={insert path={{% https://tex.stackexchange.com/a/127045
  [overlay] (0,0)  -|
  (\@venn@w,\@venn@h) -| cycle
    }}},
  even odd clip/.code={% https://tex.stackexchange.com/a/76216
  \pgfseteorule}}
\def\VennShade{\@venn@shade}
\makeatother
\begin{document}
\begin{venndiagram3sets}[labelA=$X$,labelB=$Y$,labelC=$Z$]
\setpostvennhook{
\begin{scope}[on background layer]
 \clip[common of=B and C,reverse clip];
 \fill[\VennShade,interior of=A];
\end{scope}}
\end{venndiagram3sets}
\end{document}

enter image description here

2

Another solution with venndiagram:

\documentclass[svgnames, tikz, border=6pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern, amssymb, nccmath}

\usepackage{venndiagram}

\begin{document}

   \begin{venndiagram3sets}[radius=1.5cm, overlap=1.2cm]%
        \setkeys{venn}{shade=Lavender, labelA=, labelB=}
        \fillOnlyA\fillACapBNotC\fillACapCNotB
        \setkeys{venn}{shade=WhiteSmoke!80!Thistle, labelA=, labelB=}
        \fillBCapC
        \setpostvennhook
        {%
        \draw (labelA) ++(180:1.1cm) node{$A$};
         \draw (labelB) ++(0:1.1cm) node{$B$};
        \draw (labelABC) ++(-20:0.6cm) node[text=MediumVioletRed]{$\medmath{ B ∩ C}$};
        \draw (labelA) ++(-100:1.2cm) node[text=NavyBlue]{$\medmath{A\smallsetminus(B\cap C)}$};
        }%
        \end{venndiagram3sets}

\end{document} 

enter image description here

2
  • All the distances are hard coded, so if the user ever decides to change the settings of the Venn diagram, they have to readjust them. Please note also that the "proper" syntax for node{$\color{MediumVioletRed}\medmath{ B ∩ C}$} is node[text=MediumVioletRed]{$\medmath{ B ∩ C}$}.
    – user194703
    Sep 14, 2019 at 18:00
  • I thought it didn't work in mathmode… I'll fix it readily.
    – Bernard
    Sep 14, 2019 at 18:02
1

This is usual clip of TikZ as Taussig did above. Maybe I am a bit difficult when considering plainess and flexibility of the TikZ code.

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\r{1.5} \def\d{1}
\def\firstC{(90:\d) circle(\r)}
\def\secondC{(-30:\d) circle(\r)}
\def\thirdC{(210:\d) circle(\r)}

\fill[magenta!30] \firstC;   
\begin{scope}
\clip \firstC;
\clip \secondC;
\fill[white] \thirdC;
\end{scope}
\draw \firstC \secondC \thirdC;

\path
(90:1.2*\d) node[magenta,scale=.6]{$A\setminus (B\cap C)$} 
(90:2*\d)  node{$A$}
(210:2*\d) node{$B$}
(-30:2*\d) node{$C$};
\end{tikzpicture}
\end{document}

Here are translation to Asymptote code (exactly the same picture).

unitsize(1cm);
real r=1.5,d=1;
pair A=d*dir(90),B=d*dir(210),C=d*dir(-30);
path firstC=circle(A,r);
path secondC=circle(B,r);
path thirdC=circle(C,r);

fill(firstC,.3magenta+.7white);   
picture BcapC; 
fill(BcapC,thirdC,white);   
clip(BcapC,firstC);
clip(BcapC,secondC);
add(BcapC);

draw(firstC^^secondC^^thirdC);

label("$A$",2A);
label("$B$",2B);
label("$C$",2C);
label((scale(.6))*"$A\setminus (B\cap C)$",1.2A,magenta);
add(bbox(5mm),Fill(white));

You must log in to answer this question.

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