3

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}

enter image description here

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

1 Answer 1

6

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}

enter image description here


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}

enter image description here


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}

enter image description here

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

enter image description here

7
  • 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.
    – JJSilva
    Jul 19 at 22:08
  • +1 for imaginative second solution!
    – Zarko
    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.).
    – JJSilva
    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"?
    – JJSilva
    Jul 20 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.