我正在使用给出的第一个答案绘制 AVL 树TikZ 中的 AVL 树:在节点外部绘制举个例子。
我要做的就是添加一个样式定义,以小字体和蓝色绘制标签。当然,我可以为每个标签单独执行此操作,但将其设置为样式会更优雅。到目前为止,我的样式是
\begin{tikzpicture}[
level/.style={sibling distance=60mm/#1},
every node/.style={draw,circle},
label distance=-1mm]
效果很好 - 但是如何向这些标签添加蓝色文本和小字体?
请注意,我根据其角度和文本创建每个标签:
node [label=30:\small $1$] {6}
或颜色
node [label={[blue]30:\small $1$}] {6}
无论如何,我想将标签格式化为样式的一部分。谢谢!
答案1
在您所指的答案中,将标签字体设为蓝色且较小是完全没有问题的。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[level/.style={sibling distance=60mm/#1},
every node/.style={draw,circle},
label distance=-1mm,
edge from parent path=
{(\tikzparentnode.south) .. controls +(0,-.5) and +(0,.5)
.. (\tikzchildnode.north)},
every node/.style={draw,circle},
label distance=-1mm]
\node [label={[font=\small,text=blue]30:$-1$}]{50}
child {node[label={[font=\small,text=blue]30:$-1$}] {10}}
child {node[label={[font=\small,text=blue]30:$0$}] {20}
child {node[label={[font=\small,text=blue]30:$-1$}] {10}}
child {node[label={[font=\small,text=blue]30:$-1$}] {20}}
};
\end{tikzpicture}
\end{document}
将其变成一种风格是相当容易的。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[level/.style={sibling distance=60mm/#1},
every node/.style={draw,circle},
label distance=-1mm,
edge from parent path=
{(\tikzparentnode.south) .. controls +(0,-.5) and +(0,.5)
.. (\tikzchildnode.north)},
every node/.style={draw,circle},
label distance=-1mm,
my label/.style n args={2}{label={[font=\small,text=blue]#1:#2}}]
\node [my label={30}{$-1$}]{50}
child {node[my label={30}{$-1$}] {10}}
child {node[my label={30}{$0$}] {20}
child {node[my label={-30}{$-1$}] {10}}
child {node[my label={-60}{$-1$}] {20}}
};
\end{tikzpicture}
\end{document}