Will clip in Tikz to get the complement of the union?

Here is another method using the invclip key (from https://tex.stackexchange.com/a/59168/14500):

\documentclass{standalone}
\usepackage{tikz}

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

\begin{document}
\begin{tikzpicture}
  \def\A{(-1,0) circle (1.5cm)}
  \def\B{(0,0) circle (1.2cm)}

  \begin{scope}
    \begin{pgfinterruptboundingbox}
      \clip[invclip] \B;
    \end{pgfinterruptboundingbox}
    \fill[fill=blue!50]\A;
  \end{scope}

  \begin{scope}
    \begin{pgfinterruptboundingbox}
      \clip[invclip] \A;
    \end{pgfinterruptboundingbox}
    \fill[fill=red!50]\B;
  \end{scope}

  \begin{scope}
    \begin{pgfinterruptboundingbox}
      \clip[invclip] \A \B;
    \end{pgfinterruptboundingbox}
    \filldraw[gray!50] (current bounding box.south west) rectangle (current bounding box.north east);
  \end{scope}

  \draw \A;
  \draw \B;

\end{tikzpicture}
\end{document}

enter image description here


Note the even odd rule does not clip the union of both circles.

Lines are drawn half in and half out of a region, so unless you want to reduce their width by half, draw them last.

Also, you were providing two semicolons, and everything after the first is ignored until the next \path etc.

\documentclass{standalone}
  \usepackage{tikz}
  \usetikzlibrary{hobby}

  \begin{document}
  \def\A{(-1,0) circle (1.5cm)}
  \def\B{(0,0) circle (1.2cm)}
  \begin{tikzpicture}[use Hobby shortcut,closed=true]
    \begin{scope}[fill opacity=0.5]
      \fill[color=blue]\A;
      \fill[color=red] \B;
    \end{scope}

    \begin{scope}[even odd rule]
      \clip (-3,-2) rectangle (3,2) \B \A;
      \fill[gray!50] (-3,-2) rectangle (3,2);
    \end{scope}
    \draw \A;
    \draw \B;
  \end{tikzpicture}
  \end{document}

demo


It is straightforward to determine the contour of the union with calc and intersections.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc,intersections}
\begin{document}
  \def\A{(-1,0) circle [radius=1.5cm];}
  \def\B{(0,0) circle [radius=1.2cm];}
  \begin{tikzpicture}
    \begin{scope}[fill opacity=0.5]
      \fill[color=blue]\A;
      \fill[color=red] \B;
    \end{scope}
      \draw[name path=A] \A;
      \draw[name path=B] \B;

    \begin{scope}[fill opacity=1]
     \path[name intersections={of=A and B},fill=orange]
      let \p1=($(intersection-1)-(-1,0)$),\p2=($(intersection-1)$),
       \n1={atan2(\y1,\x1)},\n2={atan2(\y2,\x2)} in
      (intersection-1) arc[start angle=\n1,end angle=360-\n1,radius=1.5]
      arc[start angle=360-\n2,end angle=\n2+360,radius=1.2]
      -- cycle  (-3,-2) rectangle (1.7,2) ;
    \end{scope}
  \end{tikzpicture}
\end{document}

enter image description here

Tags:

Tikz Pgf