# Customise Space between Nodes in Tree (using Forest)

I have the following tree, and I would like the central and the right branches to have a wider separation between them, to avoid confusion. I have been trying some things because I thought it would not be difficult, but I do not get what I need. See the screenshot below:

This is a MWE that should reproduce the error:

\documentclass[a4paper]{report}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{forest}
\usepackage{amsmath}
\usepackage{tikz}

\begin{document}
\begin{forest}
for tree={l sep=2em, s sep=0em, anchor=center},
for children={l sep=3em, s sep=1em, anchor=center, calign=fixed edge angles},
[\scriptsize{State}, circle, draw,
[{$-\vert\hat{\pi}_s-\pi_m\vert+\beta$ \\ $-\vert\hat{\pi}_t-\pi_m\vert$}, align=center, edge label={node[midway,fill=white,]{$s_{m}$}}]
[\scriptsize{Troika}, circle, draw, edge label={node[midway,fill=white,]{$s_{t}$}}, before computing xy={s=(s("!p")+s("!n"))/2},
[, name=0, coordinate,]
[\scriptsize{State}, name=P2, coordinate, l*=2, before computing xy={s=(s("!p")+s("!n"))/2}, circle, draw, edge label={node[midway,fill=white,]{$\lambda$}},
[{$\delta(-\vert\hat{\pi}_s-\pi_m\vert+\beta)$ \\ $\delta(-\vert\hat{\pi}_t-\pi_m\vert)$}, align=center, edge label={node[midway,fill=white,]{$s_m$}}]
[{$\delta(-\vert\hat{\pi}_s-\pi_t\vert+\alpha)$ \\ $\delta(-\vert\hat{\pi}_t-\pi_t\vert-\alpha)$}, align=center, edge label={node[midway,fill=white,]{$s_t$}}, before computing xy={s=(s("!p")+s("!n"))/2}]
[{$\delta(-\vert\hat{\pi}_s-\pi_d\vert)$ \\ $\delta(-\vert\hat{\pi}_t-\pi_d\vert)$}, align=center, text width=110pt, edge label={node[midway,fill=white,]{$s_d$}}]]
[, name=1, coordinate,]]
[{$(-\vert\hat{\pi}_s-\pi_d\vert)$ \\ $-\vert\hat{\pi}_t-\pi_d\vert$}, align=center, edge label={node[midway,fill=white,]{$s_{d}$}}]]]
\draw[densely dotted, bend right](0)to(P2);
\draw[densely dotted, bend right](P2)to(1);
\end{forest}
\end{document}


Thank you all!

• You have an extra square bracket on line 21 (but that doesn't answer your question). Maybe xshift=1.5em, on line 19 between align=center and textwidth..? That still isn't quite an answer though, as it pulls the left child along with it. – Jeffery Shivers Jun 25 '16 at 21:52
• Try adjusting s for the relevant nodes. You must do this just after packing, in before computing xy. – Sašo Živanović Jun 25 '16 at 23:57
• Try removing before computing xy={s=(s("!p")+s("!n"))/2}. – cfr Jun 26 '16 at 0:49
• Thank you all, guys! I directly tried cfr solution in his answer and it worked perfectly right since the beginning, so I have not investigated other approaches to the question. – Héctor Jun 26 '16 at 11:04

Is this the idea?

Here is essentially what I ended up doing:

• remove as much stuff affecting the spacing from the tree specification itself as possible;

• adjust the preamble to get something as close as possible to the desired result;

The thought behind this was that Forest typically does significantly better by default than your original code produced, so probably manual adjustments were screwing things up. It is often easier, with Forest, to strip back to the default until either you get the result you need or, failing that, you at least understand why you get the result you do. In the second case, you can then see what adjustments to make.

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
anchor=center,
if n=2{calign with current edge}{},
align=center
},
where level=1{%
s sep=1em
}{%
l sep=2em,
s sep=0em,
},
[{State}, circle, draw, font=\scriptsize
[{$-\vert\hat{\pi}_s-\pi_m\vert+\beta$ \\ $-\vert\hat{\pi}_t-\pi_m\vert$}, edge label={node[midway,fill=white,]{$s_{m}$}}
]
[{Troika}, circle, font=\scriptsize, draw, edge label={node[midway,fill=white,]{$s_{t}$}},
[, name=0,coordinate]
[{State}, font=\scriptsize, name=P2, before computing xy={l*=1.5}, circle, draw, edge label={node[midway,fill=white,]{$\lambda$}},
[{$\delta(-\vert\hat{\pi}_s-\pi_m\vert+\beta)$ \\ $\delta(-\vert\hat{\pi}_t-\pi_m\vert)$}, edge label={node[midway,fill=white,]{$s_m$}}]
[{$\delta(-\vert\hat{\pi}_s-\pi_t\vert+\alpha)$ \\ $\delta(-\vert\hat{\pi}_t-\pi_t\vert-\alpha)$}, edge label={node[midway,fill=white,]{$s_t$}},
]
[{$\delta(-\vert\hat{\pi}_s-\pi_d\vert)$ \\ $\delta(-\vert\hat{\pi}_t-\pi_d\vert)$}, edge label={node[midway,fill=white,]{$s_d$}}]
]
[, name=1, coordinate,]
]
[{$(-\vert\hat{\pi}_s-\pi_d\vert)$ \\ $-\vert\hat{\pi}_t-\pi_d\vert$}, edge label={node[midway,fill=white,]{$s_{d}$}}]
]
\draw[densely dotted, bend right](0)to(P2);
\draw[densely dotted, bend right](P2)to(1);
\end{forest}
\end{document}


However, the package author may hopefully follow up the comment on your answer with a (more) definitive solution.

• Your solution is perfect!! To be honest, I do not know what you mean with "a (more) definitive solution", since yours is awesome. Your code solves my initial problem and another problem I did not even know I had. Once I copied-paster your solution, the tree is properly centred in the page, whereas it was slightly shifted to the left with my initial code. – Héctor Jun 26 '16 at 11:00
• @Héctor Well, it is really that I did not do it the way the package author suggested it be done in comments and that makes me a bit anxious about it. However, changing s as suggested didn't seem to me to be necessary - it was enough to adjust l. Glad it helped! – cfr Jun 26 '16 at 12:54
• I wholeheartedly agree with the philosophy employed in this answer! – Sašo Živanović Jun 27 '16 at 12:26