9

below you can see my code of a tree besides a table.

Now I would like to insert some rectangles and ellipses around the nodes. Can someone show me how I can do this. I tried it but it did not work. Thank you!

\documentclass[doc,apacite]{apa}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%%%%Tikz%%%%%%
\usepackage{tikz}
\usetikzlibrary{trees,matrix,fit}
%%%%%%%%%%%%%%
%%%%Table%%%%%
\usepackage{ctable} % needed for \cmidrule{}
\usepackage{multirow} % needed for \multirow{}
%%%%%%%%%%%%%%
\usepackage{pgfkeys}

\begin{document}

    \begin{tikzpicture}[
        grow=right,
        sloped,
        bag/.style={text width=4em, text centered},
        end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
        sibling distance=\sibdist,
        level 1/.style={level distance=2.0cm},
        level 2/.style={level distance=2.5cm},
        every node/.style={font=\strut},
        ]
      \node(n){x}
          child {
              node{U2}        
                  child {
                      node[label=right:
                          ]{D2} {}
                              child {node[end, label=right:
                              {R1}] {}
                              edge from parent
                              node[above] {$1-R$}
                              node[below]  {}}
                              child {node[end, label=right:
                              {R2}] {}
                              edge from parent
                              node[above] {$R$}
                              node[below]  {}}
                              child[missing]
                      edge from parent
                      node[above] {$1-D$}
                      node[below]  {}
                  }
                  child {%empty child on level 2
                    child{
                        node[end, label=right:
                            {D1}] {}
                        edge from parent
                        node[above] {$D$}
                        node[below]  {}
                    }
                  }
                  child[missing]
                  edge from parent 
                  node[above] {}
                  node[below]  {$1-u$}
          }
          child {
              node{U1}        
                  child{% empty child on level 2
                    child{
                        node[end, label=right:
                            {$T$}](e) {}
                        edge from parent
                        node[above] {}
                        node[below]  {}
                    }
                  }
              edge from parent         
                  node[above] {$u$}
                  node[below]  {}
          };
      \matrix[
          matrix of nodes,
          anchor=m-4-1.west,
          xshift=1cm,
          nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
        ](m)at(n-1-1-1){%
          c&b&a&c&bd&a\\
          a&c&b&a&a&c\\
          x&g&f&c&d&b\\%[1cm]
          ta&g&a&x&p&d\\
      };
      \foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
      \foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);
    \end{tikzpicture}

\end{document}
3
  • 1
    There is missing definition for \sibdist in your code. I'm not sure what exactly you mean by "insert rectangles and ellipses around the nodes". If my guess is correct, however, you could try the following: put \usetikzlibrary{shapes} in the preamble, and add options draw,ellipse (for ellipse) or simply draw (for rectangle) to the node commands (in addition to above/below/etc).
    – Herr K.
    Commented Jul 25, 2014 at 20:43
  • I mean rectangles like in this example: texample.net/tikz/examples/work-breakdown-structure
    – Gilfoyle
    Commented Jul 27, 2014 at 14:50
  • Then I think my comment above should solve the problem.
    – Herr K.
    Commented Jul 27, 2014 at 18:36

1 Answer 1

7

enter image description here

You can use the fit library to construct a shape fitting the desired group of nodes; in the case of ellipses you'll need the shapes library. Since the root node of the three was called n, you can access the children using n-<string>; for exmaple, n-1-1 corresponds to D2, and n-2 is U1.

The code for the exmaple:

\documentclass[doc,apacite]{apa}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{trees,matrix,fit,shapes}
\usepackage{ctable}
\usepackage{multirow}
\usepackage{pgfkeys}

% I added the following two lines since they were missing in the example code
\newlength\sibdist
\setlength\sibdist{1cm}

\begin{document}

    \begin{tikzpicture}[
        grow=right,
        sloped,
        bag/.style={text width=4em, text centered},
        end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
        sibling distance=\sibdist,
        level 1/.style={level distance=2.0cm},
        level 2/.style={level distance=2.5cm},
        every node/.style={font=\strut},
        ]
      \node(n){x}
          child {
              node{U2}        
                  child {
                      node[label=right:
                          ]{D2} {}
                              child {node[end, label=right:
                              {R1}] {}
                              edge from parent
                              node[below] {$1-R$}
                              node[below]  {}}
                              child {node[end, label=right:
                              {R2}] {}
                              edge from parent
                              node[above] {$R$}
                              node[below]  {}}
                              child[missing]
                      edge from parent
                      node[below] {$1-D$}
                      node[below]  {}
                  }
                  child {%empty child on level 2
                    child{
                        node[end, label=right:
                            {D1}] {}
                        edge from parent
                        node[above] {$D$}
                        node[below]  {}
                    }
                  }
                  child[missing]
                  edge from parent 
                  node[above] {}
                  node[below]  {$1-u$}
          }
          child {
              node{U1}        
                  child{% empty child on level 2
                    child{
                        node[end, label=right:
                            {$T$}](e) {}
                        edge from parent
                        node[above] {}
                        node[below]  {}
                    }
                  }
              edge from parent         
                  node[above] {$u$}
                  node[below]  {}
          };
      \matrix[
          matrix of nodes,
          anchor=m-4-1.west,
          xshift=1cm,
          nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
        ](m)at(n-1-1-1){%
          c&b&a&c&bd&a\\
          a&c&b&a&a&c\\
          x&g&f&c&d&b\\%[1cm]
          ta&g&a&x&p&d\\
      };
      \foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
      \foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);

% A red rectangle enclosing U1,U2,T,D1:
\node[draw=red,fit={(n-2) (n-1-2-1)}] {};      
% A blue ellipse enclosing R2,R1:
\node[draw=blue,ellipse,fit={(n-1-1-1) (n-1-1-2)}] {};      

    \end{tikzpicture}

\end{document}

Update:

After a comment, the request was to have some of the bodes of the trees as rectangles or ellipses; this can be achieved by defining some styles and then passing these styles as options for the desired nodes; in the below example I used

        myrect/.style={draw=orange,fill=orange!30,text width=1cm,align=center},
        myellipse/.style={ellipse,draw=cyan,fill=cyan!30,inner sep=2pt,text width=1cm,align=center},

but those settings can be changed to suit needs.

\documentclass[doc,apacite]{apa}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{trees,matrix,shapes,fit}
\usepackage{ctable}
\usepackage{multirow}
\usepackage{pgfkeys}

% I added the following two lines since they were missing in the example code
\newlength\sibdist
\setlength\sibdist{1cm}

\begin{document}

    \begin{tikzpicture}[
        grow=right,
        sloped,
        bag/.style={text width=4em, text centered},
        end/.style={circle, minimum width=3pt,fill, inner sep=0pt,font=\tiny},
        sibling distance=\sibdist,
        level 1/.style={level distance=2.5cm},
        level 2/.style={level distance=2.5cm},
        every node/.style={font=\strut},
        myrect/.style={draw=orange,fill=orange!30,text width=1cm,align=center},
        myellipse/.style={ellipse,draw=cyan,fill=cyan!30,inner sep=2pt,text width=1cm,align=center},
        ]
      \node[myrect] (n){x}
          child {
              node[myellipse] {U2}        
                  child {
                      node[myrect,label=right:
                          ]{D2} {}
                              child {node[end, label=right:
                              {R1}] {}
                              edge from parent
                              node[below] {$1-R$}
                              node[below]  {}}
                              child {node[end, label=right:
                              {R2}] {}
                              edge from parent
                              node[above] {$R$}
                              node[below]  {}}
                              child[missing]
                      edge from parent
                      node[below] {$1-D$}
                      node[below]  {}
                  }
                  child {%empty child on level 2
                    child{
                        node[end, label=right:
                            {D1}] {}
                        edge from parent
                        node[above] {$D$}
                        node[below]  {}
                    }
                  }
                  child[missing]
                  edge from parent 
                  node[above] {}
                  node[below]  {$1-u$}
          }
          child {
              node[myellipse] {U1}        
                  child{% empty child on level 2
                    child{
                        node[end, label=right:
                            {$T$}](e) {}
                        edge from parent
                        node[above] {}
                        node[below]  {}
                    }
                  }
              edge from parent         
                  node[above] {$u$}
                  node[below]  {}
          };
      \matrix[
          matrix of nodes,
          anchor=m-4-1.west,
          xshift=1cm,
          nodes={inner xsep=\tabcolsep,minimum height=\sibdist}
        ](m)at(n-1-1-1){%
          c&b&a&c&bd&a\\
          a&c&b&a&a&c\\
          x&g&f&c&d&b\\%[1cm]
          ta&g&a&x&p&d\\
      };
      \foreach \i in {1,...,6}\node[draw,fit=(m-1-\i) (m-2-\i) (m-3-\i) (m-4-\i),inner sep=-.5\pgflinewidth](c\i){};
      \foreach \i in {1,...,3}\draw([yshift=-.5*\sibdist]m-\i-1.center-|c1.west)--([yshift=-.5*\sibdist]m-\i-1.center-|c6.east);

    \end{tikzpicture}

\end{document}

enter image description here

Just as a suggestion, the powerful forest package could be of interest for you to draw trees in a more easy way.

4
  • @ Gonzalo Medina: Thank you for the answer. Actually I wanted ellipses and rectangles like in this tree: texample.net/tikz/examples/work-breakdown-structure
    – Gilfoyle
    Commented Jul 27, 2014 at 14:42
  • @Samuel Ah, I see. Please see my updated answer. Is it something like that what you had in mind? Commented Jul 28, 2014 at 0:48
  • @ Gonzalo Medina: That looks very good. Is there any possibility to stretch the ellipses and rectangles horizontally?
    – Gilfoyle
    Commented Jul 28, 2014 at 7:40
  • @Samuel Sure. You can use text width=<length> to have the desired width for the nodes. I've updated my answer with a little example of the use of this option; adjust the settings according to your needs. Commented Jul 28, 2014 at 14:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .