答案1
有一个很好的例子概率树在TeXample.net.这也许能帮助你入门。
答案2
我觉得这是一个forest
解决方案,不过如果你搜索的话,你会发现更好的版本。这和你链接的图片不完全一样,因为我觉得那张图相当丑。当然,如果你喜欢更丑的版本,你可以调整它。
Forest 2.1.5 更新(2017-07-14)
肯定有更简单的方法,Sašo?
\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest,nicefrac}
\usetikzlibrary{positioning}
\begin{document}
\newcounter{forestalias}
\forestset{
left marble colour/.store in=\leftmarblecolour,
right marble colour/.store in=\rightmarblecolour,
left marble colour=black,
right marble colour=red,
declare count register={marble count},
marble count'=0,
declare toks={marble}{black},
declare keylist={marbles}{},
define long step={more marbles}{}{%
filter={root,tree}{>O+tt=!{marbles}{}}},
declare keylist={make more marbles}{},
probability tree/.style={
for tree={
grow=0,
s sep'+=2.5mm,
delay={
edge label/.process={
OO_=?_ w2 {content} {n}{1}{below}{above} {node [midway, ##2, font=\scriptsize, text=black] {##1}}
},
content=,
},
},
for descendants={
if n=1{marble=\leftmarblecolour}{marble=\rightmarblecolour},
},
before typesetting nodes={
for tree={
edge+/.option=marble,
make marble=black,
},
where n children=0{
tempkeylista'=,
for nodewalk={
c,
while={>O_> {level}{1}}{u}
}{
spare marble/.option=marble,
tempkeylista/.process={Rw{marble count}{m##1}},
},
marbles/.register=tempkeylista,
}{},
},
make more marbles={
for more marbles={
tempcountc'=0,
split option={marbles}{,}{make descendant marble},
}
},
before drawing tree={
where n children=0{
TeX={\stepcounter{forestalias}},
alias={n\theforestalias}
}{}
}
},
make descendant marble/.style={
tempcountc'+=1,
for nodewalk={L}{
if={>O+tt={name}{#1}}{}{
append/.expanded=#1, do dynamics
}
}
},
make marble/.style={circle, minimum width=2.5pt, fill=#1, draw=#1, inner sep=0pt},
spare marble/.style={
marble count'+=1,
append/.expanded={[, make marble=#1, name=m\foresteregister{marble count}, grow=0, no edge, l'+=-10mm]},
},
}
\begin{forest}(%
stages={% customised definition of stages
for root'={% nothing is removed from the standard forest definition - we only change it by adding to it
process keylist register=default preamble,
process keylist register=preamble,
},
process keylist=given options,
process keylist=before typesetting nodes,
% addition here
process keylist=make more marbles,
typeset nodes stage,
process keylist=before packing,
pack stage,
process keylist=before computing xy,
compute xy stage,
process keylist=before drawing tree,
draw tree stage,
},
)%
probability tree,
left marble colour=blue,
[, name=t0 [\nicefrac{3}{5}[\nicefrac{5}{9}][\nicefrac{4}{9}]][\nicefrac{2}{5}, name=t1 [\nicefrac{2}{3}][\nicefrac{1}{3}, name=t2]]]
\foreach \i/\j/\k/\l [count=\n] in {3/5/5/9,3/5/4/9,2/5/2/3,2/5/1/3}
\node (l\n) at (n\n) [label={[label distance=2.5mm, inner sep=0pt, , font=\scriptsize]right:$\frac{\i}{\j}.\frac{\k}{\l}$}] {};
\coordinate [above=7.5mm of t2] (m1);
\path [draw, fill, font=\scriptsize\itshape] (m1 -| t0) circle (1pt) -- (m1 -| t1) node [midway, below] {first} circle (1pt) -- (m1 -| t2) node [midway, below] {second} circle (1pt) -- ([xshift=2.5mm]m1 -| n3) node [midway, below] {result} circle (1pt) -- ++(7.5mm,0) node [midway, below] {\phantom{l}p\phantom{l}} circle (1pt);
\end{forest}
\end{document}
请注意,我假设无需重新定义 即可完成此操作stages
。我只是发现使用它来试验排序更容易。与原始代码不同,这依赖于do dynamics
,它是实验性的。但是,它确实产生了类似的结果:
早期 Forest 的原始答案(v. 1.something?)
\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest,nicefrac}
\usetikzlibrary{positioning}
\begin{document}
\newcounter{forestalias}
\forestset{
left marble colour/.store in=\leftmarblecolour,
right marble colour/.store in=\rightmarblecolour,
left marble colour=black,
right marble colour=red,
probability tree/.style={
for tree={
grow=0,
marble=black,
s sep+=2.5mm,
if n=1{
before typesetting nodes={
edge label/.wrap pgfmath arg={node [midway, below, font=\scriptsize, text=black] {##1}}{content()},
},
}{
before typesetting nodes={
edge label/.wrap pgfmath arg={node [midway, above, font=\scriptsize, text=black] {##1}}{content()},
},
},
before packing={
content={},
typeset node,
},
},
for descendants={
if n=1{make descendant marble=\leftmarblecolour}{make descendant marble=\rightmarblecolour},
},
before drawing tree={
for tree={
if n children=0{
TeX={\stepcounter{forestalias}},
alias={n\theforestalias}
}{}
}
}
},
marble/.style={circle, minimum width=2.5pt, fill=#1, draw=#1, inner sep=0pt},
make descendant marble/.style={
edge=#1,
delay n={int(level())}{
for descendants={
if n children=0{
append={[, grow=0, marble=#1, no edge, l+=-10mm]}
}{},
},
},
},
}
\begin{forest}
probability tree,
left marble colour=blue
[, name=t0 [\nicefrac{3}{5}[\nicefrac{5}{9}][\nicefrac{4}{9}]][\nicefrac{2}{5}, name=t1 [\nicefrac{2}{3}][\nicefrac{1}{3}, name=t2]]]
\foreach \i/\j/\k/\l [count=\n] in {3/5/5/9,3/5/4/9,2/5/2/3,2/5/1/3}
\node (l\n) at (n\n) [label={[label distance=2.5mm, inner sep=0pt, , font=\scriptsize]right:$\frac{\i}{\j}.\frac{\k}{\l}$}] {};
\coordinate [above=7.5mm of t2] (m1);
\path [draw, fill, font=\scriptsize\itshape] (m1 -| t0) circle (1pt) -- (m1 -| t1) node [midway, below] {first} circle (1pt) -- (m1 -| t2) node [midway, below] {second} circle (1pt) -- ([xshift=2.5mm]m1 -| n3) node [midway, below] {result} circle (1pt) -- ++(7.5mm,0) node [midway, below] {\phantom{l}p\phantom{l}} circle (1pt);
\end{forest}
\end{document}