3

I am currently trying to change the color of the intersection between two circles in a venn diagram. I have tried different proposed solution here on stack without success. The code I am running is the following;

\begin{center}
\usetikzlibrary{shapes,backgrounds}
\pagestyle{empty}
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(60:2cm) circle (1.5cm)}
\def\thirdcircle{(0:2cm) circle (1.5cm)}
\begin{tikzpicture}
\begin{scope}[shift={(3cm,-5cm)}, fill opacity=0.5]
    \fill[green] \firstcircle;
    \fill[green] \secondcircle;
    \fill[red] \thirdcircle;
    \draw \firstcircle node[below] {$A$};
    \draw \secondcircle node [above] {$B$};
    \draw \thirdcircle node [below] {$C$};
\end{scope}
\end{tikzpicture} 
\end{center} 
Paul Stanley
  • 18,151
hamp
  • 33
  • Have a look at https://tex.stackexchange.com/q/67395/86 and its answers. You may be able to adapt one of those to what you're trying to do. – Andrew Stacey Apr 12 '18 at 11:38

2 Answers2

2

Here is a Metapost version to provide some diversion while you are waiting for the TikZ mountain rescue team. Compile with lualatex for the mplib support (or work out how to adapt it for GMP).

enter image description here

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    path C[];
    C1 = fullcircle rotated -60 scaled 3cm;
    C2 = C1 shifted (2cm, 0);
    C3 = C2 rotated 60;

    path r[];

    r1 = buildcycle(C1, C2);
    r2 = buildcycle(C2, C3);
    r3 = buildcycle(C3, C1);
    r4 = buildcycle(C1, C2, C3);

    fill C1 withcolor 3/4[red, white];
    fill C2 withcolor 3/4[green, white];
    fill C3 withcolor 3/4[blue, white];

    fill r1 withcolor 3/4[red+green,white];
    fill r2 withcolor 3/4[green+blue,white];
    fill r3 withcolor 3/4[blue+red,white];
    fill r4 withcolor 3/4[red+green+blue, white];

    draw C1;  label("A", center C1);
    draw C2;  label("B", center C2);
    draw C3;  label("C", center C3);
endfig;
\end{mplibcode}
\end{document}

NB: the rotated -60 on the definition of C1 is important. It stops the point 0 of each circle being contained in the others. This is necessary to avoid a feature of buildcycle.

Thruston
  • 42,268
1

fill opacity=.5 already fills all intersections with different colors. But if this is not enough for you and prefer to select the color for every intersection, you can use following code:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning}
\usetikzlibrary{shapes,backgrounds}
\begin{document}
\def\firstcircle{(0,0) circle (1.5cm)}
\def\secondcircle{(60:2cm) circle (1.5cm)}
\def\thirdcircle{(0:2cm) circle (1.5cm)}
\begin{tikzpicture}
\begin{scope}[shift={(3cm,-5cm)}, fill opacity=1]
    \fill[green] \firstcircle node[black] {$A$};
    \fill[blue] \secondcircle node[black] {$B$};
    \fill[red] \thirdcircle node[black] {$C$};
    \begin{scope}
        \clip \firstcircle;
        \fill[brown] \secondcircle;
        \fill[cyan] \thirdcircle;
    \end{scope}
    \begin{scope}
        \clip \secondcircle;
        \fill[orange] \thirdcircle;
        \clip \firstcircle;
        \fill[pink] \thirdcircle;
    \end{scope}
\end{scope}
\end{tikzpicture} 

\end{document}

enter image description here

Ignasi
  • 136,588