我正在使用该tikz
包来绘制一棵树Latex
:
$h=i$
我对节点右侧的位置不满意
X,X_2,X_6 and X_12.
在下面的代码中,我使用了label=right
将其直接放在节点旁边的功能。我的目标是h=1,h=2,h=3,h=4
在一列中显示,并且所有元素都具有相同的水平位置(例如,所有元素都位于 h=3 以上)。使用 tikz 可以实现这一点吗?
代码:
\begin{tikzpicture}[level/.style={sibling distance=60mm/#1}]
\node [circle,draw,label=right:{\textit{h=0}}] (z){$X$}
child {node [circle,draw] (a) {$X_1$}
child {node [circle,draw] (c) {$X_3$}
child {node [rectangle,draw,label=below:1] (e) {$X_7$}}
child {node [circle,draw] (f) {$X_8$}
child {node [rectangle,draw,label=below:2] (g) {$X_{13}$}}
child {node [rectangle,draw,label=below:4] (h) {$X_{14}$}}
}
}
child {node [circle,draw] (d) {$X_4$}}
}
child {node [circle,draw,label=right:{\textit{h=1}}] (b) {$X_2$}
child {node [circle,draw] (i) {$X_5$}
child {node [circle,draw] (k) {$X_9$}
child {node [rectangle,draw,label=below:2] (o) {$X_{15}$}}
child {node [rectangle,draw,label=below:3] (p) {$X_{16}$}}
}
child {node [rectangle,draw,label=below:1] (l) {$X_{10}$}}
}
child {node [circle,draw,label=right:{\textit{h=2}}] (j) {$X_6$}
child {node [rectangle,draw, label=below:3] (m) {$X_{11}$}}
child {node [circle,draw,label=right:{\textit{h=3}}] (n) {$X_{12}$}
child {node [rectangle,draw, label=below:5] (q) {$X_{17}$}}
child {node [rectangle,draw, label=below:4] (r) {$X_{18}$}}
}
}
};
\path (a)--(b) node [midway] {Split 1};
\path (c)--(d) node [midway] {Split 2};
\path (i)--(j) node [midway] {Split 3};
\path (e)--(f) node [midway] {\tiny{Split 4}};
\path (k)--(l) node [midway] {\tiny{Split 5}};
\path (m)--(n) node [midway] {\tiny{Split 6}};
\end{tikzpicture}
答案1
像这样?
这很容易获得,只需将分离的节点与节点右侧对齐即可n
:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
node distance=1mm,
C/.style = {circle, draw, minimum size=1.5em},
R/.style = {rectangle, draw, minimum size=1.5em},
level/.style={sibling distance=60mm/#1}]
\node [C] (z){$X$}
child {node [C] (a) {$X_1$}
child {node [C] (c) {$X_3$}
child {node [R,label=below:1] (e) {$X_7$}}
child {node [C] (f) {$X_8$}
child {node [R,label=below:2] (g) {$X_{13}$}}
child {node [R,label=below:4] (h) {$X_{14}$}}
}
}
child {node [C] (d) {$X_4$}}
}
child {node [C] (b) {$X_2$}
child {node [C] (i) {$X_5$}
child {node [C] (k) {$X_9$}
child {node [R,label=below:2] (o) {$X_{15}$}}
child {node [R,label=below:3] (p) {$X_{16}$}}
}
child {node [R,label=below:1] (l) {$X_{10}$}}
}
child {node [C] (j) {$X_6$}
child {node [R, label=below:3] (m) {$X_{11}$}}
child {node [C] (n) {$X_{12}$}
child {node [R, label=below:5] (q) {$X_{17}$}}
child {node [R, label=below:4] (r) {$X_{18}$}}
}
}
};
\node[right=of z -| n.east] {$h=0$};
\node[right=of b -| n.east] {$h=1$};
\node[right=of j -| n.east] {$h=2$};
\node[right=of n.east] {$h=3$};
\path (a) -- node {Split 1} (b)
(c) -- node {Split 2} (d)
(i) -- node {Split 3} (j)
(e) -- node [font=\scriptsize] {Split 4} (f)
(k) -- node [font=\scriptsize] {Split 5} (l)
(m) -- node [font=\scriptsize] {Split 6} (n);
\end{tikzpicture}
\end{document}
我抓住机会,通过定义两种节点样式(C
圆形和R
矩形)使您的代码稍微短一些。节点定位使用 TikZ 库positioning
。