# Tikz extra node before forest

I would like to combine a forest with some kind of variable initiation on top of the first node.

This is the code I currently have

\documentclass[]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\forestset{
default preamble={
before typesetting nodes={
!r.replace by={[, coordinate, append]}
},
where n children=0{
tier=word,
}{
where n children=0{
}{
diamond, aspect=3,
}
},
where level=1{}{
if n=1{
edge label={node[pos=.2, above] {yes}},
}{
edge label={node[pos=.2, above] {no}},
}
},
for tree={
edge+={thick, -Latex},%math content,
s sep'+=0.3cm,
%draw,
thick,
edge path'={ (!u) -| (.parent)},
}
}
}

\begin{document}

\begin{forest}
[{$a_i=a_{i-1}, (a_0=1)$}\\{$b_i=b_{i-1}, (b_0=1)$}, align=center, base=bottom
[{$a_i=1$},draw
[nothing,draw]
[{$t_i>3$},draw
[{$l_i > 10$},draw
[{$x_i \in A$},draw % und zwischen 3 und 50 Zeichen
[nothing,draw]
[{$z = 1$},draw
[nothing,draw]
[I am Lorde yayaya,draw]
]
]
[nothing,draw]
]
[{$Speeder_i=1$},draw]
]
]
]
\end{forest}

\end{document}


It looks like this

But it should look like this

• $Speeder_i$ is the same as $Spdr_ie^3$, i.e. the product of S, p, d, e, e and e and rᵢ. You maybe want to use \mathit{Speeder}_i. Sep 17 at 1:04

The upper no comes from your first option

before typesetting nodes={% I don't even know what that does …
!r.replace by={[, coordinate, append]}
},


Just removing it removes the no. (I don't know what it does …)

Your now upper most node (the one with the a_i and b_i equation) has two problems:

1. It uses the diamond shape.

We use

where n children<=1{}{diamond, aspect=3}


instead of where n children=0 because we only want the diamond shape when a node has 2 or more children. (I guess where n children >=2{diamond, aspect=3}{} should work, too.)

2. It connects to its child with the -| path you set via edge path'. The -| path operation doesn't work nicely with nodes that are vertically and too wide. This one even is exactly vertically.

We will use

where level> = 2{
edge path'={ (!u) -| (.parent)}
}{}


## Code

I've marked the changed lines with %%%!.

\documentclass[]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\forestset{
default preamble={
%    before typesetting nodes={%%%! I don't even know what that does …
%      !r.replace by={[, coordinate, append]}
%    },
where n children=0{
tier=word,
}{
where n children< = 1{%%%! changed from where n children=0
}{
diamond, aspect=3,
}
},
where level=1{}{
if n=1{
edge label={node[pos=.2, above] {yes}},
}{
edge label={node[pos=.2, above] {no}},
}
},
for tree={
edge+={thick, -Latex},%math content,
s sep'+=0.3cm,
%draw,
thick,
where level> = 2{%%%! added condition for edge path'
edge path'={ (!u) -| (.parent)}
}{},
}
}
}

\begin{document}
\begin{forest}
[{$a_i=a_{i-1}, (a_0=1)$}\\{$b_i=b_{i-1}, (b_0=1)$},
align=center,
yshift=1cm %%%! base=bottom removed (only one node on this level)
%%%! and yshift=1cm added (to make the arrow longer)
[{$a_i=1$}, draw
[nothing, draw]
[{$t_i>3$}, draw
[{$l_i > 10$}, draw
[{$x_i \in A$}, draw % und zwischen 3 und 50 Zeichen
[nothing, draw]
[{$z = 1$}, draw
[nothing, draw]
[I am Lorde yayaya, draw]
]
]
[nothing, draw]
]
[{$Speeder_i=1$}, draw]
]
]
]
\end{forest}
\end{document}


## Output

• If the Lorde node also contains only nothing then you will have the same problem with -| as with the root node because z = 1 node is wider than the distance between the centers of its children. Sep 17 at 1:07