Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

enter image description here

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:

What I would like

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

share|improve this question
1  
Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE. While solving problems is fun, setting them up is not. Then those trying to help can simply cut and paste your MWE and get started on solving problem. –  Peter Grill Oct 15 '12 at 20:18
4  
You should be able to use the fit tikz library to achieve this. See for example the tutorial style answer at how to draw a container box in latex around automata?. –  Peter Grill Oct 15 '12 at 20:19
    
Similar question: Highlighting some nodes of a TikZ binomial tree. –  Claudio Fiandrino Oct 16 '12 at 7:09
add comment

1 Answer

up vote 10 down vote accepted

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}

enter image description here

share|improve this answer
    
Works great, but gives me the warning "No argument following \label" in the foreach loop. –  lodhb Oct 16 '12 at 8:10
    
@lodhb if the exact code I posted gives you warnings, most probably your LaTeX system is outdated. What LaTeX distribution and version do you use? –  Gonzalo Medina Oct 16 '12 at 11:52
    
Yes it is the exact same code. I am using MiKTeX 2.9.4476 , downloaded last week. –  lodhb Oct 17 '12 at 15:21
    
@lodhb Ah, I see. I use TeX Live2012 and (besides an overfull \hbox warning) the code runs OK in my system. No idea why it generates the warning you mention in your system (I haven't used MikTeX in a long while). Fortunately the warning seems to be harmless. –  Gonzalo Medina Oct 17 '12 at 17:12
    
Perfectly harmless, I just have a natural aversion to warnings and errors :-) thanks again! –  lodhb Oct 18 '12 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.