5

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

sautedman
  • 231
  • Please complete the code so it is a MWE, and replace the minimal class (article is a good substitute) as per why should the minimal class be avoided?. – Peter Grill Jan 06 '15 at 20:17
  • It works exactly as your previus question. But you have to learn how to draw ellipses : ellipse [x radius = \MyRadius, y radius = .5*\MyRadius]. The syntax is not the same as the ellipse shape for nodes. And to make your code compilable with TikZ 3.0 you have to add \usetikzlibrary{shapes.geometric}. – Kpym Jan 06 '15 at 20:35
  • 1
  • @Kpym: This is NOT a duplicate of that question. This is about ellipses, the other is about circle. The two questions are very much related so good to have them cross referenced. – Peter Grill Jan 06 '15 at 20:53
  • @PeterGrill: As an aside... would every question about a different shape then NOT be a duplicate? – Werner Jan 06 '15 at 21:05
  • @Werner: This is not a duplicate just because it is a different shape. IMHO, The handling of circle and rectangle for \node shapes is quite different from other shapes. This one can be considered a more general question than the one linked to above - so if a question is to be closed I would recommend the one about circular shape be closed. – Peter Grill Jan 06 '15 at 21:23
  • @PeterGrill could you explain what is different for the "other shapes" please. The solutions of this question are identical to the solutions of the previous one : you replace "circle" by what you want. The proof : if you correct the code in the question to make it compilable, it works ! – Kpym Jan 06 '15 at 21:28
  • @Kpym: Does the other question require \usetikzlibrary{shapes.geometric}? – Peter Grill Jan 06 '15 at 22:01
  • @PeterGrill Is this question : how I draw ellipse node ? – Kpym Jan 06 '15 at 22:06
  • 1
    At the very least, attempting what seems to be a straightforward extension of the suggested code from the other question fails (much like trying to extend other examples led to the first question). A non-tikz expert would be surprised to learn that there are two different things (paths and shapes) with the same name (ellipse) and that they behave quite differently. I'd say this is sufficiently different/informative to be relevant for users of this quite opaque package. – sautedman Jan 07 '15 at 01:14

2 Answers2

6

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}
Andrew Swann
  • 95,762
2

This answer of @Kpym works also for other shapes because there is no need to reproduce the nodes by paths.

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{shapes.geometric}

\newcommand*{\MyRadius}{2cm}
\tikzset{
  myellipse/.style={draw, shape=ellipse, minimum width=4*\MyRadius, minimum height=2*\MyRadius, ultra thick}
}
\begin{document}
  \begin{tikzpicture}
    \node[myellipse,
      path picture={\node[myellipse,fill=green,opacity=0.2] at (4,0){};}
    ]at(0,1) (A){};
    \node[myellipse] (B) at (4,1){};
  \end{tikzpicture}
\end{document}

enter image description here

Or another example:

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{shapes.symbols}

\newcommand*{\MyRadius}{2cm}
\tikzset{
  myecloud/.style={draw, shape=cloud, minimum width=3*\MyRadius, minimum height=2*\MyRadius, ultra thick}
}
\begin{document}
  \begin{tikzpicture}
    \node[mycloud,
      path picture={\node[mycloud,fill=green,opacity=0.2] at (4,0){};}
    ]at(0,1) (A){};
    \node[mycloud] (B) at (4,1){};
  \end{tikzpicture}
\end{document}

enter image description here

esdd
  • 85,675