画一个指向树的箭头

画一个指向树的箭头

是否可以在树中绘制指向方括号的箭头?但是,如何在箭头上方和下方书写?这是我的代码。它遵循我心中的目标形象。

\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}

在此处输入图片描述

答案1

也许是这样的?

\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}

在此处输入图片描述


您还可以执行以下操作:

\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}

在此处输入图片描述


更新:

使用\forestset,还可以创建自定义边,使用方式如下:

\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}

在此处输入图片描述

fork sep您可以使用和选项更改距离l

在此处输入图片描述

相关内容