1

Problem:

Two minor issues which I have not been able to solve.

  1. How do I fill the intersection for the second figure (on the right)?
  2. How do I create "padding" or more "air" around the circles in each box?

Additional issue:

Why is the rectangle for figure 2 higher than figure 1? The code is identical for the two boxes.

Minimal Working Example (MWE):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning,calc}
\usepackage{float}
\usepackage{showframe}
\begin{document}

\begin{figure}[H]
    \centering
    \begin{minipage}[t]{0.45\textwidth}
        \begin{tikzpicture}[show background rectangle]
            % Sets colors for cirlces
            \colorlet{circle edge}{black}
            \colorlet{circle area}{gray!50}
            % Sets style for filled and outline
            \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,filled,left=-1cm of secondcircle,outline,text width=3cm] {};
            \draw [outline] (secondcircle) circle (1.5cm);
            % The labels
            \node at ([xshift=-0.15cm]firstcircle) {$A$};
            \node at ([xshift=0.4cm]secondcircle) {$B$};
        \end{tikzpicture}
        \caption{$\mathbf{A} \cup \mathbf{B}$}
        \label{fig:venncup}
    \end{minipage}
    \hspace{0.02\linewidth}
    \begin{minipage}[t]{0.45\textwidth}
        \begin{tikzpicture}[show background rectangle]
            % Sets colors for cirlces
            \colorlet{circle edge}{black}
            \colorlet{circle area}{gray!50}
            % Sets style for filled and outline
            \tikzset{
                 filled/.style={fill=circle area, thick,inner sep=0pt}, 
                 outline/.style={draw=circle edge, thick,inner sep=0pt}}
            % The circles
            \node (secondcircle) [circle,fill=white,text width=3cm] {};
            \node (firstcircle) [circle,left=-1cm of secondcircle,outline,text width=3cm] {};
            \draw [outline] (secondcircle) circle (1.5cm);
            % The labels
            \node at ([xshift=-0.15cm]firstcircle) {$A$};
            \node at ([xshift=0.4cm]secondcircle) {$B$};
        \end{tikzpicture}
        \caption{$\mathbf{A} \cap \mathbf{B}$}
        \label{fig:venncap}
    \end{minipage}
\end{figure}

\end{document}

Current output:

enter image description here

Desired output:

Fill the intersection on the second figure (one to the right) and to "shrink" the circles in order to add space around them.

2 Answers 2

2

Questions of this type have been asked before, but there is something that has changed only recently: the newest version of TikZ has the use path key, which allows us to recycle paths. (The save path key was there for a long time, but not mentioned.) This allows you to redraw the circle paths of the nodes without dialing the radius etc. by hand. So in the left diagram you can save the path of the first circle with

\node[save path=\pathB] (secondcircle) [my circ] {};

and recycle it with

\draw[outline,use path=\pathB];

In the second diagram, you can save both paths with

\node[save path=\pathB] (secondcircle) [my circ] {};
\node[save path=\pathA] (firstcircle) [my circ,left=-1cm of secondcircle] {};

and then just use these paths for clip & fill,

\clip[use path=\pathA];
\fill[blue,use path=\pathB];

For your convenience I also added a style bb that extends and draws the bounding box, see the following for its usage. (One cannot overdo it here because there is not too much space left, but if you change the external parameters you can give the pics more breathing space.)

I also have defined (locally) a style my circ that fixes the properties of all circles. The size is controlled by the minimum size key. In the left picture, the definition is appended by filled.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usepackage{float}
\usepackage{showframe}
\begin{document}
\begin{figure}[H]
\tikzset{bb/.style={insert path={([xshift=-#1,yshift=-#1]current bounding box.south west) rectangle 
             ([xshift=#1,yshift=#1]current bounding box.north east) }},
    filled/.style={fill=circle area, thick,inner sep=0pt}, 
                 outline/.style={draw=circle edge, thick,inner sep=0pt},
                 my circ/.style={minimum size=2.5cm,circle,outline}}
    \centering
    \begin{minipage}[t]{0.45\textwidth}
        \begin{tikzpicture}[my circ/.append style={filled}]
            % Sets colors for cirlces
            \colorlet{circle edge}{black}
            \colorlet{circle area}{gray!50}
            % Sets style for filled and outline
            % The circles
            \node[save path=\pathB] (secondcircle) [my circ] {};
            \node (firstcircle) [my circ,left=-1cm of secondcircle] {};
            \draw[outline,use path=\pathB];
            % The labels
            \node at ([xshift=-0.15cm]firstcircle) {$A$};
            \node at ([xshift=0.4cm]secondcircle) {$B$};
            \draw[bb=1.3em];
        \end{tikzpicture}
        \caption{$\mathbf{A} \cup \mathbf{B}$}
        \label{fig:venncup}
    \end{minipage}
    \hspace{0.02\linewidth}
    \begin{minipage}[t]{0.45\textwidth}
        \begin{tikzpicture}
            % Sets colors for cirlces
            \colorlet{circle edge}{black}
            \colorlet{circle area}{gray!50}
            % Sets style for filled and outline
            % The circles
            \node[save path=\pathB] (secondcircle) [my circ] {};
            \node[save path=\pathA] (firstcircle) [my circ,left=-1cm of secondcircle] {};
            % The labels
            \node at ([xshift=-0.15cm]firstcircle) {$A$};
            \node at ([xshift=0.4cm]secondcircle) {$B$};
            \begin{scope}[on background layer]
            \clip[use path=\pathA];
            \fill[blue,use path=\pathB];
            \end{scope}
            \draw[bb=1.3em];
        \end{tikzpicture}
        \caption{$\mathbf{A} \cap \mathbf{B}$}
        \label{fig:venncap}
    \end{minipage}
\end{figure}
\end{document}

enter image description here

1
  • 1
    @kexxcream I updated (and simplified) the answer. The size is controlled by the minimum size key. I stored all universal parameters in styles that are local in this figure, but work for both tikzpictures.
    – user121799
    Jul 14, 2019 at 12:42
1

You can have a much simpler code using the venndiagram package (based on TikZ):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning,calc}
\usepackage{venndiagram}
\usepackage{float, caption}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}

\begin{document}

{\captionsetup[figure]{skip =-1.5ex}
\begin{figure}[H]
    \centering
    \begin{minipage}[t]{0.45\textwidth}\begin{venndiagram2sets}[radius=1.3cm]
\fillA \fillB
\end{venndiagram2sets}
        \caption{$\mathbf{A} \cup \mathbf{B}$}
        \label{fig:venncup}
    \end{minipage}
    \hspace{0.02\linewidth}
    \begin{minipage}[t]{0.45\textwidth}\begin{venndiagram2sets}[radius=1.3cm]
\fillACapB
\end{venndiagram2sets}
        \caption{$\mathbf{A} \cap \mathbf{B}$}
        \label{fig:venncap}
    \end{minipage}
\end{figure}}

\end{document} 

enter image description here

2
  • This seems more simple - is there a way to get the A / B's in the center of the circles?
    – kexxcream
    Jul 14, 2019 at 12:53
  • I'll try to have a look at the code – there's no key to place the labels, so I think it should be patched.
    – Bernard
    Jul 14, 2019 at 13:41

You must log in to answer this question.

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