我正在尝试使用该包构建决策树TikZ
,但遇到了一个问题。来自不同分支的两个节点重叠在一起,因此我只能看到其中一个节点,表面上看,两个独立的分支是混合在一起的。代码只允许我通过一个全局参数来操纵节点距离,该参数会同时影响所有节点之间的距离。有没有办法只操纵一部分节点并分离重叠的节点,而不改变所有节点之间的距离?
这是我的代码和输出:
\documentclass{article}
\usepackage{pgf, tikz}
\begin{document}
\begin{tikzpicture}[sibling distance=15em,
every node/.style = {shape=rectangle, rounded corners,
draw, align=center,
top color=white, bottom color=gray!20}]]
\node {no slopes}
child { node {subj slope}
child { node {subj \& item slopes}
child { node {\textbf{max RE}} }
child { node {\textbf{subj slope}} }} %this node is buried and not visible
child { node {item slope}
child { node {subj \& item slopes}
child { node {\textbf{max RE}} }
child { node {\textbf{item slope}} } }
child { node {\textbf{no slopes}} } }};
\end{tikzpicture}
\end{document}
答案1
使用纯 TikZ 不如forest
... 优雅,但通过适当确定sibling distance
第一级和第三级,您可以获得:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
level 1/.style = {sibling distance=17em},
level 3/.style = {sibling distance=8em},
every node/.style = {shape=rectangle, rounded corners,
draw, align=center,
top color=white, bottom color=gray!20}
]
\node {no slopes}
child { node {subj slope}
child { node {subj \& item slopes}
child { node {\textbf{max RE}} }
child { node {\textbf{subj slope}} }
} %this node is buried and not visible
child { node {item slope}
child { node {subj \& item slopes}
child { node {\textbf{max RE}} }
child { node {\textbf{item slope}} }
}
child { node {\textbf{no slopes}} }
}
};
\end{tikzpicture}
\end{document}
答案2
forest
树的一个版本,其中s sep
定义节点边界之间的最小距离,以及l sep
节点与其后代之间的最小距离。要增加它们只需添加+
符号。
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={l sep+=.8cm,s sep+=.5cm,shape=rectangle, rounded corners,
draw, align=center,
top color=white, bottom color=gray!20}
[no slopes
[subject slope
[subj \& item slopes,for children={font=\bfseries}
[max RE]
[subj slope]
]
[max RE,font=\bfseries
[subj \& item slopes,for children={font=\bfseries}
[max RE]
[item slope]
]
[no slopes]
]
]
]
\end{forest}
\end{document}