循环树中边的级别距离

循环树中边的级别距离
\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}

在此处输入图片描述

相关内容