4

I have a problem using relative positioning in TikZ. As shown in the MWE below, my "vertex circles" are not placed correctly when putting two figures side by side using \node[right = of ...] syntax.

Code using "right = of ..." syntax:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
    vertex/.style={circle,fill=black,minimum size=.8mm,inner sep=0pt}
}

\begin{document}
\begin{tikzpicture}
\node (2nd_1) at (0,0) {
    \tikz{
        \coordinate (u1) at (0,0);
        \coordinate[below= of u1] (l1);
        \coordinate[right= of u1] (u2);
        \coordinate[right= of l1] (l2);
        %           
        \draw           (u1)    --  (l1);
        \draw           (u2)    --  (l2);
        \draw           (l1)    --  (l2);
        \draw           (l1)        node[vertex]{};
        \draw           (l2)        node[vertex]{};
    }
};
\node[right=0 of 2nd_1] (2nd_2) {
    \tikz{
        \coordinate (u1) at (0,0);
        \coordinate[below= of u1] (l1);
        \coordinate[right= of u1] (u2);
        \coordinate[right= of l1] (l2);
        %           
        \draw           (u1)    --  (l1);
        \draw           (u2)    --  (l2);
        \draw           (l1)    --  (l2);
        \draw           (l1)        node[vertex]{};
        \draw           (l2)        node[vertex]{};
    }
};
\end{tikzpicture}
\end{document}

Code setting the position manually:

\begin{tikzpicture}
\node (2nd_1) at (0,0) {
    \tikz{
        \coordinate (u1) at (0,0);
        \coordinate[below= of u1] (l1);
        \coordinate[right= of u1] (u2);
        \coordinate[right= of l1] (l2);
        %           
        \draw           (u1)    --  (l1);
        \draw           (u2)    --  (l2);
        \draw           (l1)    --  (l2);
        \draw           (l1)        node[vertex]{};
        \draw           (l2)        node[vertex]{};
    }
};
\node (2nd_2) at (1.5,0) {
    \tikz{
        \coordinate (u1) at (0,0);
        \coordinate[below= of u1] (l1);
        \coordinate[right= of u1] (u2);
        \coordinate[right= of l1] (l2);
        %           
        \draw           (u1)    --  (l1);
        \draw           (u2)    --  (l2);
        \draw           (l1)    --  (l2);
        \draw           (l1)        node[vertex]{};
        \draw           (l2)        node[vertex]{};
    }
};
\end{tikzpicture}

Left: using "right = of" syntax. Right: Manual position Diagrams

As is evident from the picture, the vertex-circles are not placed correctly on the second figure to the left. I can't seem to figure out, where the error originates so any help/explanation is appreciated.

3

Another case study of the message: Don't nest tikzpictures! The \tikz{} contents inherit the node options due to being in the same scope hence it doesn't start a fresh copy of tikzpicture environment.

If you want to group things together use scopes and shift the scope.

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\tikzset{vertex/.style={circle,fill=black,minimum size=.8mm,inner sep=0pt}}

\begin{document}
\begin{tikzpicture}
        \coordinate (u1) at (0,0);
        \coordinate[below= of u1] (l1);
        \coordinate[right= of u1] (u2);
        \coordinate[right= of l1] (l2);
        %           
        \draw           (u1)    --  (l1);
        \draw           (u2)    --  (l2);
        \draw           (l1)    --  (l2);
        \draw           (l1)        node[vertex]{};
        \draw           (l2)        node[vertex]{};

\begin{scope}[shift={([xshift=1cm]u2)}]
        \coordinate (u1) at (0,0);
        \coordinate[below= of u1] (l1);
        \coordinate[right= of u1] (u2);
        \coordinate[right= of l1] (l2);
        %           
        \draw           (u1)    --  (l1);
        \draw           (u2)    --  (l2);
        \draw           (l1)    --  (l2);
        \draw           (l1)        node[vertex]{};
        \draw           (l2)        node[vertex]{};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you. I wasn't aware of the "don't nest tikzpictures", but it makes sense. I ended up using a matrix-structure for aligning even more pictures, but the scope solution is very nice. – Rasmus Søgaard Christensen Apr 14 '15 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.