4

I'm using forest to draw decision trees, and, if a node has two branches, everything displays as it should. However, once a node has three, the top two branches display above the node.

Here's an example of what I'm talking about

\usepackage{pgfplots} 
\pgfplotsset{width=12cm,compat=1.10}  
\usetikzlibrary{patterns}
\usepackage{adjustbox}
\usepackage{forest}

\tikzset{     
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, 
shape border rotate=180, outer sep=-\pgflinewidth}   
}

\forestset{
 declare toks={optimality}{},
 declare toks={edge label below}{},
}




\begin{document}
\begin{center}


\begin{frame}

\scalebox{0.85}{
 \begin{forest}    
    my label/.style={       
        edge label={node[auto, sloped,pos=.75,anchor=south]{#1}}
    },
    my below/.style={
        edge label below={node[midway,below, pos = 0.75, font = \scriptsize,align = center]{#1}}
        %edge label={node[auto, sloped,pos=1.75,anchor=south]{#1}}
    },
    for tree={
      grow=0,
      child anchor=west,
      anchor=west,
      text ragged,
      inner sep=1mm,
      edge={thick, draw=black},
      l sep+=30mm,
      s sep+=5mm,
      if n children=0{
        before typesetting nodes={
          label/.wrap pgfmath arg={right:#1}{content()},
          content={},
          leaf-chance,
        },
      }{},
      edge path={
       \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label below}\forestoption{edge label};
  },
    }
    [, decision, label = left: Getting ice cream,
        [,chance,my label=TSweets
            [{$5-2=3$}, my label = Rain $\rightarrow$ Rocky Road, my below = 0.10, tier = word]
            [5,  my label = No rain $\rightarrow$ Rocky Road, my below = 0.90, tier = word]
        ]
        [,chance,my label=Bent Spoon
            [, chance, my label = Rain, my below = 0.30
                [{$10-2=8$}, my label = Chocolate cardamom, my below = 0.10, tier = word]
                [{$7-2=5$},  my label = Blueberry sorbet, my below = 0.25, tier = word]
                [{$1-2=-1$},  my label = Other flavors, my below = 0.65, tier = word]
            ]
            [, chance, my label = No Rain, my below = 0.70
                [10, my label = Chocolate cardamom, my below = 0.10, tier = word]
                [7,  my label = Blueberry sorbet, my below = 0.25, tier = word]
                [1,  my label = Other flavors, my below = 0.65, tier = word]
            ]
        ]
    ]
\end{forest} }
\end{frame}

\end{center} 

This code creates the following tree: Nodes with overlapping branches

You can see that the decision and chance nodes with two branches display correctly. The ones with more than two do not.

Thanks!

2

Welcome to TeX-SE! The reason why some of the connections of the path are on top of the nodes is that forest gets confused by the anchors of the parent node. It is, however not too difficult to fix this. With calc, which is automatically loaded by forest, one can find out whether the child node is at a higher, lower or the same y-value as the parent, and set the anchor accordingly.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage[edges]{forest}

\tikzset{     
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, 
shape border rotate=180, outer sep=-\pgflinewidth}   
}

\forestset{
 declare toks={optimality}{},
 declare toks={edge label below}{},
}




\begin{document}
 \begin{forest}    
    my label/.style={       
        edge label={node[auto, sloped,pos=.75,anchor=south]{#1}}
    },
    my below/.style={
        edge label below={node[midway,below, pos = 0.75, font = \scriptsize,align = center]{#1}}
        %edge label={node[auto, sloped,pos=1.75,anchor=south]{#1}}
    },
    for tree={
      grow=0,
      child anchor=west,
      anchor=west,
      text ragged,
      inner sep=1mm,
      edge={thick, draw=black},
      l sep+=30mm,
      s sep+=5mm,
      if n children=0{
        before typesetting nodes={
          label/.wrap pgfmath arg={right:#1}{content()},
          content={},
          leaf-chance,
        },
      }{},
      edge path'={
       let \noexpand\p1=($(.child anchor)-(!u)$),\noexpand\n1={sign(\noexpand\y1)} in
       \noexpand\ifdim\noexpand\y1>0pt
        (!u.north)
       \noexpand\else
        \noexpand\ifdim\noexpand\y1<0pt 
         (!u.south)
        \noexpand\else
         (!u.east)
        \noexpand\fi
       \noexpand\fi  
        |- (.child anchor)},
    }
    [, decision, label = left: Getting ice cream,
        [,chance,my label=TSweets
            [{$5-2=3$}, my label = Rain $\rightarrow$ Rocky Road, my below = 0.10, tier = word]
            [5,  my label = No rain $\rightarrow$ Rocky Road, my below = 0.90, tier = word]
        ]
        [,chance,my label=Bent Spoon
            [, chance, my label = Rain, my below = 0.30
                [{$10-2=8$}, my label = Chocolate cardamom, my below = 0.10, tier = word]
                [{$7-2=5$},  my label = Blueberry sorbet, my below = 0.25, tier = word]
                [{$1-2=-1$},  my label = Other flavors, my below = 0.65, tier = word]
            ]
            [, chance, my label = No Rain, my below = 0.70
                [10, my label = Chocolate cardamom, my below = 0.10, tier = word]
                [7,  my label = Blueberry sorbet, my below = 0.25, tier = word]
                [1,  my label = Other flavors, my below = 0.65, tier = word]
            ]
        ]
    ]
\end{forest} 
\end{document}

enter image description here

Or

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage[edges]{forest}

\tikzset{     
    decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    chance/.style={circle, minimum width=10pt, draw=black, fill=black!30!white, thick, inner sep=0pt},
    leaf-chance/.style={isosceles triangle, minimum width=10pt, draw=black, thick, fill=white, inner sep=0pt, 
shape border rotate=180, outer sep=-\pgflinewidth}   
}

\forestset{
 declare toks={optimality}{},
 declare toks={edge label below}{},
}




\begin{document}
 \begin{forest}    
    my label/.style={       
        edge label={node[auto, sloped,pos=.75,anchor=south]{#1}}
    },
    my below/.style={
        edge label below={node[midway,below, pos = 0.75, font = \scriptsize,align = center]{#1}}
        %edge label={node[auto, sloped,pos=1.75,anchor=south]{#1}}
    },
    for tree={
      grow=0,
      child anchor=west,
      anchor=west,
      text ragged,
      inner sep=1mm,
      edge={thick, draw=black},
      l sep+=30mm,
      s sep+=5mm,
      if n children=0{
        before typesetting nodes={
          label/.wrap pgfmath arg={right:#1}{content()},
          content={},
          leaf-chance,
        },
      }{},
      edge path={
       \noexpand\path let \noexpand\p1=($(.child anchor)-(!u)$),\noexpand\n1={sign(\noexpand\y1)} in
       [draw, \forestoption{edge}]
       \noexpand\ifdim\noexpand\y1>0pt
        (!u.north)
       \noexpand\else
        \noexpand\ifdim\noexpand\y1<0pt 
         (!u.south)
        \noexpand\else
         (!u.east)
        \noexpand\fi
       \noexpand\fi  
        |- (.child anchor)\forestoption{edge label below}\forestoption{edge label};
  },
    }
    [, decision, label = left: Getting ice cream,
        [,chance,my label=TSweets
            [{$5-2=3$}, my label = Rain $\rightarrow$ Rocky Road, my below = 0.10, tier = word]
            [5,  my label = No rain $\rightarrow$ Rocky Road, my below = 0.90, tier = word]
        ]
        [,chance,my label=Bent Spoon
            [, chance, my label = Rain, my below = 0.30
                [{$10-2=8$}, my label = Chocolate cardamom, my below = 0.10, tier = word]
                [{$7-2=5$},  my label = Blueberry sorbet, my below = 0.25, tier = word]
                [{$1-2=-1$},  my label = Other flavors, my below = 0.65, tier = word]
            ]
            [, chance, my label = No Rain, my below = 0.70
                [10, my label = Chocolate cardamom, my below = 0.10, tier = word]
                [7,  my label = Blueberry sorbet, my below = 0.25, tier = word]
                [1,  my label = Other flavors, my below = 0.65, tier = word]
            ]
        ]
    ]
\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.