# How can I draw a rectangle around venn Diagrams?

I have tried some of the solutions to similar questions, but none of them included minipages, which seems to be the problem (Im fairly new to LaTeX)

So, here's the coding:

% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}

\colorlet{circle edge}{black!50}
\colorlet{circle area}{grey!20}

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

\setlength{\parskip}{5mm}

% Set A or B
\begin{tikzpicture}
\begin{minipage}[t][2cm][t]{.48\textwidth}
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$M$};
\end{minipage}
\end{tikzpicture}
% Set A and B
\begin{tikzpicture}
\begin{minipage}[t][2cm][t]{.48\textwidth}
\begin{scope}
\clip \firstcircle;
\fill[filled] \secondcircle;
\end{scope}
\draw[outline] \firstcircle node {$A$};
\draw[outline] \secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$M$};
\node[anchor=west] at (current bounding box.east) {$A \cap B$}
\end{minipage}
\end{tikzpicture} Also, if you know how to put the "B" in the center of the circle on the right Id appreciate your help! Thank you.

• You should not use minipage inside TikZ pictures – user156344 May 7 at 2:17

Welcome to TeX.SE! It is good that you included your code. In terms of drawing a rectangle around the venn diagram, you could import the fit library from Tikz using \usetikzlibrary{fit}. I have completed an example to illustrate. Also, I have changed the code a little bit. You now have one tikzpicture (with no minipage). The first venn diagram has its own scope and the second has its own as well. I have changed the placement of M so that it would be easier to read. There are two types overall in terms of appearance, you choose the one you like. \documentclass[margin=1cm, tikz]{standalone}
\usepackage{tikz,xcolor,color}
\usetikzlibrary{fit}
\begin{document}

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

\setlength{\parskip}{5mm}

\begin{tikzpicture}
% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}
%
\colorlet{circle edge}{black!50}
\colorlet{circle area}{gray!20}
%
\begin{scope}[local bounding box = orScope]
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (orScope.north){$M$};
\node[anchor=north] at (orScope.south) {$A \cup B$};
\end{scope}
\node[fit=(orScope), draw] {};% The frame around the scope
%
\begin{scope}[xshift = 7cm,local bounding box = andScope]
\begin{scope}
\clip \firstcircle;
\fill[filled] \secondcircle;
\end{scope}
\draw[outline] \firstcircle node {$A$};
\draw[outline] \secondcircle node {$B$};
\node[anchor=south] at (andScope.north) {$M$};
\node[anchor=west] at (andScope.south east) {$A \cap B$};
\end{scope}
\node[fit=(andScope), draw] {};
\end{tikzpicture}

\end{document}

• Thank you very much! I have managed to complete this part of my project thanks to your help! – Matheus Chebli May 7 at 21:56

There are dedicated packages for that. Assuming you do not want to use any of those, I'd like to advertize some styles that help you drawing these diagrams. These are not inside, inside to specify the sets and frame for drawing a frame around these pictures. Your first picture is easy, you only need to fill the circles. The second one can be achieved with

\pgfkeys{inside/.list={\pathA,\pathB},shade=gray}


and the frames can be done with

\draw[frame=5pt];


where 5pt indicates the margin.

Here are code and result.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
% based on https://tex.stackexchange.com/a/12033/121799
\tikzset{reverseclip/.style={insert path={(current bounding box.south west)rectangle
(current bounding box.north east)} },
use path/.code={\pgfsetpath#1},%learned from Kpym
frame/.style={insert path={
([xshift=-#1,yshift=-#1]current bounding box.south west) rectangle
([xshift=#1,yshift=#1]current bounding box.north east)}}
}
\begin{document}
\pgfkeys{not inside/.code={\clip[use path=#1,reverseclip];},
inside/.code={\clip[use path=#1];},
(current bounding box.north east);}
\begin{tikzpicture}
\draw[thick,fill=gray] (-1,0) node{$A$} circle [radius=1.5cm]
(1,0) node{$B$} circle[radius=1.5cm] (0,1.5) node[above]{$M$}
(0,-1.5) node[below]{$A\cup B$};
\draw[frame=5pt];
\end{tikzpicture}~%
\begin{tikzpicture}
\draw[thick,save path=\pathA] (-1,0) node{$A$} circle [radius=1.5cm];
\draw[thick,save path=\pathB] (1,0) node{$B$} circle[radius=1.5cm];
\path (0,1.5) node[above]{$M$}
(0,-1.5) node[below]{$A\cap B$};
\begin{scope}[on background layer]
\end{scope}
\draw[frame=5pt];
\end{tikzpicture}
\end{document} • Thank you so much! I now have a bigger understanding of those commands and maybe will be able to draw some myself :D – Matheus Chebli May 7 at 21:56

Another solution:

If you really wants to use minipage, you have to put the environment outside tikzpicture. Literally minipage does nothing for you here, but it does many things against you.

However, for figures side-by-side, a subfigure environment (from subcaption) is better in this case.

And, to draw a rectangle, you can use the special node current bounding box, with some shifts if needed.

Btw your diagrams are too wide for the normal margin.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tikz}
% Definition of circles
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(0:2cm) circle (1.5cm)}

\colorlet{circle edge}{black!50}
\colorlet{circle area}{gray!20}

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

\setlength{\parskip}{5mm}
\begin{document}
\noindent\begin{minipage}[t]{.5\textwidth}
\centering%
\begin{tikzpicture}
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$M$};
\end{tikzpicture}
\end{minipage}%
\begin{minipage}[t]{.5\textwidth}
\centering%
\begin{tikzpicture}
\begin{scope}
\clip \firstcircle;
\fill[filled] \secondcircle;
\end{scope}
\draw[outline] \firstcircle node {$A$};
\draw[outline] \secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$M$};
\node[anchor=west] at (current bounding box.east) {$A \cap B$};
\draw ([shift={(-1ex,1ex)}]current bounding box.north west) rectangle
([shift={(1ex,-1ex)}]current bounding box.south east);
\end{tikzpicture}
\end{minipage}
\end{document} 