我想在 xytree 的 E 和 F 节点下方添加花括号和标签,如下所示:
到目前为止,我有树的代码,但还不知道如何插入花括号和标签。
\documentclass{article}
\usepackage{xytree}
\begin{document}
\xytree{
&&& \xynode{A} \xyconnect{2,-2}"_{Yes}" \xyconnect{2,2}"^{No}" &&& \\
&&&&&& \\
& \xynode{B} \xyconnect{2,-1}"_{Yes}" \xyconnect{2,1}"^{No}" &&&& \xynode{C} \xyconnect{2,-1}"_{Yes}" \xyconnect{2,1}"^{No}" \\
&&&&&& \\
\xynode{\begin{tabular}{c}D\end{tabular}} && \xynode{E} && \xynode{F} && \xynode{\begin{tabular}{c}G\end{tabular}} \\
}
\end{document}
答案1
一个选项是使用TikZ
及其tikzmark
(在括号的开始和结束位置放置标记)和decorations.pathmorphing
(将括号与文本放在一起)库(编译两到三次以使代码稳定):
\documentclass{article}
\usepackage{xytree}
\usepackage{tikz}
\usetikzlibrary{tikzmark,decorations.pathreplacing}
\begin{document}
\xytree{
&&& \xynode{A} \xyconnect{2,-2}"_{Yes}" \xyconnect{2,2}"^{No}" &&& \\
&&&&&& \\
& \xynode{B} \xyconnect{2,-1}"_{Yes}" \xyconnect{2,1}"^{No}" &&&& \xynode{C} \xyconnect{2,-1}"_{Yes}" \xyconnect{2,1}"^{No}" \\
&&&&&& \\
\xynode{\begin{tabular}{c}D\end{tabular}} && \xynode{\tikzmark{a}E} && \xynode{F\tikzmark{b}} && \xynode{\begin{tabular}{c}G\end{tabular}} \\
}
\begin{tikzpicture}[remember picture,overlay]
\draw[decorate,decoration={brace,mirror,raise=5pt}]
(pic cs:a) -- node[below=6pt] {label} (pic cs:b);
\end{tikzpicture}
\end{document}
也许你可能对forest
用于绘制树木的包;这个包非常强大且用途广泛,它基于 PGF/TikZ 构建,因此与 TikZ 代码的集成非常自然。以下是一些代码,展示了如何使用该forest
包获得相同的结果:
\documentclass{article}
\usepackage{forest}
\usetikzlibrary{decorations.pathreplacing}
\begin{document}
\begin{forest}
yes/.style={
edge label={node[midway,left,font=\footnotesize,xshift=-3pt]{Yes}}
},
no/.style={
edge label={node[midway,right,font=\footnotesize,xshift=3pt]{No}}
},
for tree={
parent anchor=south,
child anchor=north,
l sep=1cm,
s sep=1cm
}
[A
[B,yes
[D,yes]
[E,no,name=a]
]
[C,no
[F,yes,name=b]
[G,no]
]
]
\draw[decorate,decoration={brace,mirror}]
(a.south west) -- node[below] {label} (b.south east);
\end{forest}
\end{document}