如何在 TiKZ 中将节点定位在另一个节点周围?

如何在 TiKZ 中将节点定位在另一个节点周围?

我想将 TiKZ 中的一些节点定位在给定节点周围。我可以通过设置每个节点的位置来手动完成此操作,但为什么不寻找一种看起来更好的更简单方法呢?

我考虑过使用一条路径并将节点粘贴到路径上,但我不知道该怎么做,而且我也想知道:路径是最好的解决方案吗?

以下是一个示例,其中基节点是 A(它下面有一个节点,但这不是问题;另请注意,如果您的答案中不包含箭头,也没关系,我知道那部分):

在此处输入图片描述

我的代码到目前为止已经到了这个阶段(如果您认为可以更改代码中的某些选项以使事情变得更容易或更好,请告诉我):

\documentclass{minimal}
\usepackage{tikz}
\usepackage{verbatim}


\usetikzlibrary{arrows,positioning} 
\tikzset{
    %Define standard arrow tip
    >=stealth',
    %Define style for boxes
    punkt/.style={
           rectangle,
           rounded corners,
           draw=black, thin,
           text width=6.5em,
           minimum height=2em,
           text centered},
    % Define arrow style
    pil/.style={
           ->,
           thin,
           shorten <=2pt,
           shorten >=2pt,}
}

\begin{document}

    \center\begin{tikzpicture}[node distance=5mm,
    terminal/.style={
    % The shape:
    rectangle,minimum size=6mm,rounded corners=3mm,
    % The rest
    very thick,draw=black!50,
    top color=white,bottom color=black!20,
    font=\ttfamily}]

    \node (nodezero) [terminal] {another node below A};

    \node (a) [terminal, above=3em of nodezero] {A};

    \node (01) [terminal] {1};
    \node (02) [terminal] {2};        
    \node (03) [terminal] {3};
    \node (04) [terminal] {4};
    \node (05) [terminal] {5};
    \node (06) [terminal] {6};
    \node (07) [terminal] {7};
    \node (08) [terminal] {8};
    \node (09) [terminal] {9};

\end{tikzpicture}

\end{document}

答案1

您可能想要使用treestikz 中的库,在这种情况下,在我看来,它会提供相当简单的语法。我为您做了一个小例子。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,trees,arrows}


\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten <=0.5pt,
    main/.style={draw,thick,rounded corners, top color=blue!20, bottom color=white,
        minimum width=1cm},
    child/.style={draw,thick,rounded corners, top color=red!20, bottom color=white}]

    \tikzstyle{level 1}=[sibling angle=22.5]

    \node[main] (a) {A} [counterclockwise from=0]
        child { node[child] {1}}
        child { node[child] {2}}
        child { node[child] {3}}
        child { node[child] {4}}
        child { node[child] {5}}
        child { node[child] {6}}
        child { node[child] {7}}
        child { node[child] {8}}
        child { node[child] {9}}
    ;

    \node[main] (ba) [below of=a] {belowA};

    \path 
    (ba) edge (a)
    ;

\end{tikzpicture}

\end{document}

结果是:

在此处输入图片描述

答案2

这里可以使用的一种方法是\node (<name>) at (<coordinate>) {<text>};语法,您可以使用calc库在节点的位置添加极坐标A,即\node (1) at ($ (A) + (<angle>:<distance>) $) {1};。有关其他方法,请参阅positioning手册中的库。

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{arrows,positioning}
\usetikzlibrary{calc}
\tikzset{
    %Define standard arrow tip
    >=stealth',
    %Define style for boxes
    punkt/.style={
           rectangle,
           rounded corners,
           draw=black, thin,
           text width=6.5em,
           minimum height=2em,
           text centered},
    % Define arrow style
    pil/.style={
           ->,
           thin,
           shorten <=2pt,
           shorten >=2pt,}
}

\begin{document}

    \begin{tikzpicture}[node distance=5mm,
    terminal/.style={
    % The shape:
    rectangle,minimum size=6mm,rounded corners=3mm,
    % The rest
    very thick,draw=black!50,
    top color=white,bottom color=black!20,
    font=\ttfamily}]

    \node (nodezero) [terminal] {another node below A};

    \node (a) [terminal, above=3em of nodezero] {A};

    \node (01) at ($ (a) + (10:2cm) $) [terminal] {1};
    \node (02) at ($ (a) + (30:2cm) $) [terminal] {2};        
    \node (03) at ($ (a) + (50:2cm) $) [terminal] {3};
    \node (04) at ($ (a) + (70:2cm) $) [terminal] {4};
    \node (05) at ($ (a) + (90:2cm) $) [terminal] {5};
    \node (06) at ($ (a) + (110:2cm) $) [terminal] {6};
    \node (07) at ($ (a) + (130:2cm) $) [terminal] {7};
    \node (08) at ($ (a) + (150:2cm) $) [terminal] {8};
    \node (09) at ($ (a) + (170:2cm) $) [terminal] {9};

\end{tikzpicture}

\end{document}

结果

相关内容