均勻分佈節點

均勻分佈節點

我正在嘗試透過使用相對位置手動放置節點來建立圖表。但我在節點放置方式方面遇到了一些問題。我嘗試將其包含在 MWE 中:

微量元素

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
    test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex}
]
    \small

    \node (a) [test node] {First node};
    \node (b) [test node, above right=of a] {Upper branch 1};
    \node (c) [test node, below right=of a] {Lower branch 1};

    \begin{scope}[red]
        \foreach \pos/\n in {above right/x, right/y, below right/z}
            {
                \node (\n) [circle, draw, \pos=of a] {};
                \foreach \a in {north, center, south}
                {
                    \draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
                }
            }
        \foreach \n in {x, y, z}
            \draw (0,2 -| \n.center) -- ++(0,-4);
        \node [circle, draw, below=of a] at (3,0) {};
    \end{scope}
\end{tikzpicture}
\end{document}

正如您所看到的,圓圈和文字節點相對於第一個節點的位置不符。其次,這三個圓圈並不水平對齊。作為第三個“獎勵”,用鍵和 x 方向上的顯式位置組合放置的最後一個圓圈below以第三種方式不匹配。

在不手動新增座標的情況下放置此類節點(具有不同形狀和不同放置方式)的正確方法是什麼?我需要組合不同的形狀,並且需要明確設置一些 x 座標以正確地將節點分佈在不同的分支中...

編輯:澄清一下:我想垂直對齊圓形和矩形,並且我想水平對齊它們的西錨點。

答案1

\node (b) [test node, above right=of a] {Upper branch 1};

anchorof 節點設定bsouth west不是west。然後b.south west定位在 的右側 1cm 和上方 1cm 處a.north east。如果節點是圓形,則x錨點south west和之間的方向存在差異west

如果錨點b應該是west而不是south west你必須使用anchor=west 選項above right=of a

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
    test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex},
    node distance=1cm and 1cm
]
    \small

    \node (a) [test node] {First node};
    \node (b) [test node, above right=of a,anchor=west] {Upper branch 1};
    \node (c) [test node, below right=of a,anchor=west] {Lower branch 1};

    \begin{scope}[red]
        \foreach \pos/\n in {above right/x, right/y, below right/z}
            {
                \node (\n) [circle, draw, \pos=of a,anchor=west] {};
                \foreach \a in {north, center, south}
                {
                    \draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
                }
            }
        \foreach \n in {x, y, z}
            \draw (0,2 -| \n.center) -- ++(0,-4);
        \node [circle, draw, below=of a] at (3,0) {};
    \end{scope}

    \draw[green](a.east)--+(1,0);
    \draw[blue](a.north east)--++(1,0)--+(0,1);
    \draw[orange](a.south east)--++(1,0)--+(0,-1);
\end{tikzpicture}
\end{document}

![在此輸入影像描述

也許您想west相對於east的錨點定位錨點a

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
    test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex},
    node distance=1cm and 1cm
]
    \small

    \node (a) [test node] {First node};
    \node (b) [test node, above right=of a.east,anchor=west] {Upper branch 1};
    \node (c) [test node, below right=of a.east,anchor=west] {Lower branch 1};

    \begin{scope}[red]
        \foreach \pos/\n in {above right/x, right/y, below right/z}
            {
                \node (\n) [circle, draw, \pos=of a.east,anchor=west] {};
                \foreach \a in {north, center, south}
                {
                    \draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
                }
            }
        \foreach \n in {x, y, z}
            \draw (0,2 -| \n.center) -- ++(0,-4);
        \node [circle, draw, below=of a] at (3,0) {};
    \end{scope}

    \draw[green](a.east)--+(1,0);
    \draw[blue](a.east)++(1,0)--+(0,1);
    \draw[orange](a.east)++(1,0)--+(0,-1);
    \draw[purple](a.center)--++(0,-1)--+(3,0);
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容