# Tree Diagram Spacing Tikz

I am having trouble fixing the spacing of my tree.

Currently, I have the following code and result:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees, arrows}
\begin{document}
\pagestyle{empty}

% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=4cm, sibling distance=4cm]
\tikzstyle{level 2}=[level distance=4cm, sibling distance=4cm]
\tikzstyle{level 3}=[level distance=20mm]

% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em,text centered]

\begin{tikzpicture}[grow=right,->,>=angle 60, sloped]
%\begin{scope}[yshift=0]
\node[bag, minimum width=2cm] {Testing Consumer $t=0$}
child {
node[bag, minimum width=2cm] {$E[u(c,l,\eta_1)|K]$}
edge from parent
node[above] {$\kappa = 0$}
}
child {node[bag, minimum width=2cm] {t=1 $E[u(c',l',\eta_{k,1})|K]$ }
child {node{$E[u(c',l',\eta_{k,1})|\eta_0]$}
edge from parent
node[above] {$\kappa = 0$}
}
child {node[bag, minimum width=4cm] {$E[u(c',l',\eta_k)|K]$}
edge from parent
node[above] {$\kappa = 1$}
}
edge from parent
node[above] {$\kappa = 1$}
}

;
%\end{scope}

\end{tikzpicture}
\end{document}


But I hope to have a neater image, with more spacing in the labels of the child nodes as well. Any help is highly appreciated :)

• Welcome! Please can you make your code compilable? Off-topic: \tikzstyle is deprecated. Consider using \tikzset instead. Are you open to solutions using other packages?
– cfr
Commented Mar 16 at 1:52
• The base TikZ tree methods are really not ideal for most trees. For this sort of of tree either forest (general purpose) or istgame (designed specifically for game trees) is a much better choice. Commented Mar 16 at 15:52
• @cfr Yes I am fully open to other packages. As long as they allow me to work with probability and dynamic diagrams such as this one. I also updated the code to make it compilable.
– MarV
Commented Mar 16 at 18:38

maybe like this ? I replaced deprecated \tikzstyle.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{trees, arrows}
\begin{document}
\pagestyle{empty}

% tikzset instead of deprecated tikzstyle
\tikzset{
level 1/.style={level distance=4cm, sibling distance=6cm},
level 2/.style={level distance=4cm, sibling distance=3cm},
level 3/.style={level distance=3cm, sibling distance=2cm},
bag/.style={text width=8em, text centered}
}

\begin{tikzpicture}[grow=right,->,>=angle 60, sloped]
\node[bag, minimum width=2cm] {Testing Consumer $t=0$}
child {
node[bag, minimum width=2cm] {$E[u(c,l,\eta_1)|K]$}
edge from parent
node[above] {$\kappa = 0$}
}
child {node[bag, minimum width=2cm] {t=1 $E[u(c',l',\eta_{k,1})|K]$ }
child {node{$E[u(c',l',\eta_{k,1})|\eta_0]$}
edge from parent
node[above] {$\kappa = 0$}
}
child {node[bag, minimum width=2cm] {$E[u(c',l',\eta_k)|K]$}
edge from parent
node[above] {$\kappa = 1$}
}
edge from parent
node[above] {$\kappa = 1$}
};
\end{tikzpicture}
\end{document}

[1]:


https://i.sstatic.net/n0X1o.png

I would use forest, though istgame would also be a great choice. Possibly something like this?

\begin{forest}
align=center,


This is just for the root node. Children's alignment is handled below.

  for tree={%
grow'=east,
calign=midpoint,
parent anchor=children,
child anchor=parent,
anchor=child anchor,


Basically, align parents with the point halfway between their children, align children on the same level according to their child anchors and set the growth direction and anchors for the tree. This also ensures the branching edges start from a common point, which seems to be usual for probability/game trees.

    l sep'+=25pt,


Increase the distance between levels i.e. between each parent and its children. This may not be sufficient.

    s sep'+=20pt,


Increase the minimum separation between siblings.

    edge+={-Stealth},


Add an arrow to all edges.

    font=\sffamily,


Use sans for text. This is usually a good idea for diagrams, but you may or may not want it in this case given the high ratio of maths to text.

  },
for descendants={%


The following affects all descendants i.e. everything except the root.

    if n=1{edge label={node[above,midway,sloped,font=\scriptsize]{$\kappa=1$}}}{
edge label={node[below,midway,sloped,font=\scriptsize]{$\kappa=0$}},
},


If we're dealing with the first child, set the edge label to $\kappa=1$ and put it above the line; if the second, use \kappa=0 and set it below.  align=left,  Alignment for these nodes. Use center here if preferred.  },  Finally, the tree itself. I've broken lines manually because it is easier to get a neat result and there aren't many to do.  [{Testing\\Consumer\\t=0$} [{t=1\\$E[u(c',l',\eta_{k,1})|K]$} [{$E[u(c',l',\eta_k)|K]$} ] [{$E[u(c',l',\eta_{k,1})|\eta_0]$} ] ] [{$E[u(c,l,\eta_1)|K]$} ] ] \end{forest}  Be aware that I cannot currently produce images of output which are worth squat due to an Okular/KDE bug. So my output looks fine in the PDF, but sucks when I post it here. I'm not certain what the t=0 etc. designate. If these apply to all nodes on a level, I'd put these markers either above or below the tree instead. For example, you could add something like  before typesetting nodes={% tikz+={\coordinate (c) at ([yshift=5pt]current bounding box.north);}, tempcounta/.max={level}{tree}, for nodewalk={% do while={>OR<{level}{tempcounta}}{fake=next node}, current and ancestors }{% tikz+/.process={Ow{level}{% \node [anchor=base] at ( |- c) {$t=#1}; } }, }, },  to the preamble to create labels for the levels at the top. Complete code: \documentclass[tikz]{standalone} % ateb: https://tex.stackexchange.com/a/713428/ addaswyd o gwestiwn MarV: https://tex.stackexchange.com/q/713292/ \usepackage{forest} \usetikzlibrary{arrows.meta} \begin{document} \begin{forest} align=center, for tree={% grow'=east, calign=midpoint, parent anchor=children, child anchor=parent, anchor=child anchor, l sep'+=25pt, s sep'+=20pt, edge+={-Stealth}, font=\sffamily, }, for descendants={% if n=1{edge label={node[above,midway,sloped,font=\scriptsize]{\kappa=1$}}}{ edge label={node[below,midway,sloped,font=\scriptsize]{$\kappa=0}}, }, align=left, }, [{Testing\\Consumer\\t=0$} [{t=1\\$E[u(c',l',\eta_{k,1})|K]$} [{$E[u(c',l',\eta_k)|K]$} ] [{$E[u(c',l',\eta_{k,1})|\eta_0]$} ] ] [{$E[u(c,l,\eta_1)|K]} ] ] \end{forest} \begin{forest} align=center, for tree={% grow'=east, calign=midpoint, parent anchor=children, child anchor=parent, anchor=child anchor, l sep'+=25pt, s sep'+=20pt, edge+={-Stealth}, font=\sffamily, }, for descendants={% if n=1{edge label={node[above,midway,sloped,font=\scriptsize]{\kappa=1$}}}{ edge label={node[below,midway,sloped,font=\scriptsize]{$\kappa=0}}, }, align=left, }, before typesetting nodes={% tikz+={\coordinate (c) at ([yshift=5pt]current bounding box.north);}, tempcounta/.max={level}{tree}, for nodewalk={% do while={>OR<{level}{tempcounta}}{fake=next node}, current and ancestors }{% tikz+/.process={Ow{level}{% \node [anchor=base] at ( |- c) {t=#1$}; } }, }, }, [{Testing\\Consumer} [{$E[u(c',l',\eta_{k,1})|K]$} [{$E[u(c',l',\eta_k)|K]$} ] [{$E[u(c',l',\eta_{k,1})|\eta_0]$} ] ] [{$E[u(c,l,\eta_1)|K]\$}
]
]
\end{forest}

\end{document}