![TikZ 森林 - 绘制子树](https://linux22.com/image/340826/TikZ%20%E6%A3%AE%E6%9E%97%20-%20%E7%BB%98%E5%88%B6%E5%AD%90%E6%A0%91.png)
我想在子树上添加一个矩形,如下图所示。该怎么做?
这是forest
代码。
\documentclass{article}
\usepackage{forest}
\useforestlibrary{linguistics}
\begin{document}
\pagestyle{empty}
\begin{forest}
for tree = {
sn edges,
grow'=0,
l=2.5cm,
s sep=0.2cm,
anchor=west,
child anchor=west}
[
[O
[R
[T]
]
[T
[R]
]
]
[R
[O
[T]
]
[T
[O]
]
]
[T
[O
[R]
]
[R
[O]
]
]
]
\end{forest}
\end{document}
答案1
您可以在中为节点分配名称forest
,因此您可以将其与 Tikzfit
库一起使用在您选择的节点周围绘制一个节点。
如果您不想使用节点,您也可以这样做(不需要任何库):
\draw[red,thick] (bottomright.south east) rectangle (left.west|-topright.north);
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{forest}
\useforestlibrary{linguistics}
\usetikzlibrary{fit}
\begin{document}
\begin{forest}
for tree = {
sn edges,
grow'=0,
l=2.5cm,
s sep=0.2cm,
anchor=west,
child anchor=west}
[
[O,name=left
[R
[T,name=topright]
]
[T
[R,name=bottomright]
]
]
[R
[O
[T]
]
[T
[O]
]
]
[T
[O
[R]
]
[R
[O]
]
]
]
\node[draw=red,thick,fit=(left)(topright)(bottomright)] {};
\end{forest}
\end{document}