[Tex/LaTex] Color overlap between ellipse nodes

tikz-pgf

This is very closely related to a question about highlighting the overlap between circular nodes at Highlight overlap between nodes and draw arrow from node edge. But there is a new angle: how to generalize this to ellipses (and I suppose other shapes, but I care about ellipses for the moment).

You might expect that the following code would fill in the overlap between the ellipses with green:

\documentclass{article}

\usepackage{tikz}

\newcommand*{\MyRadius}{7cm}

\begin{document}

\def\A{(0,0) ellipse [minimum width = 2*\MyRadius, minimum height = \MyRadius]}
\def\B{(8,0) ellipse [minimum width = 2*\MyRadius, minimum height = \MyRadius]}

\begin{tikzpicture}

\node[draw, shape=ellipse, minimum width=2*\MyRadius, minimum height=\MyRadius, ultra thick] (A) at (0,0) {};
\node[draw, shape=ellipse, minimum width=2*\MyRadius, minimum height=\MyRadius, ultra thick] (B) at (8,0) {};

\begin{scope}
\clip \A;
\fill[green, opacity=0.2] \B;
\end{scope}

\end{tikzpicture}

\end{document}

But it doesn't. I also tried specifiying the height and width of the ellipses in (x, y) format, instead of in square brackets, but that is even worse (produces an error).

ellipses without color in overlap

Best Answer

Note that there is a difference between the path ellipse, which has parameters x radius and y radius, and the shape ellipse, which is not predefined but to be found in the shapes.geometric library and for which minimum width is an "x diameter". Once this is taken into account you can use the same type of code as you used for circles:

Sample output

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\newcommand*{\MyRadius}{2cm}

\begin{document}

\def\A{(0,0) ellipse [x radius = 2*\MyRadius, y radius = \MyRadius]}
\def\B{(4,0) ellipse [x radius = 2*\MyRadius, y radius = \MyRadius]}

\begin{tikzpicture}
  \node[draw, shape=ellipse, minimum width=4*\MyRadius, minimum height=2*\MyRadius, ultra thick] (A) at (0,0) {};
\node[draw, shape=ellipse, minimum width=4*\MyRadius, minimum height=2*\MyRadius, ultra thick] (B) at (4,0) {};

\begin{scope}
\clip \A;
\fill[green, opacity=0.2] \B;
\end{scope}

\end{tikzpicture}

\end{document}

Adding your node connecting like operations as your circle case now works as expected too:

Second sample

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\newcommand*{\MyRadius}{2cm}

\begin{document}

\def\A{(0,0) ellipse [x radius = 2*\MyRadius, y radius = \MyRadius]}
\def\B{(4,0) ellipse [x radius = 2*\MyRadius, y radius = \MyRadius]}

\begin{tikzpicture}
  \node[draw, shape=ellipse, minimum width=4*\MyRadius, minimum height=2*\MyRadius, ultra thick] (A) at (0,0) {};
\node[draw, shape=ellipse, minimum width=4*\MyRadius, minimum height=2*\MyRadius, ultra thick] (B) at (4,0) {};

\begin{scope}
\clip \A;
\fill[green, opacity=0.2] \B;
\end{scope}

\node(nom) at (0,-3) {[A]}; 
\node(gen) at (4,-3) {[B]}; 

\draw[->]  (A) -- (nom);
\draw[->]  (B) -- (gen);
\end{tikzpicture}

\end{document}