# Surrounding Nodes with an Ellipse in TikZ

The following code places six nodes along the perimeter of an equilateral triangle:

\begin{tikzpicture}[every node/.style={draw}]

\node (l1) at (0,0) [circle] {};
\node (l2) at (2,0) [circle] {};
\node (l3) at (4,0) [circle] {};
\node (m1) at (1, 1.732) [circle] {};
\node (m2) at (3, 1.732) [circle] {};
\node (h1) at (2, 3.464) [circle] {};

\end{tikzpicture}


How can I surround each "leg" of the triangle with an ellipse in a manner similar to the following:

• The fit library could help here, but it doesn’t try to be intelligent and does not account for rotating. Have you tried to manually draw the ellipses? There is a shape called ellipse as well as the path operatore ellipse/circle. The calc library can help to let TikZ calculate all necessary steps. – Qrrbrbirlbel Jun 12 '13 at 4:08

One possibility using the calc library to draw some rotated ellipses:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}

\begin{document}

\begin{tikzpicture}[every node/.style={draw}]
\node (l1) at (0,0) [circle] {};
\node (l2) at (2,0) [circle] {};
\node (l3) at (4,0) [circle] {};
\node (m1) at (1, 1.732) [circle] {};
\node (m2) at (3, 1.732) [circle] {};
\node (h1) at (2, 3.464) [circle] {};
\draw let \p1=(h1), \p2=(l1), \n1={atan2(\y2-\y1,\x2-\x1)}, \n2={veclen(\y2-\y1,\x2-\x1)}
in ($(h1)!0.5!(l1)$) ellipse [x radius=\n2/2+20pt, y radius=0.7cm,rotate=90-\n1];
\draw let \p1=(h1), \p2=(l3), \n1={atan2(\y2-\y1,\x2-\x1)}, \n2={veclen(\y2-\y1,\x2-\x1)}
in ($(h1)!0.5!(l3)$) ellipse [x radius=\n2/2+20pt, y radius=0.7cm,rotate=90-\n1];
\draw let \p1=(l1), \p2=(l3), \n1={atan2(\y2-\y1,\x2-\x1)}, \n2={veclen(\y2-\y1,\x2-\x1)}
in ($(l1)!0.5!(l3)$) ellipse [x radius=\n2/2+20pt, y radius=0.7cm,rotate=90-\n1];
\end{tikzpicture}

\end{document}


• Thank you for making this solution fairly general. I have been able to modify it for various shapes that arise in my illustrations. It has been a great help in my current endeavor. – Austin Mohr Jun 29 '13 at 20:09
• @AustinMohr You're welcome! I'm glad I could help. – Gonzalo Medina Jun 30 '13 at 0:15

Based on Gonzalo's answer, I wrote myself a little command

\newcommand{\fitellipsis}[2] % first and second node names without parentheses
{\draw let \p1=(#1), \p2=(#2), \n1={atan2(\y2-\y1,\x2-\x1)}, \n2={veclen(\y2-\y1,\x2-\x1)}
in ($(\p1)!0.5!(\p2)$) ellipse [x radius=\n2/2+1cm, y radius=1.5cm, rotate=\n1];
}


Following code uses an equilateral triangle made with a regular polygon node. This node is not drawn but their anchors are used to easily place the dots. Ellipses are drawn with a fit node. As all ellipses are equal they are build with same fitting points but placed and rotated according to desired positions.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, fit, shapes.geometric}

\begin{document}
\begin{tikzpicture}[
myellipse/.style={
fit=(a.corner 2) (a.corner 3),
draw, ellipse,
inner xsep=0mm,
inner ysep=3mm}]

\node[regular polygon, regular polygon sides=3, inner sep=1cm] (a) {};

\foreach \i in {1, 2, 3}{
\fill[red] (a.side \i) circle (2pt);
\fill[red] (a.corner \i) circle (2pt);
}

\node[myellipse] {};
\node[myellipse, rotate=120] at (a.side 3) {};
\node[myellipse, rotate=60] at (a.side 1) {};

\end{tikzpicture}
\end{document}