我有以下代码:
\begin{tikzpicture}[grow=right, sloped]
\tikzstyle{level 1}=[level distance=1.3cm, sibling distance=1cm]
\tikzstyle{level 2}=[level distance=1.2cm, sibling distance=1cm]
\node[bag] {$S_i$}
child[grow=left] {
node[bag] {$S_{i+1}^{(1)}$}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$W$}
}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$B$}
}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$B$}
}
edge from parent[-latex]
node[above] {$W$}
}
child[grow=right] {
node[bag] {$A$}
child {
node[bag] {$ \dots S_{i+1}^{(k_1)}$}
edge from parent[-latex]
node[above] {$w$}
}
child {
node[bag] {$S_{i+1}^{(2)}$}
child {
node[bag] {$S_{i+2}^{(2)(k_{\dots})}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(2)(2)}$}
edge from parent[-latex]
node[above] {u}
}
edge from parent[-latex]
node[above] {$w$}
}
child {
node[bag] {$S_{i+1}^{(1)}$}
child {
node[bag] {$S_{i+2}^{(1)(k_{\dots})}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(1)(2)}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(1)(1)}$}
edge from parent[-latex]
node[above] {u}
}
edge from parent[-latex]
node[above] {$w$}
}
edge from parent[-latex]
node[above] {$B$}
};
\end{tikzpicture}
在序言中
\tikzstyle{bag} = [text width=1.5em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
我的问题是右边的分支产生碰撞,并且树不会自动扩展。
基本上来说节点确实重叠。我该如何避免这种情况?
谢谢
答案1
您只需为适当的级别赋予适当的值sibling distance
;在您的示例中,有问题的级别是第二和第三个:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[grow=right, sloped,bag/.style={draw},end/.style={draw}]
\tikzstyle{level 2}=[level distance=1.3cm, sibling distance=4cm]
\tikzstyle{level 3}=[level distance=2.2cm, sibling distance=1.5cm]
\node[bag] {$S_i$}
child[grow=left,level 2/.style={sibling distance=1.5cm}] {
node[bag] {$S_{i+1}^{(1)}$}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$W$}
}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$B$}
}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$B$}
}
edge from parent[-latex]
node[above] {$W$}
}
child[grow=right] {
node[bag] {$A$}
child {
node[bag] {$ \dots S_{i+1}^{(k_1)}$}
edge from parent[-latex]
node[above] {$w$}
}
child {
node[bag] {$S_{i+1}^{(2)}$}
child {
node[bag] {$S_{i+2}^{(2)(k_{\dots})}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(2)(2)}$}
edge from parent[-latex]
node[above] {u}
}
edge from parent[-latex]
node[above] {$w$}
}
child {
node[bag] {$S_{i+1}^{(1)}$}
child {
node[bag] {$S_{i+2}^{(1)(k_{\dots})}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(1)(2)}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(1)(1)}$}
edge from parent[-latex]
node[above] {u}
}
edge from parent[-latex]
node[above] {$w$}
}
edge from parent[-latex]
node[above] {$B$}
};
\end{tikzpicture}
\end{document}
或许你也会对edge from parent path
控制箭头连接节点的位置的选项感兴趣:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[grow=right, sloped,bag/.style={draw},end/.style={draw},
edge from parent path={(\tikzparentnode.east) -- (\tikzchildnode.west)}
]
\tikzstyle{level 2}=[level distance=2cm, sibling distance=4cm]
\tikzstyle{level 3}=[level distance=2.2cm, sibling distance=1.5cm]
\node[bag] {$S_i$}
child[grow=left,level 2/.style={sibling distance=1.5cm},edge from parent path={(\tikzparentnode.west) -- (\tikzchildnode.east)}] {
node[bag] {$S_{i+1}^{(1)}$}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$W$}
}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$B$}
}
child {
node[end, label=left:
{$S$}] {}
edge from parent[-latex]
node[above] {$B$}
}
edge from parent[-latex]
node[above] {$W$}
}
child[grow=right] {
node[bag] {$A$}
child {
node[bag] {$ \dots S_{i+1}^{(k_1)}$}
edge from parent[-latex]
node[above] {$w$}
}
child {
node[bag] {$S_{i+1}^{(2)}$}
child {
node[bag] {$S_{i+2}^{(2)(k_{\dots})}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(2)(2)}$}
edge from parent[-latex]
node[above] {u}
}
edge from parent[-latex]
node[above] {$w$}
}
child {
node[bag] {$S_{i+1}^{(1)}$}
child {
node[bag] {$S_{i+2}^{(1)(k_{\dots})}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(1)(2)}$}
edge from parent[-latex]
node[above] {u}
}
child {
node[bag] {$S_{i+2}^{(1)(1)}$}
edge from parent[-latex]
node[above] {u}
}
edge from parent[-latex]
node[above] {$w$}
}
edge from parent[-latex]
node[above] {$B$}
};
\end{tikzpicture}
\end{document}