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.

I'd like to make this Venn diagram using TikZ (or otherwise at your discretion) based on this post enter image description here

so far I have this, which looks even worse than a powerpoint attempt

\documentclass{article}
\usepackage{tikz}
\begin{document}


 \def\firstcircle{(0,0) circle (1.5cm)}
 \def\secondcircle{(0:2cm) circle (1.5cm)}

  \colorlet{circle edge}{blue!50}
  \colorlet{circle area}{blue!20}

  \tikzset{filled/.style={fill=circle area, draw=circle edge, thick}, outline/.style={draw=circle edge, thick}}

  \setlength{\parskip}{5mm}
\begin{figure}
\centering
% Set A and B

 \begin{tikzpicture}
  \begin{scope}
    \clip \secondcircle;
    \draw[filled, even odd rule] \firstcircle
                                 \secondcircle node {Env.=242};
  \end{scope}
     \draw[outline] \firstcircle node {Pat=9}
               \secondcircle;
   \node[anchor=south] at (current bounding box.north) {$B - A$};


   \draw (-2.5,-2.5) rectangle (4.5,2.5) node [text=black,above] {$\emptyset$};
   \end{tikzpicture}
   \end{figure}

enter image description here

I'd really appreciate a helping hand here as I do know from the posts that I've seen it should be quite straight forward. Regards,

EDIT:

Is it possible to put the labels of the sets as they are in the picture? The reason for this is that I have half a dozen Venn diagrams and I'm not sure colour coordination is the easiest to see.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

I would suggest some modifications: instead of crowding the diagram with information, you can use colors and a legend to display some of the information; in my example code I used a simple tabular to build the legend; also notice that no additional libraries were used

\documentclass{article}
\usepackage{array}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\def\radius{2cm}
\def\mycolorbox#1{\textcolor{#1}{\rule{2ex}{2ex}}}
\colorlet{colori}{blue!70}
\colorlet{colorii}{red!70}

\coordinate (ceni);
\coordinate[xshift=\radius] (cenii);

\draw[fill=colori,fill opacity=0.5] (ceni) circle (\radius);
\draw[fill=colorii,fill opacity=0.5] (cenii) circle (\radius);

\draw  ([xshift=-20pt,yshift=20pt]current bounding box.north west) 
  rectangle ([xshift=20pt,yshift=-20pt]current bounding box.south east);

\node[yshift=10pt] at (current bounding box.north) {Number of total episodes of case};

\node at ([xshift=\radius]current bounding box.east) 
{
\begin{tabular}{@{}lr@{\,=\,}c@{}}
& Total & 431 \\
\mycolorbox{colori!50} & Env. & 189 \\
\mycolorbox{colorii!50} & Pat. & 422 \\
\end{tabular}
};

\node[xshift=-.5\radius] at (ceni) {$9$};
\node[xshift=.5\radius] at (cenii) {$242$};
\node[xshift=.9\radius] at (ceni) {$180$};
\node[xshift=10pt,yshift=10pt] at (current bounding box.south west) {$\emptyset$};
\end{tikzpicture}

\end{document}

enter image description here

After a comment, here's another variant:

\documentclass{article}
\usepackage{array}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\def\radius{2cm}
\def\mycolorbox#1{\textcolor{#1}{\rule{2ex}{2ex}}}
\colorlet{colori}{blue!70}
\colorlet{colorii}{red!70}

% some coordinates for the center of the circles
\coordinate (ceni);
\coordinate[xshift=\radius] (cenii);

% the circles
\draw (ceni) circle (\radius);
\draw (cenii) circle (\radius);

% the rectangle
\draw  ([xshift=-25pt,yshift=25pt]current bounding box.north west) 
  rectangle ([xshift=25pt,yshift=-25pt]current bounding box.south east);

%the labels
\node[xshift=-.5\radius] at (ceni) {$9$};
\node[xshift=.5\radius] at (cenii) {$242$};
\node[xshift=.9\radius] at (ceni) {$180$};
\node[xshift=-30pt,yshift=\radius+10pt] at (ceni) {Pat.${}=180$};
\node[xshift=30pt,yshift=\radius+10pt] at (cenii) {Env.${}=422$};
\node[xshift=10pt,yshift=10pt] at (current bounding box.south west) {$\emptyset$};
\node[yshift=10pt] at (current bounding box.north) {Number of total episodes of case};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Looks super! How would you put the Env and Pat labels above the sets themselves? –  HCAI Mar 11 '13 at 12:25
    
@user1134241 please see my updated answer. –  Gonzalo Medina Mar 11 '13 at 13:33
    
Superb! ¡Muchísimas gracias! –  HCAI Mar 11 '13 at 14:59

enter image description here

\documentclass[pstricks,border=12pt]{standalone}
\begin{document}
\begin{pspicture}(8,6)
    \psframe(8,6)
    \psset{fillstyle=solid,linecolor=cyan}
    \pscircle[fillcolor=cyan!50](5.25,3){2}
    \pscircle[fillcolor=white](2.75,3){2}
    \pscircle[fillstyle=none](5.25,3){2}
    \uput{5pt}[45](0,0){$\emptyset$}
    \rput[r](2.75,3){Pat=9}
    \rput[l](5.25,3){Env=242}
    \rput[b](4,5){$B-A$}
    \uput{2pt}[45](8,6){$H$}
\end{pspicture}
\end{document}
share|improve this answer

I will go about it differently. Instead, I will draw the circle as nodes. Then, with the fit library, draw the borders. This way, you can write the labels relative to the named nodes.

The Code

%http://tex.stackexchange.com/questions/101839/tikz-venn-diagramm
\documentclass[tikz,border=5]{standalone}

\usetikzlibrary{positioning,fit,calc}

\begin{document}
\begin{tikzpicture}
\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}
\tikzset{
     filled/.style={fill=circle area, thick,inner sep=0pt}, 
     outline/.style={draw=circle edge, thick,inner sep=0pt}}

% The circles
\node (secondcircle) [circle,filled,text width=3cm] {};
\node (firstcircle) [circle,left=-1cm of secondcircle,outline,text width=3cm, fill=white] {};
\draw [outline] (secondcircle) circle (1.5cm);

% The labels
\node at ([xshift=-0.15cm]firstcircle) {$\mathrm{Pat=9}$};
\node at ([xshift=0.4cm]secondcircle) {$\mathrm{Env.=242}$};
\node at ($(firstcircle)!0.5!(secondcircle)$) {180};

% The rectangle and labels

\node (box) [fit=(firstcircle)(secondcircle), inner sep=1cm,draw,rounded corners] {};

\node at (box.north) [anchor=north] {$B-A$};
\node at (box.south west) [anchor=south west] {$\emptyset$};
\node at (box.south) [anchor=north] {432 total};
\node at (box.north east) [anchor=south west] {$H$};
\end{tikzpicture}
\end{document}

The Output

enter image description here

BTW, if you have a lot of Venn Diagrams of two or three sets to draw, I advise you to try the package venndiagram by Nicola Talbot.

Update

I was not so sure at first what should be placed where, but the code below seems to be a more complete attempt.

\documentclass[tikz,border=5,convert={density=150}]{standalone}

\usetikzlibrary{positioning,fit,calc}

\begin{document}
\begin{tikzpicture}
\colorlet{circle edge}{blue!50}
\colorlet{circle area}{blue!20}
\tikzset{filled/.style={fill=circle area, thick,inner sep=0pt}, outline/.style={draw=circle edge, thick,inner sep=0pt}}
% The circles
\node (secondcircle) [circle,filled,text width=3cm] {};
\node (firstcircle) [circle,left=-1cm of secondcircle,outline,text width=3cm, fill=white] {};
\draw [outline] (secondcircle) circle (1.5cm);
% The labels
\node at ([xshift=-0.8cm]firstcircle.north) [anchor=south] {$\mathrm{Pat=422}$};
\node at ([xshift=0.8cm]secondcircle.north) [anchor=south] {$\mathrm{Env.=189}$};
\node at ($(firstcircle)!0.5!(secondcircle)$) {180};
\node at (firstcircle) {9};
\node at (secondcircle) {242};
% The rectangle and labels

\node (box) [fit=(firstcircle)(secondcircle), inner sep=1cm,draw, ultra thick,rounded corners] {};

\node [below=12pt of box.north] {$B-A$};
\node at (box.south west) [anchor=south west] {$\emptyset$};
\node at (box.south) [anchor=north] {431 total};
\node at (box.north east) [anchor=south west] {$H$};

\end{tikzpicture}
\end{document}

enter image description here

The coordinates with respect to the named nodes are called anchors. center is the only anchor present in all shapes. So if you name an anchor as A, then you can position another object, say, at A.center. Some more anchors are north, south, east, west, etc. You can see more of these in the pgfmanual. Just type and enter texdoc pgf in your terminal. In version 2.10, you can see some explanations starting from Section 16.5 on Positioning Nodes.

share|improve this answer
    
Very nice! With this method, to be able to put the Pat and Env labels on the tops of the circles (sets), do you have to know the coordinates of said points? –  HCAI Mar 11 '13 at 12:24
1  
@user1134241 See my edit. –  hpesoj626 Mar 12 '13 at 2:18

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.