7

How can I add a centered caption to the venn-diagram below the rectangle?

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw (-2,-1.5) rectangle (3.5,1.5) node[below left]{$U$};
\fill[gray] (0,0) circle (1cm);
\fill[gray] (1.5,0) circle (1cm);
\draw (0,0) circle (1cm) node {$A$};
\draw (1.5,0) circle (1cm) node {$B$};
\end{tikzpicture}
\end{center}
\end{document}

1 Answer 1

9

with symmetry in circle placing the drawing of all elements is simpler:

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{center}
\begin{tikzpicture}
\fill[gray!50]  (-0.75,0) circle (1cm);
\fill[gray!50]  ( 0.75,0) circle (1cm);
\draw           (-0.75,0) circle (1cm) node {$A$};
\draw           ( 0.75,0) circle (1cm) node {$B$};
\draw (-2.5,-1.5) rectangle +(5,3) node[below left]{$U$};
    \node[below] at (0,-1.5) {caption};
\end{tikzpicture}
    \end{center}
\end{document}

enter image description here

however, I doubt that placing of caption on this way is the best way. To my opinion is better to include image in figure environment:

\documentclass{article}
\usepackage{tikz}
\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}
\fill[gray!50]  (-0.75,0) circle (1cm);
\fill[gray!50]  ( 0.75,0) circle (1cm);
\draw           (-0.75,0) circle (1cm) node {$A$};
\draw           ( 0.75,0) circle (1cm) node {$B$};
\draw (-2.5,-1.5) rectangle +(5,3) node[below left]{$U$};
\end{tikzpicture}
\caption{Caption}
    \end{figure}
\end{document}

enter image description here

Addendum: More Venn's diagrams in one row you can obtain on different ways. For example as \subfloats{...}:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{subfig}

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

\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},
      F/.style = {draw, inner sep=7mm, fit=(current bounding box),
                  node contents={}}
            }

    \begin{figure}
    \centering
    \subfloat[presentation of sets union by Venn diagram
              \label{fig:ven-1a}]{      
\begin{tikzpicture}
    \draw[filled] \firstcircle node {$A$}
                  \secondcircle node {$B$};
    \node (a) [F];                 
    \node[below left] at (a.north east) {$A \cup B$};
\end{tikzpicture}
                                    }% end of subfloat
\hfil
    \subfloat[presentation of sets intersection by Venn diagram
              \label{fig:ven-1b}]{
\begin{tikzpicture}
    \begin{scope}
        \clip \firstcircle;
        \fill[filled] \secondcircle;
    \end{scope}
    \draw[outline] \firstcircle node {$A$};
    \draw[outline] \secondcircle node {$B$};
    \node (a) [F];
    \node[below left] at (a.north east) {$A \cap B$};
\end{tikzpicture}
                                    }% end of subfloat
\caption{Operations on sets presented by Venn diagrams}                   
    \end{figure}
\end{document}

enter image description here

or in minipages:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{subfig}

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

\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},
      F/.style = {draw, inner sep=7mm, fit=(current bounding box),
                  node contents={}}
            }

    \begin{figure}
    \centering
    \begin{minipage}[t]{0.45\linewidth} 
    \centering   
\begin{tikzpicture}
    \draw[filled] \firstcircle node {$A$}
                  \secondcircle node {$B$};
    \node (a) [F];                 
    \node[below left] at (a.north east) {$A \cup B$};
\end{tikzpicture}
    \caption{Presentation of sets union by Venn diagram}
\label{fig:ven-1a}
    \end{minipage}
\hfil
    \begin{minipage}[t]{0.45\linewidth}
    \centering
\begin{tikzpicture}
    \begin{scope}
        \clip \firstcircle;
        \fill[filled] \secondcircle;
    \end{scope}
    \draw[outline] \firstcircle node {$A$};
    \draw[outline] \secondcircle node {$B$};
    \node (a) [F];
    \node[below left] at (a.north east) {$A \cap B$};
\end{tikzpicture}
    \caption{Presentation of sets union by Venn diagram}
\label{fig:ven-1a}
    \end{minipage}% end of subfloat
    \end{figure}
\end{document}

enter image description here

or in sequence of tikzpictures on a more "fancy" way:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, shadows}
\usepackage{subfig}

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

\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},
      F/.style = {draw, fill=white, inner sep=7mm, fit=(current bounding box),
                  drop shadow,  
                  node contents={}}
            }

    \begin{figure}
    \centering
\begin{tikzpicture}
    \draw[filled] \firstcircle node {$A$}
                  \secondcircle node {$B$};
    \scoped[on background layer]\node (a) [F];                 
    \node[below left] at (a.north east) {$A \cup B$};
\end{tikzpicture}
\hfil
    \centering
\begin{tikzpicture}
    \begin{scope}
        \clip \firstcircle;
        \fill[filled] \secondcircle;
    \end{scope}
    \draw[outline] \firstcircle node {$A$};
    \draw[outline] \secondcircle node {$B$};
    \scoped[on background layer]\node (a) [F];
    \node[below left] at (a.north east) {$A \cap B$};
\end{tikzpicture}

    \caption{Operations on sets presented by Venn diagram}
\label{fig:ven-1a}
    \end{figure}
\end{document}

enter image description here

Codes for Venn diagrams are stolen and slightly adopted from texample.net. There you can find diagrams for other set operations.

2
  • What would you recommend with multiple figures next to each other on the same line? Also, when I add the caption $A\setminusB$ to one it throws it out of vertical alignment. Any way to force alignment? Commented May 29, 2017 at 15:56
  • @SethKillian, see addendum to my answer. It contain three from among possible ways to put more Venn diagrams in one line.
    – Zarko
    Commented May 29, 2017 at 19:33

You must log in to answer this question.

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