# How to clip a rectangle with multiple shapes (circle)?

How to clip a rectangle with multiple shapes (circle)? I need to create a complex venn diagram and need to clip the outer rectangle in a shape that is combined with multiple circle. Not sure how to do it. I could do it with one circle though,

\documentclass{minimal}

\usepackage{graphicx, tikz}

\begin{document}

\def\Space{(-5,-5) rectangle (5,5)}
\def\Item{(-1.9,0.62) circle (2.5cm)}

\begin{tikzpicture}
\begin{scope}[shift={(3cm,-5cm)}, fill opacity=1.0]
\fill [orange, even odd rule] \Space \Item;
\end{scope}
\end{tikzpicture}

\end{document}


• Hi Pixelord, welcome to the site! Could you edit your question to turn the code snippet into a complete minimal example document (starting from \documentclass)? At the moment, the code sample doesn't really help (what's \Space? What's \Item? What does the fill pattern have to do with the question?) – Jake Oct 15 '13 at 9:28

You can use Paul Gaborit's invclip style for this, using one clipping path per circle:

\documentclass{article}
\usepackage{tikz}

\begin{document}
\tikzset{
invclip/.style={
clip,
insert path={{[reset cm](-16383.99999pt,-16383.99999pt) rectangle (16383.99999pt,16383.99999pt)}}
}
}

\begin{tikzpicture}

\begin{scope}
\begin{pgfinterruptboundingbox}
\end{pgfinterruptboundingbox}
\fill [orange] (-1.5,-1.5) rectangle (2.5,1.5);
\end{scope}
\end{tikzpicture}

\end{document}

• Hi Jake,I am getting following error while trying your code. pgfkeys: I do not know the key '/tikz/insert path' and I am going to ignore it. Perhaps you misspelled it. \path [invclip] ( – Pixelord Oct 15 '13 at 15:18
• @Pixelord: That sounds like you're using a really outdated version of TikZ. The current one is (and has been for a couple of years) version 2.1 – Jake Oct 15 '13 at 15:20
• Yes I guessed so. I am using TexStudio 2.6.2 and not sure how to update the tikZ it is using. – Pixelord Oct 15 '13 at 15:25
• @Pixelord: That depends on which distribution you're using (MikTeX?). TexStudio is an editor, not a distribution. Take a look at tex.stackexchange.com/questions/2044/… and the questions linked from there for information on how to update. – Jake Oct 15 '13 at 15:27

This is another alternative where (\x,\y) is changeable and different radius is possible by introducing a new variable \z to the circle radius.

Code

\documentclass[]{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=2]
\fill[orange] (-1.2,-1.2) rectangle (1.4,1.4);
\begin{scope}
\foreach \x/\y in {0/0.5, -0.3/0, 0.3/0}
{
\filldraw[yellow,opacity=0.5] (\x,\y) circle (0.5 cm);
}
\end{scope}
\node at (0,0.2) {A};
\end{tikzpicture}

• Painting white is often not desirable if you have something lying under the rectangle which is probably why you are clipping anyway. – percusse Oct 15 '13 at 10:05
• @percusse -- Thanks for the comment. Observed and improved. – Jesse Oct 15 '13 at 10:21

I'm not sure about your question, but if it means "how to draw a background rectangle behind several circles?" here you have another option. In this case you don't need to fix a previous size for the rectangle.

\documentclass[tikz,border=1mm]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{tikzpicture}[execute at end picture=%
{
\begin{pgfonlayer}{background}
\path[fill=orange]
([shift={(-1mm,-1mm)}]current bounding box.south west) rectangle
([shift={(1mm,1mm)}]current bounding box.north east);
\end{pgfonlayer}
}]

\end{tikzpicture}

\end{document}


Thank you for helping me find a bug in my package. Once the fix gets distributed as V3.21, the MWE could be as simple as

\documentclass{article}
\usepackage{stackengine}
\usepackage{xcolor}
\usepackage{graphicx}

\begin{document}
{\color{white}%
\stackinset{l}{.5cm}{b}{1.8cm}{\scalebox{15}{$\bullet$}}{%
\stackinset{l}{1.5cm}{b}{2cm}{\scalebox{15}{$\bullet$}}{%
\stackinset{l}{1.2cm}{b}{0.8cm}{\scalebox{15}{$\bullet$}}{%
\textcolor{orange}{\rule{5cm}{5cm}}%
}%
}}}
\end{document}


But in the meantime, until the fix gets propagated, here is the MWE, using the stackengine package:

\documentclass{article}
\usepackage{stackengine}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{etoolbox}
\makeatletter
%\stackinset{l/c/r}{x}{b/c/t}{y}{inset}{anchor}
\renewcommand*\stackinset[6]{%
\def\conditioned@inset{\stack@delim#5\stack@delim}%
\def\conditioned@anchor{\stack@delim#6\stack@delim}%
\ifstrequal{#4}{}%
{\setlength{\stack@tmplength}{0pt}}%
{\setlength{\stack@tmplength}{#4}}%
\if c#3%
\setlength{\temp@stkl}{%
\heightof{\conditioned@anchor}+\depthof{\conditioned@anchor}%
-\heightof{\conditioned@inset}-\depthof{\conditioned@inset}}%
\fi%
\ifstrequal{#2}{}{\def\stack@tmp{0pt}}{\def\stack@tmp{#2}}%
\def\stack@lroffset{\rule{\stack@tmp}{0pt}}%
\heightof{\conditioned@inset}+\depthof{\conditioned@inset}}%
\if c#1%
\def\conditioned@inset{\stack@lroffset\stack@lroffset#5}%
\else%
\def\conditioned@inset{\stack@lroffset#5\stack@lroffset}%
\fi%
\stackengine{-\stack@tmplength}{#6}{\conditioned@inset}%
{\inset@valign{#3}}{#1}{\quietstack}{T}{S}%
}
\makeatother
\begin{document}
{\color{white}%
\stackinset{l}{.5cm}{b}{1.8cm}{\scalebox{15}{$\bullet$}}{%
\stackinset{l}{1.5cm}{b}{2cm}{\scalebox{15}{$\bullet$}}{%
\stackinset{l}{1.2cm}{b}{0.8cm}{\scalebox{15}{$\bullet$}}{%
\textcolor{orange}{\rule{5cm}{5cm}}%
}%
}}}
\end{document}