我有一个关于 Tikz 绘图标题的问题。下面是我用 Tikz 绘制的二叉树的源代码。我想在它下面放置一个标题,但不幸的是,标题并没有出现在树的中心正下方,而是出现在树的右侧很远的地方。
有人知道如何将标题直接放在树的下方吗?
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}
[level distance=20mm, every node/.style={fill=red!60,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=red!45}},
level 2/.style={sibling distance=20mm,nodes={fill=red!30}},
level 3/.style={sibling distance=10mm,nodes={fill=red!25}}]
\node {A}
child {node {D}
child {node {3}
child {node {X}}
child[missing]
}
child {node {9}}
}
child {node {V}
child {node {19}}
child {node {6}}
};
\end{tikzpicture}
\caption{A tree}
\end{figure}
\end{document}
顺便问一下,有人知道如何在节点旁边放置注释/评论,比如这张图片(Höhe 标签):
非常感谢你的帮助!
答案1
你可能希望使用\centering
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{figure}[htb] %% use position specifiers [htb]
\centering %% <-- here
\begin{tikzpicture}
[level distance=20mm, every node/.style={fill=red!60,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=red!45}},
level 2/.style={sibling distance=20mm,nodes={fill=red!30}},
level 3/.style={sibling distance=10mm,nodes={fill=red!25}}]
\node {A}
child {node {D}
child {node {3}
child {node {X}}
child[missing]
}
child {node {9}}
}
child {node {V}
child {node {19}}
child {node {6}}
};
\end{tikzpicture}
\caption{A tree}
\end{figure}
\end{document}
答案2
要添加注释,需要一个内部名称(),以便您以后可以参考(在此演示中定义了(D)和(3)。这里使用了来自positioning
tikzlibrary 的相对距离,这意味着pos=<0~1>, above, left, right, below, or combination of those keys
可以用来定位注释。此外,您需要使用draw=none
和,fill=none
因为每个节点样式都已定义为用颜色填充的圆圈。
代码
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
[level distance=20mm, every node/.style={fill=red!60,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=red!45}},
level 2/.style={sibling distance=20mm,nodes={fill=red!30}},
level 3/.style={sibling distance=10mm,nodes={fill=red!25}}]
\node {A}
child {node(D) {D}
child {node(3) {3}
child {node {X}}
child[missing]
}
child {node {9}}
}
child {node {V}
child {node {19}}
child {node {6}}
};
\draw (D) -- (3) node[below left= 0.5cm of D,draw=none,fill=none](){comment};
\end{tikzpicture}
\caption{A tree}
\end{figure}
\end{document}