3

I have the following code that I am trying to understand better:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[htb] 
\begin{tikzpicture} 
\def\firstcircle{ (-1,0) circle (2cm) } 
\def\secondcircle{ (1,0) circle (2cm) }

\begin{scope}       
    \clip \firstcircle;     
    \fill[red!50!white] \secondcircle;      
\end{scope}

\begin{scope}
        \begin{scope}[even odd rule]
            \clip  \secondcircle (-3,-3) rectangle (3,3);   
            \fill[blue!50!white] \firstcircle;
    \end{scope}
\end{scope}
\end{tikzpicture}
\end{figure}
\end{document}

This draws a Venn diagram with two circles. The first scope environment clips out the first circle and then shades red everything in the first circle that is also part of the second circle. Is that correct?

The second scope is confusing to me. I don't understand why I need to type both \secondcircle and (-3,3) rectangle (3,3). Why does this have the effect of shading what is in the first circle that is not in the second circle?

  • 1
    You can of course simplify this by filling the blue circle first, i.e. \fill[blue!50!white] \firstcircle; \clip \firstcircle; \fill[red!50!white] \secondcircle; No scoping necessary. – Torbjørn T. Sep 22 '16 at 20:17
  • @TorbjørnT. Thanks for the input. That is useful to know. In whole picture that I am drawing, I've also shaded the part in the second circle that is not in the first. Is there anyway to do that without using \scope? – Joe Johnson 126 Sep 22 '16 at 20:29
  • 1
    The purpose of the scopes is to keep the effect of \clip local. \clip only affects what comes after it so, in general, if you can draw things in an order so that all the paths that need clipping come at the end of the diagram, then you don't need a scope. – Torbjørn T. Sep 22 '16 at 20:49
6

The second \clip command contains two different closed paths, the second circle and a rectangle, a little larger than the current drawing area. The even odd rule says, that if an area is covered by one, three, five, ... areas (odd-numbered), than this area is inside the clipping result path. Otherwise, if an area is not covered, covered by two, four, ... areas (even-numbered), than this area is excluded.

In this case, the full rectangle except the second circle is in the clipping area of the second case.

The pgf manual explains the rule a little different (probably a little more precise, but also more complicate). It can be found as subsection "15.5.2 Graphic Parameters: Interior Rules" below "15.5 Filling a Path".

The example in the question can be refined further:

  • The nested scope environment is not necessary.
  • The magic numbers for the rectangle can be replaced by using the dimensions of the current bounding box.
  • Option overlay removes the second circle and rectangle from the bounding box calculations, only leaving the first circle there. \fbox visualizes the fixed bounding box.

Full example:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[htb]
% Check bounding box with \fbox
\setlength{\fboxsep}{0pt}
\fbox{\begin{tikzpicture}

\def\firstcircle{ (-1,0) circle (2cm) }

\def\secondcircle{ (1,0) circle (2cm) }

\begin{scope}

    \clip \firstcircle;

    \fill[red!50!white, overlay] \secondcircle;

\end{scope}

\begin{scope}[even odd rule, overlay]

    \clip
      \secondcircle
      (current bounding box.south west) rectangle
      (current bounding box.north east)
    ;

    \fill[blue!50!white] \firstcircle;

\end{scope}

\end{tikzpicture}}
\end{figure}
\end{document}

Result

  • It works without even odd rule as well. – Torbjørn T. Sep 22 '16 at 20:16
  • @TorbjørnT. Default is the nonzero rule (nonzero winding number rule). This rule is more complex, because also the direction of the paths matter. In this case, the areas are created with circle and rectangle. Apparently, they are created with different directions. AFAIK, the directions are not specified, thus it might not work in other circumstances. Also using a circle instead of the rectangle would not work. – Heiko Oberdiek Sep 22 '16 at 20:26
  • Let me see if I understand: It takes a point in the first circle and figures out if it is in just the rectangle (one area) or both the rectangle and the second circle (two areas). In the former case it will color it because one is odd. In the latter case it will not color it because two is even. – Joe Johnson 126 Sep 22 '16 at 20:37
  • Also, I don't quite understand your comment about the bounding box. You say it removes the second circle and rectangle from the calculation. But, isn't the bounding box the only rectangle around? How does it remove itself from the calculation? – Joe Johnson 126 Sep 22 '16 at 20:38
  • @JoeJohnson126 TikZ uses all coordinates for its calculation of the bounding box. It does not matter is the coordinate is actually part of a visible drawing. Therefore the final bounding box is the superset of the first, second circle and the rectangle, apparently to large, if the visible area is just the first circle. The example uses option overlay for all elements except the first circle in the first clip path. – Heiko Oberdiek Sep 22 '16 at 20:41

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.