我尝试重现这棵决策树但遇到了一些问题。
我就是这么做的
\documentclass{article}
\usepackage{tikz,forest}
\usetikzlibrary{arrows.meta}
\tikzset{% common styles of tikz pictures elements
treenode/.style = {shape=rectangle, rounded corners,
draw, align=center,
minimum height=2ex, text depth=0.25ex,
top color=white, bottom color=blue!20},
root/.style = {treenode, font=\Large\rmfamily, bottom color=red!30},
env/.style = {treenode, font=\ttfamily\normalsize},
}
\begin{document}
\begin{tikzpicture}[% local settings for tree
grow = right,
sibling distance = 6em,
level distance = 10em,
edge from parent/.style = {draw, -latex},
every node/.append style = {font=\footnotesize},
sloped
]
\node [root] {}
child { node [env] {\(M\)}
child { node [env] {\(T\)}
edge from parent node [below] {\(p(T|M) = \dots\)}}
child { node [env] {\(A\)} %%%confondu avec
edge from parent node [above] {\(p(\overline{T}|M) = \dots\)}}
edge from parent node [below] {\(p(M) = \dots\)}
}
child { node [env] {\(\overline{M}\)}
child { node [env] {\(B\)} %%%ceci
edge from parent node [below] {\(p(T|\overline{M}) = \dots\)}}
child { node [env] {\(\overline{T}\)}
edge from parent node [above] {\(p(\overline{T}|\overline{M}) = \dots\)}}
edge from parent node [above] {\(p(\overline{M}) = \dots\)}
};
\end{tikzpicture}
\end{document}
结果如下:
A 和 B 在同一个地方。
答案1
使用起来稍微容易一些forest
(您已加载但未使用)。您可以使用它forestset
来设置边缘标签的样式。
\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\tikzset{% common styles of tikz pictures elements
treenode/.style = {shape=rectangle, rounded corners,
draw, align=center,
minimum height=2ex, text depth=0.25ex,
top color=white, bottom color=blue!20},
root/.style = {treenode, font=\Large\rmfamily, bottom color=red!30},
env/.style = {treenode, font=\ttfamily\normalsize}
}
\forestset{
bedge/.style = {edge label={node[midway, sloped, font=\footnotesize, below]{$#1$}}},
aedge/.style = {edge label={node[midway, sloped, font=\footnotesize, above]{$#1$}}}
}
\begin{document}
\begin{forest}
for tree={
l sep=10em, s sep=6em,
edge={-latex},
math content,
grow'=0,
if level=0{root}{env}
}
[,
[\overline{M}, aedge={p(\overline{M})=\cdots}
[\overline{T}, aedge={p(\overline{T}|\overline{M})=\cdots}]
[A, bedge={p(T|\overline{M})=\cdots}]
]
[M, bedge={p(M)=\cdots}
[B, aedge={p(\overline{T}|M)=\cdots}]
[T, bedge={p(T|M)=\cdots}]
]
]
\end{forest}
\end{document}