# Draw a rectangle to given Venn Diagram

Question: How can i draw a rectangle box which covers this figure?

Codes:

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

% Definition of circles
\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}}
% Set A or B
\begin{tikzpicture}
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$A \cup B$};
\end{tikzpicture}

\end{document}


(from www.texample.net)

One possibility is to use the bounding box.

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

% Definition of circles
\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}}
% Set A or B
\begin{tikzpicture}
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$A \cup B$};
\draw (current bounding box.north west) rectangle
(current bounding box.south east);
\end{tikzpicture}

\end{document}


If you need a somewhat larger box (this need not be the most elegant solution, but it works):

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

% Definition of circles
\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}}
% Set A or B
\begin{tikzpicture}
\draw[filled] \firstcircle node {$A$}
\secondcircle node {$B$};
\node[anchor=south] at (current bounding box.north) {$A \cup B$};
\draw ($(current bounding box.north west)+(-1,1)$)
node [below right] {$U$}
rectangle ($(current bounding box.south east)+(1,-1)$);
\end{tikzpicture}

\end{document}

• how may i increase the size of rectangle? – Maths4Sandy Apr 28 '17 at 5:10
• @snehal I added a second version with a distance of 1 cm in each direction. – Jürgen Apr 28 '17 at 7:19
• Just to let you know, seems the OP also wanted a label in the rectangle (see comments on my answer). – Torbjørn T. Apr 28 '17 at 8:04
• @Torbjørn T. Thanks for the hint, I added it to the second example. – Jürgen Apr 28 '17 at 8:08

You can use \usetikzlibrary{backgrounds} and just simply add the option

[show background rectangle]


right after \begin{tikzpicture}

An alternative approach using the fit library.

Note, as egreg mentions in a comment, that in this particular case there is little point in defining macros for \firstcircle and \secondcircle, because you only use them once. If your actual document has more circles, that's another thing. Note also, that using \newcommand is in general preferred over \def, as it will not overwrite existing macros, but throw an error if the macro is already used for something.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\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}
}
\begin{document}
% Set A or B
\begin{tikzpicture}
\draw[filled] (0,0) circle[radius=1.5cm] node {$A$}
(0:2cm) circle[radius=1.5cm] node {$B$};
\node[anchor=south] at (current bounding box.north) {$A \cup B$};

% draw frame
\node [draw,fit=(current bounding box),inner sep=3mm] (frame) {}; % modify inner sep to adjust gap from circles to frame

\node [below right] at (frame.north west) {$U$};
\end{tikzpicture}
\end{document}

• Even if they're in the example code in texexample,, I don't think that \firstcircle and \secondcircle are a good idea. – egreg Apr 28 '17 at 7:38
• @egreg In what sense? For this example it's not much point to use macros, sure, but the original example had more diagrams, and in some cases the same circle was used more than once in the same diagram (for clipping and drawing). – Torbjørn T. Apr 28 '17 at 7:44
• One small question i wanna lable that rectagnle as U(universal set). – Maths4Sandy Apr 28 '17 at 7:46
• Exactly: here they serve no purpose. And using \def is another bad idea (which you fixed). – egreg Apr 28 '17 at 7:46
• @egreg I'll say something about it when I edit. – Torbjørn T. Apr 28 '17 at 7:49