我找到了这个代码,但我只想绘制一个非常简单的决策树。我试图修改代码,但我无法摆脱其他分支。我只想要 S,然后是“上”和“下”两个选项。
\documentclass[12pt]{article}
\usepackage{tikz,forest}
\tikzset{
treenode/.style = {shape=rectangle, rounded corners,
draw, align=center,
top color=white, bottom color=blue!20},
root/.style = {treenode, font=\Large, bottom color=red!30},
env/.style = {treenode, font=\ttfamily\normalsize},
dummy/.style = {circle,draw}
}
\begin{document}
\begin{tikzpicture}
[
grow = right,
sibling distance = 6em,
level distance = 10em,
edge from parent/.style = {draw, -latex},
every node/.style = {font=\footnotesize},
sloped
]
\node [root] {S}
child { node [env] {down}
edge from parent node [below] {$40$} }
child { node [env] {up}
edge from parent node [below] {$20$} }
child { node [dummy] {}
child { node [dummy] {}
child { node [env] {align\\flalign}
edge from parent node [below] {at relation sign?} }
child { node [env] {alignat}
edge from parent node [above] {at several}
node [below] {places?} }
child { node [env] {gather}
edge from parent node [above] {centered?} }
edge from parent node [below] {aligned?} }
child { node [env] {multline}
edge from parent node [above, align=center]
{first left,\\centered,}
node [below] {last right}}
edge from parent node [above] {$40$} };
\end{tikzpicture}
\end{document}
如果有人能解释一下这是如何工作的,我将不胜感激,因为我对 LaTeX 还很陌生。非常感谢!
答案1
tikz
您可以在软件包文档(它是tikz
安装的一部分,或者您可以在 CTAN 上找到它)中找到有关按软件包绘制树的最相关信息:钛钾Z 和 PGF 手册. 在部分21 让树木生长,第 333-342 页描述了基础知识,部分76 树图书馆 描述了trees
库的使用(定义了两个新的增长函数)。
无论如何,在 MWE(最小工作示例)中,下面是一些非常简单的树的示例代码。前两个是用纯 绘制的tikz
,最后一个使用forest
。
\documentclass[12pt]{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}
Using `tikz` tree growing right:
\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] {S} % root node, here tree start, after it are childrens
child { node [env] {down}
edge from parent node [above] {Yes}}
child { node [env] {up}
edge from parent node [above] {No}}
;
\end{tikzpicture}
Using `tikz` tree growing down (default direction):
\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] {S} % root node, here tree start, after it are childrens
child { node [env] {down}
edge from parent node [above] {Yes}}
child { node [env] {up}
edge from parent node [above] {No}}
;
\end{tikzpicture}
Using `forest` growing down:
\begin{forest}
for tree = {% settings for elemts in tree
% nodes
draw, rounded corners,
top color=white, bottom color=blue!20,
font = \ttfamily,
minimum height=2ex, text depth = 0.25ex,
anchor = north,
% edges
edge = {-Stealth},
s sep = 3em,
l sep = 6em
},
EL/.style = {% shortenes for Edge Label, defined as style
before typesetting nodes={% edge labels positioning
where n=1{edge label/.wrap value={node[pos=0.5,anchor=east]{#1}}}% above left
{edge label/.wrap value={node[pos=0.5,anchor=west]{#1}}}% above right
}
}% end of EL
% tree body
[ S, root % rot is style defined in document preamble
[down, EL=Yes]
[up, EL=No]
]
\end{forest}
\end{document}
其生产成果为:
除了这种可能性之外,还存在其他可能性,如qtree
包(tikz-qtree:使用 TikZ 制作更好的树) ETC。