Decision trees in Tikz

So I can do this:

But I want to do this:

That is, its easy to add text alongside the connectors (either slanting alongside them or straight), but I would like part of the connector to be flat with the caption text along the flat part for better readability.

I thought perhaps I could do this by creating a fake node that would be just a line with text above it. Not sure how to implement that or if there is an easier way.

Thanks for any ideas?

Code for first figure:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, ultra thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, ultra thick, inner sep=0pt]
\tikzstyle{line} = [draw=none]

\tikzset{
grow=right,
sloped,
join=miter,
level 1/.style={sibling distance=5cm,level distance=5.2cm},
level 2/.style={sibling distance=4cm, level distance=6.7cm},
level 3/.style={sibling distance=3cm, level distance=6.7cm},
edge from parent/.style={ultra thick, draw=blue},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every node/.style={text ragged, inner sep=1mm}
}

\begin{tikzpicture}[]
\small
\node[decision]{}
child{node[line]{}
edge from parent
node[above]{Option 2}
}
child{node[chance]{}
child{node[line]{}}
child{node[line]{}}
edge from parent
node[below]{Option 1}
};
\end{tikzpicture}

\end{document}

-
And will the text always be of the form "Option #"? –  Gonzalo Medina May 14 at 20:39
Oh no, not necessarily. Just a short description of that branch. –  user2726768 May 14 at 20:41
Your "I can do this" should be accompanied by a minimal working example (MWE) always. It makes it far quicker for the community to get going on a solution than having to rewrite code from scratch. Always. –  Werner May 14 at 20:53
Thanks for the tip. First time poster. –  user2726768 May 15 at 15:11

1 Answer

You can use the forest package. A little example (adjust the settings according to your needs):

\documentclass{article}
\usepackage{forest}

\tikzset{
Above/.style={
midway,
above,
font=\scriptsize,
text width=1.5cm,
align=center,
},
Below/.style={
midway,
below,
font=\scriptsize,
text width=1.5cm,
align=center
}
}

\begin{document}

\begin{forest}
for tree={
grow=east,
draw=cyan,
circle,
line width=0.2pt,
parent anchor=east,
child anchor=west,
edge={draw=cyan},
edge label={\Huge\color{black}},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- ([xshift=-1.6cm].child anchor) --
(.child anchor)\forestoption{edge label};
},
l sep=2cm,
}
[,rectangle, s sep=35pt,
[,edge label={node[Below]{option1}}
[,edge label={node[Below]{a longer text goes here}}
]
[,edge label={node[Above]{text}}
]
]
[,edge label={node[Above]{option2}}
[,edge label={node[Below]{a longer text goes here}}
]
[,edge label={node[Above]{text}}
]
]
]
\end{forest}

\end{document}


-
Hey, that looks great! Thanks! –  user2726768 May 14 at 20:58
@user2726768 You're welcome! I made a simplification to the code that might be of interest for you. –  Gonzalo Medina May 14 at 21:03