노드를 균등하게 분배

노드를 균등하게 분배

상대 배치를 사용하여 노드를 수동으로 배치하여 그래프를 작성하려고 합니다. 하지만 노드 배치 방식에 몇 가지 문제가 있습니다. MWE에 포함시키려고 했습니다.

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};

anchor노드를 및 b로 설정합니다 .south west~ 아니다에게 west. 그런 다음 b.south west의 오른쪽으로 1cm, 위쪽으로 1cm 위치에 배치됩니다 a.north east. 그리고 노드가 원인 경우 x앵커 south west와 의 방향에 차이가 있습니다 west.

대신에 앵커를 사용해야 b하는 경우westsouth westanchor=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기준으로 앵커를 배치하고 싶을 수도 있습니다 .easta

\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}

여기에 이미지 설명을 입력하세요

관련 정보