2

I am trying to built a graph by manually placing the nodes using relative placement. But I face some issues with the way the nodes are placed. I tried to include it in a 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}

As you can see, the placement of the circles and of the text nodes relative to the first node does not match. Secondly, the three circles do not align horizontally. And as a third "bonus", the last circle placed with a combination of the below key and an explicit position in x-direction is mismatched in a third way.

What is right way to place such nodes (with different shapes and different ways to place them) without manually adding coordinates? I need to combine different shapes and I need to set some x-coordinates explicitly to correctly distribute the nodes in different branches...

Edit: To clarify: I would like to vertically align the circles and the rectangles, and I would like to horizontally align their west anchors.

7
  • 1
    You can change the node anchor anchor=west for example.
    – Sigur
    Commented Feb 17, 2016 at 18:02
  • As far as I know, this is done automatically when using the keyword right. Otherwise the nodes would not end up next to each other but overlap.
    – Dux
    Commented Feb 17, 2016 at 18:03
  • My problem is that no anchors of the different nodes align. Neither the center of the red circles align with the center of the text node, nor the north or south anchor or the west or east anchor
    – Dux
    Commented Feb 17, 2016 at 18:04
  • Try with anchor=base west
    – Sigur
    Commented Feb 17, 2016 at 18:06
  • Doesn't change a thing. Why should it?
    – Dux
    Commented Feb 17, 2016 at 18:14

1 Answer 1

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

sets the anchor of node b to south west and not to west. Then b.south west is positioned 1cm to the right and 1cm above of a.north east. And if the node is a circle there is difference in x-direction between anchor south west and west.

If the anchor of b should be west instead of south west you have to use anchor=west after the option 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}

![enter image description here

And maybe you want to position the west anchors relatively to the east anchor 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.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}

enter image description here

1
  • This cleared things up significantly, thank you very much! I didnt realize this although i read the corresponding parts of the manual twice...
    – Dux
    Commented Feb 18, 2016 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .