我的 Latex 文档中有一棵 Tikz 树,如下所示:
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=4.5cm, sibling distance=6.5cm]
\tikzstyle{level 2}=[level distance=4cm, sibling distance=2.5cm]
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=8em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
\tikzstyle{atom} = [draw=black,thick]
% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {$S_0 = 1$}
child {
node[bag] {$S_1 = 0.90$}
child {
node[end, label=right:
{$S_2 = 0.810$}] {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 0.900$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 0.918$}] {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_3$}
}
child {
node[bag] {$S_1 = 1.00$}
child {
node[end, label=right:
{$S_2 = 0.900$}] {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 1.000$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 1.020$}] {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_2$}
}
child {
node[bag] {$S_1 = 1.02$}
child {
node[end, label=right:
{$S_2 = 0.918$}] {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 1.020$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 1.0404$}] {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_1$}
};
\end{tikzpicture}
结果:
我想在三个第一级子节点的末端绘制圆角矩形。因此每个矩形中将有三个节点,顶部三个在第一个,中间三个在第二个,底部三个在最后一个。我还希望标签在每个矩形的右侧垂直居中。最终结果应该是这样的:
由于我对 Tikz 完全陌生,我不知道如何实现这个结果。救命!
答案1
这个想法(作为彼得·格里尔在评论中建议)是命名必要的节点并使用fit
库;使用了辅助节点;一个简单的循环绘制三个框架并放置标签;我也从过时的\tikzstyle
改为\tikzset
:
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,fit}
\begin{document}
% Set the overall layout of the tree
\tikzset{
level 1/.style={level distance=4.5cm, sibling distance=6.5cm},
level 2/.style={level distance=4cm, sibling distance=2.5cm}
}
% Define styles for bags and leafs
\tikzset{
bag/.style = {text width=8em, text centered},
end/.style = {circle, minimum width=3pt,fill, inner sep=0pt},
atom/.style = {draw=black,thick}
}
% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {$S_0 = 1$}
child {
node[bag] {$S_1 = 0.90$}
child {
node[end, label=right:
{$S_2 = 0.810$}] (b3) {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 0.900$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 0.918$}] (t3) {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_3$}
}
child {
node[bag] {$S_1 = 1.00$}
child {
node[end, label=right:
{$S_2 = 0.900$}] (b2) {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 1.000$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 1.020$}] (t2) {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_2$}
}
child {
node[bag] {$S_1 = 1.02$}
child {
node[end, label=right:
{$S_2 = 0.918$}] (b1) {}
edge from parent
node[above] {$Y_2 = y_3$}
}
child {
node[end, label=right:
{$S_2 = 1.020$}] {}
edge from parent
node[above] {$Y_2 = y_2$}
}
child {
node[end, label=right:
{$S_2 = 1.0404$}] (t1) {}
edge from parent
node[above] {$Y_2 = y_1$}
}
edge from parent
node[above] {$Y_1 = y_1$}
};
\begin{scope}[label distance=6mm,]
\foreach \i/\label in {1/Label 1,2/Label 2,3/Label 3}
{
\coordinate (aux\i) at ([xshift=2cm]t\i);
\node[inner sep=6pt,rounded corners=6pt,draw,red,fit={(t\i) (b\i) (aux\i)},label=right:{\color{red}\label}] {};
}
\end{scope}
\end{tikzpicture}
\end{document}