# Adding text above a node in Tikzpicture tree diagram

I'm trying to add text above the lines coming from a chance node in a Tikzpicture decision tree. It seems easy to add text at the end of a line, or above the parent node's lines, but I can't figure out how to label the lines that are farthest to the right. Here is what I have so far:

\documentclass{article}
\usepackage{tikz}
\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, 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={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[chance]{}
child{node[below]{-2900}}
child{node[line]{-900}}
edge from parent
node[above]{Plant B}
}
child{node[chance]{}
child{node[line]{-2820}}
child{node[line]{-820}}
edge from parent
node[above]{Plant A}
};
\end{tikzpicture}
\end{document}


• Welcome to TeX SE! Even when I complete your code (I think), I can't get it to compile.
– cfr
Apr 27 '15 at 0:56
• Thanks! I wonder if I added it wrong.... I'll try again. It works when I compile it. Does it matter that I'm using Tex through Sweave? Should I include \usepackage{tikz} in the code that I put in my example? Apr 27 '15 at 0:58
• @KateThomas It would be helpful to wrap your code fragment in a full document (with \documentclass, etc. and packages loaded) so that others can play with it. Apr 27 '15 at 1:03
• Done! @AlanMunn Apr 27 '15 at 1:05
• I probably misplaced the labels. I wasn't sure where they were meant to end up.
– cfr
Apr 27 '15 at 1:31

You can simply add more edge from parent instructions in your child nodes:

\documentclass{article}
\usepackage{tikz}

\tikzstyle{decision} = [rectangle, minimum height=18pt, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt]
\tikzstyle{chance} = [circle, minimum width=18pt, draw=blue, fill=none, 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={thick, draw=blue},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)},
every node/.style={text ragged, inner sep=1mm}
}
\begin{document}
\begin{tikzpicture}[]
\small
\node[decision]{}
child{node[chance]{}
child{node[below]{-2900}}
child{node[line]{-900}}
edge from parent
node[above]{Plant B}
}
child{node[chance]{}
child{node[line] {-2820}
edge from parent
node[above] {Foo}}
child{node[line]{-820}
edge from parent
node[above] {Bar}}
edge from parent
node[above]{Plant A}
};
\end{tikzpicture}
\end{document}


As an aside, this kind of tree is much simpler to do using the forest package. As I write this answer, I'm sure cfr is making up an answer showing how.

• I have no idea how you managed to guess. I realise that I've never actually learnt to draw trees the TiKZ way. (Except mind maps.) Before forest, I used qtree...
– cfr
Apr 27 '15 at 1:28
• Thank you so much! I had tried using the edge from parent command, but had the spacing and brackets off so it wouldn't compile. Apr 27 '15 at 1:44

I couldn't figure out your code so I redrew the tree in forest. Note how much more compactly the tree can be specified with this package. (It is no secret that if it is a tree, I'll draw it with forest...)

\documentclass[tikz,border=5pt,multi]{standalone}
\usepackage{forest}
\standaloneenv{forest,tikzpicture}
\begin{document}

\tikzset{
decision/.style={rectangle, minimum height=18pt, minimum width=18pt,     draw=blue, fill=none, thick, inner sep=0pt},
chance/.style={circle, minimum width=18pt, draw=blue, fill=none, thick, inner sep=0pt},
line/.style={draw=none},
}

\begin{forest}
my label/.style={
edge label={node[auto,sloped,midway,anchor=south]{#1}}
},
for tree={
grow=0,
parent anchor=east,
child anchor=west,
anchor=west,
text ragged,
inner sep=1mm,
edge={thick, draw=blue},
if level=1{
for parent={
l sep=52mm
},
s sep=50mm,
l sep=67mm
}{
if level=2{
s sep=40mm
}{
if level=3{
s sep=30mm
}{}
}
},
}
[, decision
[, chance, my label=Some Label
[-2900, my label=Fails
]
[-900, my label=Plant B
]
]
[, chance
[-2820, my label=Some Other Thing
]
[-820, my label=Plant A
]
]
]
\end{forest}

\end{document}


# EDIT

In case it is of interest, here's a variation:

\documentclass[tikz,border=5pt,multi]{standalone}
\usepackage{forest}
\standaloneenv{forest,tikzpicture}
\begin{document}
\tikzset{
decision/.style={rectangle, minimum height=10pt, minimum width=10pt, draw=blue, fill=none, thick, inner sep=0pt},
chance/.style={circle, minimum width=10pt, draw=blue, fill=none, thick, inner sep=0pt},
}
\begin{forest}
my label/.style={
edge label={node[auto,sloped,pos=.75,anchor=south]{#1}}
},
for tree={
grow=0,
child anchor=west,
anchor=west,
text ragged,
inner sep=1mm,
edge={thick, draw=blue},
l sep+=30mm,
s sep+=5mm,
if n children=0{
before typesetting nodes={
label/.wrap pgfmath arg={right:#1}{content()},
content={},
chance,
},
}{},
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
}
}
[, decision
[, chance, my label=Plant B
[-2900, my label=Fails
]
[-900, my label=Succeeds
]
]
[, chance, my label=Plant A
[-2820, my label=Something
]
[-820, my label=Other Thing
]
]
]
\end{forest}
\end{document}

• I originally started to use the forest package to draw this tree, but couldn't figure out how to add the text at the end of the lines (the utility values). The ability to make the lines look more like an actual decision tree, as shown in the linked SE response, seemed like a big advantage of the forest package over tikz also. Thanks for your help! tex.stackexchange.com/questions/177249/decision-trees-in-tikz Apr 27 '15 at 1:46
• @KateThomas So does this solve the problem/answer your question...? Or not...?
– cfr
Apr 27 '15 at 2:16
• Yep! Thank you so much. Just wanted the labels above the lines which are the farthest to the right. @cfr Apr 27 '15 at 2:24
• @KateThomas OK. Good. I've edited my answer to show another variant just by way of showing forest's flexibility, really. (It is neither quite like the one in the linked answer nor quite like the original one you drew but maybe of interest to somebody.)
– cfr
Apr 27 '15 at 2:35