Home > tikz pgf > Drawing shapes around Tikz Tree nodes

Drawing shapes around Tikz Tree nodes

October 15Hits:3
Advertisement

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:

Drawing shapes around Tikz Tree nodes

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:

Drawing shapes around Tikz Tree nodes

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

Answers

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}

Drawing shapes around Tikz Tree nodes

Related Articles

  • Drawing shapes around Tikz Tree nodes

    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 node

    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)

    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?

    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?

    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 nodes

    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 level

    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 TikZ

    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);

  • Bad math delimiter in tikz treeMarch 3

    I'm trying to diagram an extensive form game using TikZ trees. The following code generates errors at compile time. Specifically, it complains about missing $'s and a bad math delimiter. I've been over the brackets with a fine-tooth comb and I can't

  • tikz-qtree spoils tikz trees

    tikz-qtree spoils tikz treesAugust 12

    When I enable the tikz-qtree package, the tree I am drawing with the tikz trees library looks different. Specifically the edges are no longer pointing to proper places on the nodes. In the example below, the tree looks fine when I comment out \usepac

  • How do I draw shapes inside a tikz node?

    How do I draw shapes inside a tikz node?July 23

    I know how to draw different shapes using tikz, like this: \documentclass{minimal} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw (0,0) ellipse (1 and 1.5) (0,2.5) circle (1) (2.5,0) circle (1.5); \end{tikzpicture} \end{document} Howeve

  • Draw Tikz Tree where level distance between multi-line nodes is constant

    Draw Tikz Tree where level distance between multi-line nodes is constantMarch 26

    I am drawing a tree with tikz, namely, using tikz-qtree-compat for simpler syntax. On the tree there can be nodes of different height but then branches become messy. I want to add an instruction where distance between the south point of a mother node

  • Aligning multiple TikZ trees with other nodes

    Aligning multiple TikZ trees with other nodesMay 3

    This question is directly related to Aligning TikZ trees with other nodes that I asked some time ago. By the time I asked the question I didn't realize that aligning multiple trees was not the same as aligning one tree. Indeed, the answer proposed by

  • tikz trees - children attached to wrong node

    tikz trees - children attached to wrong nodeJune 11

    Consider \usetikzlibrary{calc,decorations.pathreplacing,shapes.symbols} \begin{tikzpicture} \node [shape=cloud,aspect=2,draw] {\strut} [level distance=6em,sibling distance=1.5em,grow=45] child {node[shape=circle,draw] (B) {}} child {node[shape=circle

  • How do I draw merged red black tree into 2,3,4 tree in tikz?

    How do I draw merged red black tree into 2,3,4 tree in tikz?April 17

    I'm using tikz in study material and I want to show analogy between RB trees and B-trees (2,3,4 tree). I want my output looks something like this example from Wikipedia. For RB tree I'm using almost exactly same settings as is showed in http://www.te

  • How to cross out a child of a node in a tikz tree?

    How to cross out a child of a node in a tikz tree?September 3

    Consider this simple tikz tree: \documentclass{article} \usepackage{tikz} \usetikzlibrary{trees,shapes,snakes} \begin{document} \tikzstyle{mynode} = [text width=4em, text centered] \begin{tikzpicture}[] \node{My Root} child{node[mynode]{Good Morning}

  • TIKZ Tree: Change angle between Nodes

    TIKZ Tree: Change angle between NodesJanuary 25

    I am struggling to find out how to change the angle between nodes in tikz trees. I have already tried the attribute sibling angle=... specifically for child nodes or as level 1/.style=... attribute at the beginning of the \tikzpicture environment. Si

  • Tikz - How to Draw Boxes Around Set of Nodes

    Tikz - How to Draw Boxes Around Set of NodesJanuary 13

    How to draw the boxes as shown in the attached image? I could only manage to draw the chart but could not highlight the desired node / nodes with a rectangle box. Preferably dotted. I could not draw dotted in MS paint. \documentclass{article} \usepac

  • Aligning TikZ trees with other nodes

    Aligning TikZ trees with other nodesApril 24

    I would like to align a TikZ tree with another node by considering the tree as a whole like as if it was a node with anchors. For the moment I am only able to align the root node of the tree, not the whole tree itself. I use PGF 2.10. The following p

  • Draw an arc between 2 nodes and label it in TikZ

    Draw an arc between 2 nodes and label it in TikZSeptember 2

    I have already drawn two ellipses l1 and l2, l2 is on the left below l1: \begin{tikzpicture} \node (l1) [ellipse, draw=black, fill=white!20, text=black, scale=0.8, text centered]{ $l_1$}; \node (l2) [ellipse, draw=black, fill=white!20, text=black, sc

Copyright (C) 2018 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.510 s.