![我怎样才能在这个树形图的分支上放置一个空框?](https://linux22.com/image/377137/%E6%88%91%E6%80%8E%E6%A0%B7%E6%89%8D%E8%83%BD%E5%9C%A8%E8%BF%99%E4%B8%AA%E6%A0%91%E5%BD%A2%E5%9B%BE%E7%9A%84%E5%88%86%E6%94%AF%E4%B8%8A%E6%94%BE%E7%BD%AE%E4%B8%80%E4%B8%AA%E7%A9%BA%E6%A1%86%EF%BC%9F.png)
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=3.5cm, sibling distance=3.5cm]
\tikzstyle{level 2}=[level distance=3.5cm, sibling distance=2cm]
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=4em, text centered]
\tikzstyle{end} = [circle, minimum width=3pt,fill, inner sep=0pt]
% 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] {Bag 1 $4W, 3B$}
child {
node[bag] {Bag 2 $4W, 5B$}
child {
node[end, label=right:
{$P(W_1\cap W_2)=\frac{4}{7}\cdot\frac{4}{9}$}] {}
edge from parent
node[above] {$W$}
node[below] {$\frac{4}{9}$}
}
child {
node[end, label=right:
{$P(W_1\cap B_2)=\frac{4}{7}\cdot\frac{5}{9}$}] {}
edge from parent
node[above] {$B$}
node[below] {$\frac{5}{9}$}
}
edge from parent
node[above] {$W$}
node[below] {$\frac{4}{7}$}
}
child {
node[bag] {Bag 2 $3W, 6B$}
child {
node[end, label=right:
{$P(B_1\cap W_2)=\frac{3}{7}\cdot\frac{3}{9}$}] {}
edge from parent
node[above] {$B$}
node[below] {$\frac{3}{9}$}
}
child {
node[end, label=right:
{$P(B_1\cap B_2)=\frac{3}{7}\cdot\frac{6}{9}$}] {}
edge from parent
node[above] {$W$}
node[below] {$\frac{6}{9}$}
}
edge from parent
node[above] {$B$}
node[below] {$\frac{3}{7}$}
};
\end{tikzpicture}
\end{document}
附件中的 MWE 取自 tikz 示例网站。我想用一个方框替换每个分支上的概率。提前致谢
答案1
改编:
- 我定义了一个命令
\emptybox
来在树叶上的文本内(等式符号后)创建框。 ebox
我使用以下方式定义了一种风格:ebox/.style={below=1mm, minimum width=10mm, minimum height=6mm, draw}
。- 使用
tikzset
代替tikzstyle
(见应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?)。 - 我将
B
其与W
顶部分支进行了交换。
代码:
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\pagestyle{empty}
\tikzset{
% Set the overall layout of the tree
level 1/.style={level distance=3.5cm, sibling distance=3.5cm},
level 2/.style={level distance=3.5cm, sibling distance=2cm},
% Define styles for bags and leafs
bag/.style={text width=4em, text centered},
end/.style={circle, minimum width=3pt,fill, inner sep=0pt}
}
\newcommand{\emptybox}{\fbox{\parbox[c][8mm]{20mm}{\ }}}
% 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,
ebox/.style={below=1mm, minimum width=10mm, minimum height=6mm, draw}
]
\node[bag] {Bag 1 $4W, 3B$}
child {
node[bag] {Bag 2 $4W, 5B$}
child {
node[end, label=right:
{$P(W_1\cap W_2)=$ \emptybox}] {}
edge from parent
node[above] {$W$}
node[ebox] {}
}
child {
node[end, label=right:
{$P(W_1\cap B_2)=$ \emptybox}] {}
edge from parent
node[above] {$B$}
node[ebox] {}
}
edge from parent
node[above] {$W$}
node[ebox] {}
}
child {
node[bag] {Bag 2 $3W, 6B$}
child {
node[end, label=right:
{$P(B_1\cap W_2)=$ \emptybox}] {} %
edge from parent
node[above] {$W$}
node[ebox] {}
}
child {
node[end, label=right:
{$P(B_1\cap B_2)=$ \emptybox}] {}
edge from parent
node[above] {$B$}
node[ebox] {}
}
edge from parent
node[above] {$B$}
node[ebox] {}
};
\end{tikzpicture}
\end{document}
结果: