我正在尝试使用 tikz-qtree 设置一个用于渲染决策树的模板。以下是我最终得到的结果:
\documentclass[border = 0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[
% liens
edge from parent path={
[thick, ->, rounded corners]
(\tikzparentnode.south) |- +(0,-0.7cm) -| (\tikzchildnode.north)
},
% racine
every level 0 node/.style={
draw, rounded rectangle, minimum height=1cm, thick
},
% noeuds internes
every internal node/.style={
draw, diamond, minimum width=2cm, minimum height=2cm, thick
},
% feuilles 'oui'
yesnode/.style={
circle, draw=green!60, fill=green!5, very thick,
minimum width=1cm, minimum height=1cm
},
nonode/.style={
circle, draw=red!60, fill=red!5, very thick,
minimum width=1cm, minimum height=1cm
},
% distances de séparations
level distance=3.3cm, sibling distance=2cm
]
\Tree
[.{conditions météorologiques}
\edge node[auto=left, pos=0.25, above]{ensoleillé};
[.{humidité}
\edge node[auto=left, pos=0.25, above]{basse};
[.\node[yesnode]{oui};
]
\edge node[auto=left, pos=0.25, above]{haute};
[.\node[nonode]{non};
]
]
\edge node[auto=left, pos=0.25]{pluvieux};
[.{vent}
\edge node[auto=left, pos=0.25, above]{faible};
[.\node[yesnode]{oui};
]
\edge node[auto=left, pos=0.25, above]{fort};
[.\node[nonode]{non};
]
]
]
\end{tikzpicture}
\end{document}
在阅读文档并浏览了几个 Latex stack exchange 帖子后,我仍然有以下问题:
我如何水平对齐节点中的文本'脱太阳' 和 '普吕维厄“?基本上是向下移动”普吕维厄'。我相信字母“p”是造成问题的原因。
目前我必须手动确定内部节点的最小高度和宽度。有没有办法让编译器选择适合文本的内部节点的最小高度和宽度,然后将其应用于所有其他内部节点?
如果我的脚本中有任何类型的不良习惯,请告诉我。
我为我的英语不好以及使用的术语不正确而道歉。如果有人知道如何解决上述任何问题,或者他们已经去过其他地方,请告诉我 :)
答案1
欢迎来到 TeX.SX!
为了解决有关不同节点中文本垂直移位的第一个问题,您可以将选项设置text depth
为零(或其他固定单位)every node
。或者,正如Torbjørn T.在评论中所建议的那样,您可以添加\strut
到每个节点的文本中。
至于第三个问题,您需要定义另一条没有圆角的边,如果该边垂直向下,则需要选择该边。我假设某些舍入错误导致此处未提供所需的直线。以下代码可能有助于您实现此目的:
\documentclass[border = 0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}
\usepackage{tikz-qtree}
\begin{document}
\begin{tikzpicture}[
% liens
edge from parent path={
[thick, ->, rounded corners]
(\tikzparentnode.south) |- +(0,-0.7cm) -| (\tikzchildnode.north)
},
straight/.style={
edge from parent path={
[thick, ->]
(\tikzparentnode.south) -- (\tikzchildnode.north)
}
},
every node/.style={
text depth=0pt,
},
% racine
every level 0 node/.style={
draw, rounded rectangle, minimum height=1cm, thick
},
% noeuds internes
every internal node/.style={
draw, diamond, minimum width=2cm, minimum height=2cm, thick
},
% feuilles 'oui'
yesnode/.style={
circle, draw=green!60, fill=green!5, very thick,
minimum width=1cm, minimum height=1cm
},
nonode/.style={
circle, draw=red!60, fill=red!5, very thick,
minimum width=1cm, minimum height=1cm
},
% distances de séparations
level distance=3.3cm, sibling distance=2cm
]
\Tree
[.{conditions météorologiques}
\edge node[auto=left, pos=0.25, above]{ensoleillé};
[.{humidité}
\edge node[auto=left, pos=0.25, above]{basse};
[.\node[yesnode]{oui};
]
\edge node[auto=left, pos=0.25, above]{haute};
[.\node[nonode]{non};
]
]
\edge[straight];
[.{test} ]
\edge node[auto=left, pos=0.25]{pluvieux};
[.{vent}
\edge node[auto=left, pos=0.25, above]{faible};
[.\node[yesnode]{oui};
]
\edge node[auto=left, pos=0.25, above]{fort};
[.\node[nonode]{non};
]
]
]
\end{tikzpicture}
\end{document}
第二个问题有点难解决。目前我想不出一个简单的解决办法。
答案2
作为纯 Ti钾Z 图像。更短更简单的代码:
\documentclass[border = 0.2cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
shapes}
\begin{document}
\begin{tikzpicture}[
node distance = 21mm and 7mm,
arr/.style = {-Straight Barb, semithick, rounded corners},
C/.style = {circle, draw=#1, thick, fill=#1!30, minimum size=2em},
D/.style = {diamond, draw, thick, aspect=2, text width=5em, align=center,
inner xsep=0pt, text depth=0.5ex},
N/.style = {rounded rectangle, draw, thick, minimum height=5ex},
lbl/.style = {text depth=0.5ex, inner sep=2pt, pos=0.25, above}
]
%%% nodes
\node (n1) [N] {conditions météorologiques};
%
\node (n2a) [D, below left =of n1] {humidité};
\node (n2b) [D, below right=of n1] {vent};
\node (n2c) [D, at={(n2a -| n1)}] {test};
%
\node (n31a) [C=teal, below left =of n2a.west] {oui};
\node (n31b) [C=red, below right=of n2a.east] {non};
\node (n32a) [C=teal, below left =of n2b.west] {oui};
\node (n32b) [C=red, below right=of n2b.east] {non};
%%% edges
\draw[arr] (n1.south) -- ++ (0,-1) -| (n2a) node[lbl] {ensoleillé};
\draw[arr] (n1.south) -- ++ (0,-1) -| (n2b) node[lbl] {pluvieux};
\draw[arr] (n1) -- (n2c);
\draw[arr] (n2a.south) -- ++ (0,-0.8) -| (n31a) node[lbl] {basse};
\draw[arr] (n2a.south) -- ++ (0,-0.8) -| (n31b) node[lbl] {haute};
\draw[arr] (n2b.south) -- ++ (0,-0.8) -| (n32a) node[lbl] {faible};
\draw[arr] (n2b.south) -- ++ (0,-0.8) -| (n32b) node[lbl] {fort};
\end{tikzpicture}
\end{document}