Home > tikz pgf > Drawing shapes around Tikz Tree nodes

# Drawing shapes around Tikz Tree nodes

October 15Hits:3

I have a Tikz tree in my Latex document as follows:

% Set the overall layout of the tree \tikzstyle{level 1}=[level distance=4.5cm, sibling distance=6.5cm] \tikzstyle{level 2}=[level distance=4cm, sibling distance=2.5cm]  % Define styles for bags and leafs \tikzstyle{bag} = [text width=8em, text centered] \tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt] \tikzstyle{atom} = [draw=black,thick]  % The sloped option gives rotated edge labels. Personally % I find sloped labels a bit difficult to read. Remove the sloped options % to get horizontal labels.  \begin{tikzpicture}[grow=right, sloped] \node[bag] {$S_0 = 1$}     child {         node[bag] {$S_1 = 0.90$}                     child {                 node[end, label=right:                     {$S_2 = 0.810$}] {}                 edge from parent                 node[above] {$Y_2 = y_3$}             }             child {                 node[end, label=right:                     {$S_2 = 0.900$}] {}                 edge from parent                 node[above] {$Y_2 = y_2$}             }             child {                 node[end, label=right:                     {$S_2 = 0.918$}] {}                 edge from parent                 node[above] {$Y_2 = y_1$}             }             edge from parent              node[above] {$Y_1 = y_3$}     }     child {         node[bag] {$S_1 = 1.00$}                 child {                 node[end, label=right:                     {$S_2 = 0.900$}] {}                 edge from parent                 node[above] {$Y_2 = y_3$}             }             child {                 node[end, label=right:                     {$S_2 = 1.000$}] {}                 edge from parent                 node[above] {$Y_2 = y_2$}             }             child {                 node[end, label=right:                     {$S_2 = 1.020$}] {}                 edge from parent                 node[above] {$Y_2 = y_1$}             }             edge from parent                      node[above] {$Y_1 = y_2$}     }     child {         node[bag] {$S_1 = 1.02$}                 child {                 node[end, label=right:                     {$S_2 = 0.918$}] {}                 edge from parent                 node[above] {$Y_2 = y_3$}             }             child {                 node[end, label=right:                     {$S_2 = 1.020$}] {}                 edge from parent                 node[above] {$Y_2 = y_2$}             }             child {                 node[end, label=right:                     {$S_2 = 1.0404$}] {}                 edge from parent                 node[above] {$Y_2 = y_1$}             }         edge from parent                      node[above] {$Y_1 = y_1$}     }; \end{tikzpicture} 


With result:

I would like to draw rounded rectangles around the end nodes of each of the three first-level children. There would therefore be three nodes in each rectangle, with the top three in the first, middle three in the second, and lower three in the last. I would also like labels vertically centered on the right of each rectangle. The final result should be as so:

As I am completely new to Tikz, I do not know how to achieve this result. Help!

The idea (as Peter Grill suggested in a comment) is to name the necessary nodes and to use the fit library; auxiliary nodes were used; a simple loop draws the three frames and places the labels; also I changed from the obsolete \tikzstyle to \tikzset:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,fit}

\begin{document}

% Set the overall layout of the tree
\tikzset{
level 1/.style={level distance=4.5cm, sibling distance=6.5cm},
level 2/.style={level distance=4cm, sibling distance=2.5cm}
}

% Define styles for bags and leafs
\tikzset{
bag/.style = {text width=8em, text centered},
end/.style = {circle, minimum width=3pt,fill, inner sep=0pt},
atom/.style = {draw=black,thick}
}

% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {$S_0 = 1$}
child {
node[bag] {$S_1 = 0.90$}
child {
node[end, label=right:
{$S_2 = 0.810$}] (b3) {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 0.900$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 0.918$}] (t3) {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_3$}
}
child {
node[bag] {$S_1 = 1.00$}
child {
node[end, label=right:
{$S_2 = 0.900$}] (b2) {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 1.000$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 1.020$}] (t2) {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_2$}
}
child {
node[bag] {$S_1 = 1.02$}
child {
node[end, label=right:
{$S_2 = 0.918$}] (b1) {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 1.020$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 1.0404$}] (t1) {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_1$}
};
\begin{scope}[label distance=6mm,]
\foreach \i/\label in {1/Label 1,2/Label 2,3/Label 3}
{
\coordinate  (aux\i) at ([xshift=2cm]t\i);
\node[inner sep=6pt,rounded corners=6pt,draw,red,fit={(t\i) (b\i) (aux\i)},label=right:{\color{red}\label}] {};
}
\end{scope}
\end{tikzpicture}

\end{document}



## Related Articles

• ### Drawing shapes around Tikz Tree nodesOctober 15

I have a Tikz tree in my Latex document as follows: % Set the overall layout of the tree \tikzstyle{level 1}=[level distance=4.5cm, sibling distance=6.5cm] \tikzstyle{level 2}=[level distance=4cm, sibling distance=2.5cm] % Define styles for bags and

• ### Centering text in TikZ tree nodeDecember 10

I'd like to have my text inside TikZ tree nodes centered; below is my progress so far: \documentclass[10pt, a4paper]{article} \usepackage{tikz} \usetikzlibrary{arrows} \begin{document} \tikzset{ treenode/.style = {align=center, inner sep=1pt, text ce

• ### Showing Tikz tree nodes after each other (using bracketing format of \Tree)November 22

I want to display a tikz tree node by node. First A, then A and B and in the end all nodes. Note that I need to do it with the presented format: \Tree [. \node{A};] since trees are automatically generated in this format and I want to only modify them

• ### How can I use fork style edges with explicitely positioned TikZ tree nodes?July 4

As part of a graphical export facility, I try to render a tree in TikZ were all the node positions are exactly given and were the edges are fork-style. Doing that without the trees package demands that I calculate the edge 'corners' by myself, based

• ### How can I make Tikz tree node arcs not cross over each other?April 30

I am trying to build a tree (the theme is argumentation theory, where arguments attack and defend each other) in Tikz but after a few levels down the tree, nodes are placed on top of each other or in a way that the arcs to their parents cross over ea

• ### Properly spread tikz tree nodesJune 29

In the middle of drawing blooming tree, which is declared in generic way, something is going wrong. Please, help me to separate middle nodes and put boxes with borders on every node. \begin{tikzpicture}[text width=5cm, align=flush center,node distanc

• ### Tikz tree drawing with comments to each levelJuly 17

I have problem with drawing trees with tikz. The tree is correct, but 'rightarrows' wrongly indicate to text, which is related to each tree-level (it doesn't look good). Of course, the text should by shown also without circles. Could anyone can help

• ### Skip drawing one of the borders in a shape with TikZJune 25

I am drawing shapes with TikZ and I would like to selectively draw the borders around it: \documentclass{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture}[scale=3., line width=1pt] \coordinate (a) at (0,0); \coordinate (b1) at (1,1);