2

I'm having trouble moving the labels for the third level of the tree down to prevent overlap. Could someone help me? I tried changing yshift with no luck.

\begin{tikzpicture}[font=\footnotesize,edge from parent/.style={draw,thick}]
% Two node styles: solid and hollow
\tikzstyle{solid node}=[circle,draw,inner sep=1.2,fill=black];
\tikzstyle{hollow node}=[circle,draw,inner sep=1.2];
% Specify spacing for each level of the tree
\tikzstyle{level 1}=[level distance=15mm,sibling distance=40mm]
\tikzstyle{level 2}=[level distance=15mm,sibling distance=20mm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=15mm]
% The Tree
\node(0)[hollow node]{}
child{node[solid node]{}
child{node[solid node]{}
child{node[below, rotate=90]{$(6,6,6)$} edge from 
parent node[left]{$A$}}
child{node[below, rotate=90]{$(-7,-6,-5)$} edge from parent node[right]{$R$}}
edge from parent node[above left]{$A$}
}
child{node[solid node]{}
child{node[below, rotate=90]{$(-7,-5,-6)$} edge from parent node(s)[left]{$A$}}
child{node[below, rotate=90]{$(-7,-6,-6)$} edge from parent node(t)[right]{$R$}}
edge from parent node[above right]{$R$}
}
edge from parent node[above left]{$G$}
}
child{node[solid node]{}
child{node[solid node]{}
child{node[below, rotate=90]{$(-7,-5,-5)$} edge from parent node(m)[left]{$A$}}
child{node[below, rotate=90]{$(-1,2,-1)$} edge from parent node(n)[right]{$R$}}
edge from parent node[above left]{$A$}
}
child{node[solid node]{}
child{node[below, rotate=90]{$(-1,-1,2)$} edge from parent node[left]{$A$}}
child{node[below, rotate=90]{$(-1,2,2)$} edge from parent node[right]{$R$}}
edge from parent node[above right]{$R$}
}
edge from parent node[above right]{$B$}
};
% movers
\draw[dashed,rounded corners=10]($(1) + (0,1)$)rectangle($(2) +(0,.5
)$);
\draw[dashed,rounded corners=10,minimum size=60mm]($(1) + (0,-1)$)rectangle($(2) +(0,-1
)$);
\node[above,yshift=2]at(0){{$Investor\;1$}};
\node[above,xshift=23mm, yshift=5mm]at(1){{$Investor\;3$}};
\node[above,xshift=23mm, yshift=-10mm]at(1){{$Investor\;2$}};
\end{tikzpicture}
  • 2
    Welcome to TeX.SX! Can you please make your code compilable? Even after adding \documentclass and loading tikz and calc, pgf complains about unknown shapes (1) and (2). – gernot says Reinstate Monica Dec 19 '16 at 17:25
  • I can't reproduce the image you show (disregarding the dashed shapes), but what happens if you change below to left in the rotated nodes? (And unrelated: Write Investor 1, not $Investor\;1$.) – Torbjørn T. Dec 19 '16 at 19:03
  • Sorry, I'm new here (clearly)! Thank you for the insight. Any idea on how to fix the second dashed shape to extend farther (i.e. greater width)? Thank you again. – morgan Dec 19 '16 at 19:38
  • It helps if you post code for a complete document and check that the output you are showing (if applicable) is reproduced when you compile exactly that document. It gets difficult when people can't recreate the result you want help with! – cfr Dec 20 '16 at 3:27
3

This is derived from Torbjørn T.'s Forest solution, modified to leverage some of the specific features provided by the package.

Defining a couple of styles and controlling the appearance of the tree in the tree's preamble (the bit after \begin{forest} and before the tree specification itself), allows us to specify the tree itself quite concisely:

  [
   [
     [
       [{(6,6,6)}]
       [{(-7,-6,-5)}]
     ]
     [
       [{(-7,-5,-6)}]
       [{(-7,-6,-6)}]
     ]
   ]
   [
     [
       [{(-7,-5,-5)}]
       [{(-1,2,-1)}]
      ]
     [
       [{(-1,-1,2)}]
       [{(-1,2,2)}]
     ]
   ]
  ]

Note that the terminal nodes are set in maths mode, thanks to math content, despite the loss of $...$. The rest is handled automatically, based on nodes' levels and whether a branch is a left or right hand one. The dashed enclosures use fit, as in Torbjørn T.'s solution, but relative node names are used rather than names, which saves cluttering the tree. For example, !r11 means the root's first child's first child, while !rll means the root's last child's last child. l sep is used to increase the distance between levels somewhat and s sep to increase the distance between siblings, except for the terminal nodes.

% addaswyd o ateb Torbjørn T.: https://tex.stackexchange.com/a/344835/
\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{fit}
\forestset{
  /tikz/my branch label/.style={midway, font=\footnotesize, anchor=mid west},
  /tikz/my investor label/.style={densely dashed, draw, rounded corners},
  branch labels/.style n args=2{% position the edge labels for the children of the current node
    !1.edge label={coordinate [label={[my branch label, anchor=mid east]{135+\forestoption{level}*15}:#1}] (a)},
    !l.edge label={coordinate [label={[my branch label, anchor=mid west]{45-\forestoption{level}*15}:#2}] (a)},
  }
}
\begin{document}
\begin{forest}
  for tree={
    l sep'+=20pt,
    font=\footnotesize,
  },
  where n children=0{% for the terminal nodes i.e. no children
    rotate=90,
    math content,
    anchor=parent,
    child anchor=parent,
  }{
    circle,
    draw,
    inner sep=1.5pt,
    s sep'+=5pt,
    if level=0{% the root note
      branch labels={G}{B},
      label={[font=\footnotesize]above:Investor 1},
      tikz+={
        \node [my investor label] (i3) [fit=(!11) (!ll)] {};
        \node [font=\footnotesize] at (i3) {Investor 3};
        \node [my investor label] (i2) [fit=(!1) (!l)] {};
        \node [font=\footnotesize] at (i2) {Investor 2};
      },
    }{
      fill,
      branch labels={A}{R},
    }
  },
  [
   [
     [
       [{(6,6,6)}]
       [{(-7,-6,-5)}]
     ]
     [
       [{(-7,-5,-6)}]
       [{(-7,-6,-6)}]
     ]
   ]
   [
     [
       [{(-7,-5,-5)}]
       [{(-1,2,-1)}]
      ]
     [
       [{(-1,-1,2)}]
       [{(-1,2,2)}]
     ]
   ]
  ]
\end{forest}
\end{document}

less verbose Forest solution

  • Nice! Certainly (and unsurprisingly) an improvement on my feeble attempt. (And thanks for the edit, by the way.) – Torbjørn T. Dec 20 '16 at 8:40
  • @TorbjørnT. Thanks. And welcome. (My code is derived from yours, of course. I didn't think you'd mind.) – cfr Dec 20 '16 at 16:08
3

Your code doesn't seem to produce the image you posted, but here is something that might give you what you want. If I misunderstood something, please comment.

The first thing to note is that the tuples at the end are set using labels for the nodes, instead of the node text directly. This is done to get the text directly below the ends of the lines.

Note I defined a new style, endnode, that is used for those nodes. It is defined as

endnode/.style={coordinate,label={[rotate=90,left]below:#1}},

The #1 means that the style must be used with a single argument, i.e. endnode={text}, where text is the actual label. The reason for using left instead of below, is that left sets anchor=east, and rotation also rotates the anchors I believe, so the east anchor will be on the top of the rotated node. The coordinate style means that the node itself will have zero width/height.

To draw the dashed shapes, I used the features of the fit library. Note that nodes along the branches in a tree will be numbered by layer and branch. So if the base node is called root (it doesn't have to be called this, you could keep 0 as in your code), the first child is called root-1, the second is called root-2. The first child of root-1 is called root-1-1, and so on.

The line

\node [fit=(root-1)(root-2),draw,dashed,rounded corners=5pt,label=above:Investor 2]{};

makes a node that encompasses the root-1 and root-2 nodes. The border is drawn with a dashed line and rounded corners, and a label is added above.

Further note that \tikzstyle is considered deprecated. For styles that should be available in other diagrams as well, use \tikzset{stylename/.style={...}} in the preamble, instead of \tikzstyle{stylename}=[...]. Here I defined the styles in the optional argument to the tikzpicture environment instead, as they seemed to be local to the tikzpicture anyway.

enter image description here

\documentclass[12pt,border=5mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[
  font=\footnotesize,
  edge from parent/.style={draw,thick},
  % Two node styles: solid and hollow
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
  endnode/.style={coordinate,label={[rotate=90,left]below:#1}},
  % Specify spacing for each level of the tree
  level 1/.style={level distance=15mm,sibling distance=40mm},
  level 2/.style={level distance=15mm,sibling distance=20mm},
  level 3/.style={level distance=15mm,sibling distance=15mm}
]
% The Tree
\node(root)[hollow node,label=above:{Investor 1}]{}
  child{node[solid node]{}
  child{node[solid node]{}
    child{node[endnode={$(6,6,6)$}]{}
      edge from parent
        node[left]{$A$}}
    child{node[endnode={$(-7,-6,-5)$}]{}
      edge from parent
        node[right]{$R$}}
      edge from parent
        node[above left]{$A$}
    }
  child{node[solid node]{}
    child{node[endnode={$(-7,-5,-6)$}]{}
      edge from parent
        node(s)[left]{$A$}}
    child{node[endnode={$(-7,-6,-6)$}]{}
      edge from parent
        node(t)[right]{$R$}}
      edge from parent
        node[above right]{$R$}
  }
  edge from parent
   node[above left]{$G$}
  }
  child{node[solid node] {}
    child{node[solid node]{}
      child{node[endnode={$(-7,-5,-5)$}]{}
        edge from parent
          node(m)[left]{$A$}}
      child{node[endnode={$(-1,2,-1)$}]{}
        edge from parent
          node(n)[right]{$R$}}
       edge from parent
         node[above left]{$A$}
    }
  child{node[solid node]  {}
    child{node[endnode={$(-1,-1,2)$}]{}
      edge from parent
        node[left]{$A$}}
    child{node[endnode={$(-1,2,2)$}]{}
      edge from parent
        node[right]{$R$}}
      edge from parent
        node[above right]{$R$}
  }
  edge from parent
    node[above right]{$B$}
};

% movers
\node [fit=(root-1)(root-2),draw,dashed,rounded corners=5pt,label=above:Investor 2]{};
\node [fit=(root-1-1)(root-2-2),draw,dashed,rounded corners=5pt,label=above:Investor 3]{};
\end{tikzpicture}
\end{document}

A forest suggestion

Well, this is a tree, and one of the more popular packages for drawing trees is forest. I don't really use it myself though, so the code below was scrambled together after some scrolling in the manual. Hence, there is a good chance that it is possible to do it much better.

(Addendum: As I was saying, see cfr's answer for a more elegant solution using forest.)

enter image description here

\documentclass[border=5mm,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{fit}
\tikzset{
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
  edgelabel/.style={auto,pos=0.6,font=\footnotesize}
}
\begin{document}
\begin{forest}
[, name=root, hollow node, for tree={rotate=90,left,font=\footnotesize}
 [ , name=a1, solid node, edge label={node[edgelabel,swap]{$G$}}
   [ , name=b1, solid node, edge label={node[edgelabel,swap]{$A$}}
     [ {$(6,6,6)$}, edge label={node[edgelabel,swap]{$A$}}]
     [ {$(-7,-6,-5)$}, edge label={node[edgelabel]{$R$}} ] ]
   [  , solid node, edge label={node[edgelabel]{$R$}}
     [ {$(-7,-5,-6)$}, edge label={node[edgelabel,swap]{$A$}} ]
     [ {$(-7,-6,-6)$}, edge label={node[edgelabel]{$R$}} ]
   ] 
 ]
 [ , name=a2, solid node, edge label={node[edgelabel]{$B$}}
   [ ,solid node, edge label={node[edgelabel,swap]{$A$}}
     [ {$(-7,-5,-5)$}, edge label={node[edgelabel,swap]{$A$}} ]
     [ {$(-1,2,-1)$}, edge label={node[edgelabel]{$R$}} ]
    ]
   [, name=b2, solid node, edge label={node[edgelabel]{$R$}}
     [ {$(-1,-1,2)$}, edge label={node[edgelabel,swap]{$A$}} ]
     [ {$(-1,2,2)$}, edge label={node[edgelabel]{$R$}} ]
   ]
 ]
{
\node [above,font=\footnotesize] at (root.north) {Investor 1};
\node [fit=(a1)(a2),dashed,draw,rounded corners=5pt,label=above:\footnotesize Investor 2]{};
\node [fit=(b1)(b2),dashed,draw,rounded corners=5pt,label=above:\footnotesize Investor 3]{};
}
]
\end{forest}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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