我正在使用 绘制一棵树tikz-qtree
,想要绘制backgrounds
两组节点:左边的组由 组成l, l1, l2
,右边的组由 组成r, rl, l3, l4, l5
。但是,如下图所示,节点c
也在右侧背景矩形中。
如何绘制仅由 组成的背景矩形r, rl, l3, l4, l5
?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{backgrounds, fit, shapes}
\begin{document}
\begin{tikzpicture}[level distance = 25pt, sibling distance = 15pt, edge from parent/.style= {
draw, edge from parent path = {(\tikzparentnode) -- (\tikzchildnode)}}] \tikzset{every tree node/.style = {align = center, circle, draw, fill = red!40},
leaf/.style = {fill = teal!40}}
\Tree [.\node[](c){$c$};
[.\node[](l){$l$};
[.\node[leaf](l1){$l_1$}; ]
[.\node[leaf](l2){$l_2$}; ]
]
[.\node[](r){$r$};
[.\node[](rl){$rl$};
[.\node[leaf](l3){$l_3$}; ]
[.\node[leaf](l4){$l_4$}; ]
]
[.\node[leaf](l5){$l_5$}; ]
]
]
\node () [draw, dashed, cyan, rounded corners, fit = (l) (l1) (l2)] {};
\node () [draw, dashed, cyan, rounded corners, fit = (r) (rl) (l3) (l4) (l5)] {};
\end{tikzpicture}
\end{document}
答案1
你只需要将节点命名为 以外的其他名称r
。下面,我使用 ,3r
没有什么特别的原因。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{backgrounds, fit, shapes}
\begin{document}
\begin{tikzpicture}[level distance = 25pt, sibling distance = 15pt, edge from parent/.style= {
draw, edge from parent path = {(\tikzparentnode) -- (\tikzchildnode)}}] \tikzset{every tree node/.style = {align = center, circle, draw, fill = red!40},
leaf/.style = {fill = teal!40}}
\Tree [.\node[](c){$c$};
[.\node[](l){$l$};
[.\node[leaf](l1){$l_1$}; ]
[.\node[leaf](l2){$l_2$}; ]
]
[.\node[](3r){$r$};
[.\node[](rl){$rl$};
[.\node[leaf](l3){$l_3$}; ]
[.\node[leaf](l4){$l_4$}; ]
]
[.\node[leaf](l5){$l_5$}; ]
]
]
\node () [draw, dashed, cyan, rounded corners, fit = (l) (l1) (l2)] {};
\node () [draw, dashed, cyan, rounded corners, fit = (3r) (rl) (l3) (l4) (l5)] {};
\end{tikzpicture}
\end{document}
但是,我也不知道为什么。话虽如此,我猜这(r)
是树的根,因为你可以使用这个节点而不需要命名任何东西r
。例如,
\begin{tikzpicture}[level distance = 25pt, sibling distance = 15pt, edge from parent/.style= {
draw, edge from parent path = {(\tikzparentnode) -- (\tikzchildnode)}}] \tikzset{every tree node/.style = {align = center, circle, draw, fill = red!40},
leaf/.style = {fill = teal!40}}
\Tree [.\node[](c){$c$};
[.\node[](l){$l$};
[.\node[leaf](l1){$l_1$}; ]
[.\node[leaf](l2){$l_2$}; ]
]
[.\node[](3r){$r$};
[.\node[](rl){$rl$};
[.\node[leaf](l3){$l_3$}; ]
[.\node[leaf](l4){$l_4$}; ]
]
[.\node[leaf](l5){$l_5$}; ]
]
]
\node () [draw, dashed, cyan, rounded corners, fit = (l) (l1) (l2)] {};
\node () [draw, dashed, cyan, rounded corners, fit = (3r) (rl) (l3) (l4) (l5)] {};
\node () [draw, dashed, cyan, rounded corners, fit = (r) (rl) (l3) (l4) (l5)] {};
\end{tikzpicture}
产生完全合理的(即使出乎意料)输出,而不是预期的错误:
这强烈建议r
命名根节点,或者可能是树根处的坐标或不可见节点。
答案2
我不确定为什么会发生这种情况,但作为一种解决方法,您可以添加相对于(r)
要使用的明确坐标:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{backgrounds, fit, shapes,calc}
\begin{document}
\begin{tikzpicture}[level distance = 25pt, sibling distance = 15pt, edge from parent/.style= {
draw, edge from parent path = {(\tikzparentnode) -- (\tikzchildnode)}}] \tikzset{every tree node/.style = {align = center, circle, draw, fill = red!40},
leaf/.style = {fill = teal!40}}
\Tree [.\node[](c){$c$};
[.\node[](l){$l$};
[.\node[leaf](l1){$l_1$}; ]
[.\node[leaf](l2){$l_2$}; ]
]
[.\node[](r){$r$};
[.\node[](rl){$rl$};
[.\node[leaf](l3){$l_3$}; ]
[.\node[leaf](l4){$l_4$}; ]
]
[.\node[leaf](l5){$l_5$}; ]
]
]
\coordinate (rr) at ($(r)-(0,.6)$);
\node () [draw, dashed, cyan, rounded corners, fit = (l) (l1) (l2)] {};
\node () [draw, dashed, cyan, rounded corners, fit = (rr) (rl) (l3) (l4) (l5) ] {};
\end{tikzpicture}
\end{document}