我在进行测试时将标签“p”和“1 - p”放在分支上:有没有更严格的方法?
我需要复制图像中的树,但我不确定如何完成。
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
\node {}[grow'=right,blue!80,
level 1/.style={sibling distance=50mm,level distance=25mm},
level 2/.style={sibling distance=25mm,level distance=25mm},
level 3/.style={sibling distance=15mm,level distance=25mm}]
child {node {S}
child {node {S}
child {node {S} edge from parent node[above,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
edge from parent node[below=4pt,pos=0.5] {$1-p$}}
edge from parent node[above=4pt,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
child {node {S} edge from parent node[above,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
edge from parent node[below=4pt,pos=0.5] {$1-p$}}
edge from parent node[below=6pt,pos=0.5] {$1-p$}}
edge from parent node[above=8pt,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
child {node {S}
child {node {S} edge from parent node[above,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
edge from parent node[below=6pt,pos=0.5] {$1-p$}}
edge from parent node[above,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
child {node {S}
edge from parent node[above,pos=0.5] {$p$}}
child {node {$\overline{\text{S}}$}
edge from parent node[below=6pt,pos=0.5] {$1-p$}}
edge from parent node[below=6pt,pos=0.5] {$1-p$}}
edge from parent node[below=8pt,pos=0.5] {$1-p$}};
\node at (25mm,60mm) { Épreuve 1};
\node at (50mm,60mm) { Épreuve 2};
\node at (75mm,60mm) { Épreuve 3};
\end{tikzpicture}
\end{document}
答案1
我会只使用“原始”tikz 而不是树来实现这一点。这样我就能更好地控制事物。我为您提供了一种简单的方法,可能需要进行大量调整(尺寸、位置等)。调整一切的一种方法可能是声明参数,尤其是在环境中\newcommand
。
\documentclass[tikz, border=2mm]{standalone}
\usepackage {ifthen}
\usetikzlibrary{calc}
\newcommand{\branch}[2] % vertex, height
{
\pgfmathsetmacro\ang{atan(#2*0.5/1.8)}; % angle of rotation (labels p and 1-p)
\node (top) at ($#1+(2,#2*0.5)$) {$S$};
\node (bot) at ($#1+(2,-#2*0.5)$) {$\overline{S}$};
\draw[thick,blue] (top) -- ($#1+(0.2,0)$) --(bot);
\node[rotate= \ang] at ($#1!0.5!(top)$) [above] {$p$};
\node[rotate=-\ang] at ($#1!0.5!(bot)$) [below] {$1-p$};
}
\newcommand{\bdots}[2] % vertex, height
{
\node (top) at ($#1+(2,#2*0.5)$) {};
\node (bot) at ($#1+(2,-#2*0.5)$) {};
\draw[thick,blue,dotted] (top) -- ($#1+(0.2,0)$) --(bot);
}
\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
%\draw[gray] (0,0) grid (11,9);
% 1st column
\branch{(0,3.5)}{4};
% 2nd column
\branch{(2,5.5)}{2};
\branch{(2,1.5)}{2};
% 3rd row
\branch{(4,6.5)}{1};
\branch{(4,4.5)}{1};
\branch{(4,2.5)}{1};
\branch{(4,0.5)}{1};
% 4th column (dotted lines)
\bdots {(6,5 )}{1};
\bdots {(6,2 )}{1};
% n-th column
\branch{(9,7 )}{1};
\branch{(9,4.5)}{1};
% dots
\foreach\y in{0.5,2,5,6.5}
{
\node at (8.5,\y) {$\cdots$};
}
\foreach\y in{3,5.5}
{
\node at (10,\y) {$\cdots$};
}
% labels
\foreach\x [count=\i] in{2,4,6,11}
{
\node at (\x,8.5) {\bf\sffamily Épreuve};
\node at (\x,8) {\ifthenelse{\equal{\i}{4}}{$n$}{$\i$}};
}
\end{tikzpicture}
\end{document}