2

When I try to use relative coordinates in \node using the syntax: (x,y)++(a:r) it gives me an error, but when I just specify the second coordinate as ++(a:r) it's like the (x,y) first coordinate is implicit as (0,0). I wish I could place a node in a point using relative coordinates. Here's my code:

\documentclass[12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    % Triangle ABC
    \draw (0,0)--++(60:2)--++(-60:2)--cycle;
        \foreach \c in {(0,0),++(60:2),++(0:2)}
    \draw[fill] \c circle (.05);
    \node [above] at ++(60:2) {\( A \)};
    \node [right] at ++(0:2) {\( B \)};
    \node [left] at (0,0) {\( C \)};
    % Triangle EFG
    \draw (4,0)--++(60:2)--++(-60:2)--cycle;
        \foreach \c in {(4,0),(4,0)++(60:2),(4,0)++(0:2)}
    \draw[fill] \c circle (.05);
    \node [above] at ++(60:2) {\( E \)};
    \end{tikzpicture}
\end{document}

It would be very easy to place the node E if I could write its point as (4,0)++(60:2). How can I specify it using relative coordinates in node?

4

The relative placement does indeed start at (0,0) if there is no \pgfpathmoveto beforehand. Basically the rule for ++(a) is: move from the last position on this path expression a onwards, be it component-wise for ++(x,y) or with angle as in ++(a:r). You do need that anchor though.

As the implication is that it starts at (0,0) you should consider using a scope with a shift or simply using \path (4,0) node[above] at (60:6) {}.

One option:

\documentclass[12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    % Triangle ABC
    \draw (0,0)--++(60:2)--++(-60:2)--cycle;
        \foreach \c in {(0,0),++(60:2),++(0:2)}
    \draw[fill] \c circle (.05);
    \node [above] at ++(60:2) {\( A \)};
    \node [right] at ++(0:2) {\( B \)};
    \node [left] at (0,0) {\( C \)};
    % Triangle EFG
    \draw (4,0)--++(60:2)--++(-60:2)--cycle;
        \foreach \c in {(4,0),(4,0)++(60:2),(4,0)++(0:2)}
    \draw[fill] \c circle (.05);
    \path (4,0) node [above] at ++(60:6) {\( E \)};
    \end{tikzpicture}
\end{document}

Another one:

\documentclass[12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    % Triangle ABC
    \draw (0,0)--++(60:2)--++(-60:2)--cycle;
    \foreach \c in {(0,0),++(60:2),++(0:2)}
      \draw[fill] \c circle (.05);
    \node [above] at ++(60:2) {\( A \)};
    \node [right] at ++(0:2) {\( B \)};
    \node [left] at (0,0) {\( C \)};
    % Triangle EFG
    \begin{scope}[shift={(4,0)}]
    \draw (0,0)--++(60:2)--++(-60:2)--cycle;
    \foreach \c in {(0,0),++(60:2),++(0:2)}
      \draw[fill] \c circle (.05);
    \path node [above] at ++(60:6) {\( E \)};
    \end{scope}
    \end{tikzpicture}
\end{document}
  • Thank a lot! I didn't know about the shift option and it worked really tidy! – Levy Dec 30 '18 at 15:03

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.