![循环树中边的级别距离](https://linux22.com/image/473602/%E5%BE%AA%E7%8E%AF%E6%A0%91%E4%B8%AD%E8%BE%B9%E7%9A%84%E7%BA%A7%E5%88%AB%E8%B7%9D%E7%A6%BB.png)
\documentclass[png,tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,decorations,shadows}
% style definitions
\tikzset{level 1/.style={sibling angle=90,level distance=25mm}}
\begin{document}
\begin{tikzpicture}
[grow cyclic,cap=round]
\node[shape=circle, draw, minimum size=12pt] {\large PL}
child {node[shape=circle, draw, minimum size=8pt] {}
[level distance=1cm]
child {node {Textual}}
child {node {Visual}}
}
child {node[shape=circle, draw, minimum size=8pt] {}
[level distance=1cm]
child {node {General-purpose}}
child {node {Special-purpose}}
}
child {node[shape=circle, draw, minimum size=8pt] {}}
child {node[shape=circle, draw, minimum size=8pt] {}}
;
\end{tikzpicture}
\end{document}
如您所见,尽管我指定了level distance=1cm
。它是从中心测量的,因此“特殊用途”离其父级太近了。“文本”和“视觉”的边长也不同。
我可以测量距离边缘的水平距离,以便无论文本长度如何,边缘长度都是 1 厘米吗?
这个问题在某种程度上类似于在tikz中设置边界之间的节点距离但该柱子并没有建造树木。
答案1
另一种方法是使用chains
,默认情况下,它测量节点边界之间的距离。scopes
当您有很多分支时,该库可以使代码更简洁。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains, scopes}
\begin{document}
\begin{tikzpicture}[start chain, every on chain/.style=join, node distance=10mm and 10mm]
\node [on chain, draw, circle] {PL};
{[start branch=1 going above right]
\node [on chain, draw, circle] {};
}
{[start branch=2 going above left]
\node [on chain, draw, circle] {};
}
{[start branch=3 going below left]
\node [on chain, draw, circle] {};
{[start branch=31 going left]
\node [on chain] {Textual};
}
{[start branch=32 going below]
\node [on chain] {Visual};
}
}
{[start branch=4 going below right]
\node [on chain, draw, circle] {};
{[start branch=41 going right]
\node [on chain] {Special purpose};
}
{[start branch=42 going below]
\node [on chain] {General purpose};
}
}
\end{tikzpicture}
\end{document}
答案2
这是因为用于定位节点的坐标位于每个节点的中心。您可以使用以下方法更改该坐标anchor
:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees}
% style definitions
\tikzset{level 1/.style={sibling angle=90,level distance=25mm}}
\begin{document}
\begin{tikzpicture}[grow cyclic,cap=round]
\node[shape=circle, draw, minimum size=12pt] {\large PL}
child {node[shape=circle, draw, minimum size=8pt] {}
[level distance=1cm]
child {node[anchor=east] {Textual}}
child {node[anchor=north] {Visual}}
}
child {node[shape=circle, draw, minimum size=8pt] {}
[level distance=1cm]
child {node[anchor=north] {General-purpose}}
child {node[anchor=west] {Special-purpose}}
}
child {node[shape=circle, draw, minimum size=8pt] {}}
child {node[shape=circle, draw, minimum size=8pt] {}}
;
\end{tikzpicture}
\end{document}