我想改变边缘之间的角度,但无法做到。代码如下
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
[sibling distance=25em,
every node/.style = {shape=rectangle, rounded corners, draw, align=center,
top color=white, bottom color=blue!20},
]
\node {Body\\ Temperature}
child { node {Non\\Mammal} edge from parent [->] node [left] {Cold}}
child { node {Give\\ Birth} edge from parent [->] node [right] {Warm}
child { node {Mammal}edge from parent [->] node [left] {Yes}}
child { node {Non \\ Mammal} edge from parent [->] node [right] {No}}};
\end{tikzpicture}
\end{document}
答案1
您可能想要的是以下内容(但我不确定您实际上想要改变哪个角度,也许您可以在问题中添加更多信息):
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
sibling distance=25em,
every node/.style={
shape=rectangle,
rounded corners,
draw,
align=center,
top color=white,
bottom color=blue!20
},
]
\node {Body \\ Temperature}
child { node {Non \\ Mammal} edge from parent [->] node {Cold} }
child { node {Give \\ Birth}
child { node {Mammal} edge from parent [->] node {Yes} }
child { node {Non \\ Mammal} edge from parent [->] node {No} }
edge from parent [->] node {Warm}
};
\end{tikzpicture}
\end{document}
edge from parent
应在定义 ren 之后放置该部分child
,特别是如果添加要放置在边缘上的节点(请参阅手册另一个例子)。为了更好地定位,我会从放置在边缘的节点中删除right
或选项。left
要改变这种基本树(即 Ti钾sibling distance
您可以使用and/or选项更改边的角度(不使用任何附加库的 Z 树),level distance
不仅可以将其添加到整个树,还可以将其添加到特定的child
ren。例如:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
sibling distance=25em,
every node/.style={
shape=rectangle,
rounded corners,
draw,
align=center,
top color=white,
bottom color=blue!20
},
]
\node {Body \\ Temperature}
child { node {Non \\ Mammal} edge from parent [->] node {Cold} }
child { node {Give \\ Birth}
child [sibling distance=5em] { node {Mammal} edge from parent [->] node {Yes} }
child [level distance=10em] { node {Non \\ Mammal} edge from parent [->] node {No} }
edge from parent [->] node {Warm}
};
\end{tikzpicture}
\end{document}
答案2
这里有一个使用 的选项forest
,它允许您使用 来设置整个树的角度calign=fixed edge angles, calign angle=<angle>
。角度(在下面的示例中设置为 60)是边缘与父节点的垂直线之间的角度。您可以调整l sep
以更改节点之间的垂直间距。
请注意,主节点需要的是\forestset
而不是 ,因为垂直对齐节点与 不同。\tikzset
forest
tikz
\documentclass{article}
\usepackage{forest}
\forestset{nodestyle/.style={rounded corners, draw, align=center, top color=white, bottom color=blue!20}}
\tikzset{labelstyle/.style={midway, rounded corners, draw, align=center, top color=white, bottom color=yellow!20}}
\begin{document}
\begin{forest}
for tree={
nodestyle,
l sep=1.2cm,
edge={->},
calign=fixed edge angles, calign angle=60
}
[Body\\Temperature
[Non\\Mammal, edge label={node[labelstyle]{Cold}}]
[Give\\Birth, edge label={node[labelstyle]{Warm}}
[Non\\Mammal, base=top, edge label={node[labelstyle]{No}}]
[Mammal, edge label={node[labelstyle]{Yes}}]
]
]
\end{forest}
\end{document}