# Draw an arrow pointing to a tree

Is it possible to draw an arrow pointing to a square bracket in a tree? Still, how to write above and below the arrow? This is the code I have. It follow the image of the target I have in mind.

\documentclass{article}
\usepackage{tikz}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
for tree={forked edge, anchor=center,
}
[,phantom,s sep=1cm
[a b, for tree={grow'=east}
[x]
[y]
]
]
\end{forest}

\end{document}


• Do you need to use the forest package? Jul 19 at 20:30

Maybe something like this?

\documentclass{article}
\usepackage[edges]{forest}

\begin{document}

\begin{forest}
for tree={forked edge, fork sep=0pt, anchor=center
}
[{}, for tree={grow'=east}
[x]
[y]
]
\draw[<-] (.parent anchor) -- ++(-.5,0) node[midway, above] {a} node[midway, below] {b};
\end{forest}

\end{document}


You can also do:

\begin{forest}
for tree={forked edge, fork sep=0pt, anchor=center, child anchor=west
}
[{}, for tree={grow'=east}
[x
[]
[]
]
[y]
]
\draw[<-] (.parent anchor) -- ++(-.5,0) node[midway, above] {a} node[midway, below] {b};
\draw[<-] (!1.child anchor) -- ++(-.5,0) node[midway, above] {a} node[midway, below] {b};
\end{forest}


Update:

Using \forestset, it is also possible to create a custom edge which you could use like follows:

\documentclass{article}
\usepackage[edges]{forest}

\forestset{
arrow/.style 2 args={
edge path={
\noexpand\path[\forestoption{edge},->]
(!u.parent anchor) -- ++(\forestoption{fork sep},0)
\forestoption{edge label} node[midway,above]{#1} node[midway,below]{#2};
\noexpand\path[\forestoption{edge}]
([xshift=\forestoption{fork sep}]!u.parent anchor) |- (.child anchor)
\forestoption{edge label};
}
}
}

\begin{document}

\begin{forest}
for tree={forked edge, fork sep=.5cm, anchor=center, child anchor=west
}
[{}, for tree={grow'=east}
[x, arrow={a}{b}
[z, arrow={c}{d}]
[]
]
[y]
]
\end{forest}

\begin{forest}
for tree={forked edge, l=2cm, fork sep=1.5cm, anchor=center, child anchor=west
}
[{}, for tree={grow'=east}
[x, arrow={longer}{words}
[z, arrow={other}{stuff}]
[]
]
[y]
]
\end{forest}

\end{document}


You can change the distances using the fork sep and l options:

• I read your answer before editing. This one is way more clean. I use forest package because it's good when I want to build trees with lots of nodes. I still have a question: what to do if I want to add an arrow to another tree that has "x" (or "y", it doesn't matter) as a parent? I tried naming the node (like "name=f2"), but the result is not the one with the arrow touching square bracket. Jul 19 at 22:08
• +1 for imaginative second solution! Jul 20 at 0:50
• @Jasper Habicht, the last code, using \forestset, is perfect. It is exactly what I had in mind. Thanks a bunch. However, some problems appear when we write words (like, for example, "animal", "birds", "mammal" etc.) instead of letters ("a", "b", "x", "y", etc.). Jul 20 at 1:48
• That’s why I added a hint about how to scale this. Jul 20 at 5:24
• What about words instead of the letters "x", "y" and "z"? Jul 20 at 15:02