0

I am trying to draw a graph that can be represented as boolean operations on three shapes: A, B, C. The graph is A - B - C (or A - (B U C)). However, I have no Idea how to draw it using \clip or other commands in Tikz.

For example, here is some code adapted from TikZ example, and the first picture below is what the code generates, but the second is what I want.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\begin{document}
\pagestyle{empty}
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(45:2cm) circle (1.5cm)}
\def\thirdcircle{(0:2cm) circle (1.5cm)}

\begin{tikzpicture}
    \draw \firstcircle node[below] {$A$};
    \draw \secondcircle node [above] {$B$};
    \draw \thirdcircle node [below] {$C$};
    \begin{scope}
      \clip \firstcircle;
      \fill[red] \secondcircle;
    \end{scope}
    \begin{scope}
      \clip \firstcircle;
      \clip \secondcircle;
      \fill[green] \thirdcircle;
    \end{scope}
\end{tikzpicture}

\end{document}

_

Edit: To be specific, I am not trying to draw a Venn diagram, but to draw some shape from three basic shapes, but the implementation need to:

  1. Allow any kind of shapes (not only circles)
  2. Transparent (cannot use the white color)
  3. Image border does not include B and C

See this new image as an example

new image

2
  • 1
    Well after your edit, your question is a whole other story ... Maybe remove the venn-diagram tag, it feels a bit misleading to me
    – BambOo
    Sep 29 '19 at 21:33
  • @BambOo You are right, I should not have put the venn-diagram tag. Do you know any other tags I should put? Sep 29 '19 at 21:42
3

As for your updated question: this can be done with the reverse clip and even odd clip tricks. Assume we want to have the triangle (-4,0) -| (0,2) -- cycle minus the rectangle (-1,-0.5) rectangle (1,0.5) and minus the circle (0.5,1) circle[radius=1cm]. Then we need to

  1. add the path of the triangle to have some non-empty picture (but we don't draw that),
  2. add the rectangle with reverse clip' (actually there is a subtlety: the orientation of the outer path matters, which is why there is a primed version for the "other" orientation),
  3. add an even odd reverse clip for the circle.

Voila.

\documentclass[tikz,border=3mm]{standalone}
\tikzset{current reverse clip/.style={insert path={{% https://tex.stackexchange.com/a/127045
   ([xshift=-10cm,yshift=-10cm]current bounding box.south east)  -|
  ([xshift=10cm,yshift=10cm]current bounding box.north west) -| cycle
  }}},
  current reverse clip'/.style={insert path={{% https://tex.stackexchange.com/a/127045
   ([xshift=-10cm,yshift=-10cm]current bounding box.south east)  -|
  ([xshift=10cm,yshift=10cm]current bounding box.north west) -| cycle
  }}},
  even odd clip/.code={% https://tex.stackexchange.com/a/76216
  \pgfseteorule}}
\begin{document}
\begin{tikzpicture}
\path (-4,0) -| (0,2) -- cycle;
\clip[overlay] (-1,-0.5) rectangle (1,0.5) [current reverse clip'];
\clip[overlay,even odd clip] (0.5,1) circle[radius=1cm] [current reverse clip];
\draw[left color=red,right color=blue]  (-4,0) -| (0,2) -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

Let me mention that there is an experimental library venn which can be found here. This hosts some variations of the above-mentioned styles. Save this file as tikzlibraryvenn.code.tex in some place where LaTeX can find it, such as the same directory as the file you compile. The manual can be found here, where you can find further clipping examples. With this library getting the diagram for the original question is as simple as

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{venn}
\begin{document}
\begin{tikzpicture}[Venn diagram={style={fill=blue},offset angle=60}]
 \Venn{-B,-C,A}
\end{tikzpicture}
\end{document}

enter image description here

Or you can use the perhaps more intuitive syntax

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{venn}
\begin{document}
\begin{tikzpicture}[Venn diagram={style={fill=blue},offset angle=60}]
 \Venn{op={(A)/(BuC)}}
\end{tikzpicture}
\end{document}

BTW, if you have grumble with the syntax, say, if you'd prefer \Venn{op={(A)-(BuC)}} instead, now is the time to give such feedback.

6
  • How can I present A minus B, where A is a subset of B? Sep 30 '19 at 3:29
  • @minhthien_2016 This is a simple even odd rule task. Depending on what you need to do maybe \fill[even odd rule] <path B> <path A>; is what you need, or \clip[even odd clip] <path B> <path A>; <some other drawing commands>.
    – user194703
    Sep 30 '19 at 3:32
  • Is there in your tool? Sep 30 '19 at 4:27
  • @minhthien_2016 These styles are in the above answer with links to the posts where they might have been invented. Some variations thereof are in the experimental library venn.
    – user194703
    Sep 30 '19 at 4:31
  • Thank you very much. Sep 30 '19 at 4:37

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.