4

I need to create a plot with an area filled outside a circle. Because the filled area has to be colored with 2 different colors, simply applying the odd-even rule did not work. This, it worked for one color or the other but not for both. That is not my problem. It is solved.

From other posts in this forum I was pointed to this article: http://www.texample.net/tikz/examples/venn-diagram/. The applicable example is the yellow circle A with the area of circle B cut out.

I modified the example slightly to make visible what I did. This is the MWE code:

\documentclass[a4]{article}
\usepackage{tikz}
\begin{document}

\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(45:2cm) circle (1.5cm)}

\begin{tikzpicture}
\begin{scope}[shift={(6cm,-10cm)}]
    \draw[help lines] (0,0) grid (4,5);
    \begin{scope}[even odd rule]% first circle without the second
        \draw[clip] \secondcircle (-3,-3) rectangle (3,3);
        \fill[yellow] \firstcircle;
    \end{scope}
\draw \firstcircle node {$A$};
\draw \secondcircle node {$B$};
\end{scope}
\end{tikzpicture}
\end{document}

This works and solved my own problem as well after modifying the code so it suited my picture.

venn-diagram in square http://www.linxtech.net/~jlinkels/links/venn-diagram.jpg

The nagging question is, why does it work? What makes it happen that the yellow circle is colored, but the area occupied by circle B is empty. I understand that B remains empty when the square is filled. But first the square is not filled, does it still apply that B remains empty? Adding circle A is completely opaque (no pun intended). Why would A fill, but still leave B's area empty?

6

To see it better I've moved things around a bit. First of all, you are switching to even odd rule for clipping. An example to see how the areas are counted as even and odd

\begin{tikzpicture}[even odd rule]
\fill[clip] (0,0) circle (2cm) (60:2.5cm) arc (60:-60:2.5) (60:3cm) arc (60:-60:3cm);
\end{tikzpicture}

enter image description here

Note that this is a PostScript directive and not a TikZ thing. The implementation is pretty strange indeed.

So coming back to your example, you are punching a hole in the rectangle via \secondcircle so anything that is between the rectangle and the complement of second circle is visible and the rest is cut off in that scope. See how first circle is cut. Instead of the left edge if you put it closer to the punched hole it would also be cut which is what is happening in your case. The grid is drawn as the first thing so nothing happens to it but yellow fill covers the bottom left part creating the illusion that it is also clipped. You can see it there if you remove the yellow fill (in my example also the black fill too).

\def\firstcircle{(-2,0) circle (1.5cm)}
\def\secondcircle{(45:2cm) circle (1cm)}

\begin{tikzpicture}
\begin{scope}[shift={(6cm,-10cm)}]
    \draw[help lines] (0,0) grid (4,5);
    \begin{scope}[even odd rule]% first circle without the second
        \fill[clip] \secondcircle (-3,-3) rectangle (3,3);
        \fill[yellow] \firstcircle;
    \end{scope}
\draw (-2,0) node {$A$};
\draw (45:2) node {$B$};
\end{scope}
\end{tikzpicture}

enter image description here

  • So with the first \fill[clip] \secondcircle (-3,-3) rectangle (3,3); you are defining a rectangle with a hole. All subsequent objects (after this statement) will be clipped by both the rectangle and hole "B". But except for the mentioned clipping they will just be drawn as normally filled and overlapping objects. If I want more holes I have to write them in the same line as the rectangle. If the holes are overlapping, the overlaps are filled again because of the odd-even rule. – jlinkels Nov 25 '14 at 1:21
  • @jlinkels Spot on. One detail; the clip survives until the end of the scope it is defined and on all the objects that comes after it in that particular scope. That's why you can put the label B etc. – percusse Nov 25 '14 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.