# Placing a triangle around nodes in a tree

I have a tree filled with numbers and I want to mark a subtree by drawing a triangle around it. I can't seem to get it working.

My attempt:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,fit}
\begin{document}
\begin{tikzpicture}
\node at (0,0) {3};

\node (7) at (-1/2,-1) {7};
\node at (1/2,-1) {4};

\node at (-1,-2) {2};
\node at (0,-2) {4};
\node at (1,-2) {6};

\node (8) at (-3/2,-3) {8};
\node at (-1/2,-3) {5};
\node (9) at (1/2,-3) {9};
\node at (3/2,-3) {3};

\node[isosceles triangle,fit = (7) (8) (9)] {};
\end{tikzpicture}
\end{document}

-
If I'm not wrong, fit library only works with rectangle and ellipse. You can draw an isosceles triangle, but not with fit. –  Ignasi Dec 7 '11 at 16:26
You should first add draw option to your last node. Then notice that the triangle is too big. It might be much more precise if you just do it manually. –  percusse Dec 7 '11 at 16:28

You need to explicitly add the draw option (red triangle) to use fit library, or you could use the calc and pgf library to compute vertices of the triangle (blue triangle):

## Code: draw option:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,fit}
\begin{document}
\begin{tikzpicture}
\node at (0,0) {3};

\node (7) at (-1/2,-1) {7};
\node at (1/2,-1) {4};

\node at (-1,-2) {2};
\node at (0,-2) {4};
\node at (1,-2) {6};

\node (8) at (-3/2,-3) {8};
\node at (-1/2,-3) {5};
\node (9) at (1/2,-3) {9};
\node at (3/2,-3) {3};

\node[isosceles triangle,fit = (7) (8) (9),draw=red,thick] {};
\end{tikzpicture}
\end{document}


## Code: Computed Vertices:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes,fit, calc}

\pgfmathsetmacro{\sinOffset}{sin(60)}
\pgfmathsetmacro{\cosOffset}{cos(60)}

\begin{document}
\begin{tikzpicture}
\node at (0,0) {3};

\node (7) at (-1/2,-1) {7};
\node at (1/2,-1) {4};

\node at (-1,-2) {2};
\node at (0,-2) {4};
\node at (1,-2) {6};

\node (8) at (-3/2,-3) {8};
\node at (-1/2,-3) {5};
\node (9) at (1/2,-3) {9};
\node at (3/2,-3) {3};

\draw [blue, thick]
($(7)+(0,1)$) --
($(8)+(-\sinOffset,-\cosOffset)$) --
($(9)+( \sinOffset,-\cosOffset)$) -- cycle;
\end{tikzpicture}
\end{document}

-
Thank you! The blue triangle was what I was going for. –  Ben Ruijl Dec 7 '11 at 21:13
@Peter A little remark : i't not useful to load pgf.sty before tikz because the first line of this file is \RequirePackage{pgf,pgffor} and for your example "Computed Vertices", only the library calc is used. –  Alain Matthes Dec 8 '11 at 16:03
@Altermundus: Thanks.Have removed the pgf package from 2nd example. –  Peter Grill Dec 9 '11 at 21:15