4

Here are four TikZ environments to draw two parallel line segments. In all of the environments, one endpoint of the first line segment is specified with either \coordinate(A) or \node(A){$A$};, and one endpoint of the second line segment is specified with either \coordinate(B) or \node(B){$B$};.

Point A is specified first; no coordinates are given for it. Does TikZ place it at the origin by default? It seems to be placed at the origin because, in the second graph, I have the command \node at (0,0) {$O$};, and "O" is typeset at the coordinate for A.

Except for the positioning of "A," the second and third displays are identical. In the second display, I use \coordinate(A); and \node at (0,0) {$A$}; to position "A," and in the third display, I use \node[inner sep=0pt,outer sep=0pt] (A){$A$}; to position "A." In the second display, the "A" seems to be centered at (0,0), and in the third display, the "A" seems to be typeset left of (0,0). Why is there a difference?

The only difference in the coding for the third and fourth displays is that I have inner sep=0pt as an option in the node command for typesetting "A" in the third display and inner sep=1.5pt as an option in the node command for typesetting "A" in the fourth display. Why does this shift "everything" in the fourth display as compared to the third display?

\documentclass{amsart}
\usepackage{tikz}\usetikzlibrary{calc,positioning}
\begin{document}

    \begin{tikzpicture}
    \coordinate(A);
    \coordinate[right=of A](B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \end{tikzpicture}
    \vskip1.25mm

    \begin{tikzpicture}
    \coordinate(A);
    \coordinate[right=of A](B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \node at (0,0) {$A$};
    \end{tikzpicture}
    \vskip1.25mm

    \begin{tikzpicture}
    \node[inner sep=0pt,outer sep=0pt] (A){$A$};
    \coordinate[right=of A](B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \end{tikzpicture}
    \vskip1.25mm

    \begin{tikzpicture}
    \node[inner sep=1.5pt,outer sep=0pt] (A) {$A$};
    \coordinate[right=of A] (B);
    \draw[yellow, line width=2pt] (A) -- ++(1,1);
    \draw[red] (B) -- ++(1,1);
    \end{tikzpicture}

\end{document}
  • 1
    Add draw option to the nodes and it will be a bit more obvious – percusse May 2 '15 at 19:57
  • @percusse I added the option draw to the node commands. Since I have \node[draw] at (0,0) {$A$}; for the code in the second display, and since I have \node[inner sep=0pt,outer sep=0pt] (A){$A$}; for the code in the third display, and since the lower left corner of the box for the node in the second display seems to be in the corresponding spot as the left foot of the "A" in the third display, I surmise that lower left figure that is to be drawn first is placed at the origin. – user74973 May 2 '15 at 20:45
  • @percusse Is the default inner separation 2pt? – user74973 May 2 '15 at 20:46
  • 1
    In addition to what @percusse suggested, add at the end of each picture the line \draw (current bounding box.south west) grid (current bounding box.north east) and you should then get a more complete picture of what's happening. The right=of directives, applied to nodes, are with reference, not to the center of the node (as you might expect), but the boundary of the node (controlled by inner sep): there's also outer sep to play with. – A.Ellett May 3 '15 at 1:20
  • 1
    To ease drawing each node, you could declare at the beginning of the document \tikzset{every node/.append style={draw}} – A.Ellett May 3 '15 at 1:21
2

Does the following help?

\documentclass[tikz,border=5pt,mult]{standalone}
\usetikzlibrary{positioning}

\begin{document}

  \begin{tikzpicture}[every node/.style={draw}]
    \draw [help lines] (-1,-1) grid (2,2);
    \coordinate(A);
    \coordinate[right=of A](B);
    \draw [blue] (A) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \node at (0,0) {$A$};
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

  \begin{tikzpicture}[every node/.style=draw]
    \draw [help lines] (-1,-1) grid (2,2);
    \node[inner sep=0pt,outer sep=0pt] (A){$A$};
    \coordinate[right=of A](B);
    \draw [blue] (A) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

\end{document}

case 1

In the first case, the blue line is drawn from the coordinate A, and B is positioned relative to this coordinate. Since coordinates are points, this amounts to drawing from (0,0).

case 2

In the second case, the blue line is drawn from the node A, and B is positioned relative to the node. TikZ assumes that you want to draw or measure from the nearest point on the node's boundary. Even if A has zero inner sep and outer sep, it still has a positive size because it contains the letter A which takes up space. So, in this case, the line is drawn from a little above, and a little to the right of (0,0) and B is positioned relative to a point a little to the right of (0,0).

You can emulate the effect of using a coordinate by specifying the center anchor:

  \begin{tikzpicture}[every node/.style=draw]
    \draw [help lines] (-1,-1) grid (2,2);
    \node[inner sep=0pt,outer sep=0pt] (A){$A$};
    \coordinate[right=of A.center](B);
    \draw [blue] (A.center) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

using an anchor to emulate a coordinate

Alternatively, if the node A has no content, as well as zero sep, it will behave similarly to a coordinate because it will essentially take no space:

  \begin{tikzpicture}[every node/.style=draw]
    \draw [help lines] (-1,-1) grid (2,2);
    \node[inner sep=0pt,outer sep=0pt] (A){};
    \coordinate[right=of A](B);
    \draw [blue] (A) -- ++(1,1);
    \draw (B) -- ++(1,1);
    \node at (A) {$A$};
    \path [fill=blue] (0,0) circle (1.5pt);
  \end{tikzpicture}

using an empty node to emulate a coordinate

Note that the A is centred on the origin (0,0) (marked by the blue dot) in every case.

  • I was debating writing up just the same sort of detail. :^) – A.Ellett May 3 '15 at 1:30
  • @A.Ellett I was doing it while you were commenting, I think. There was only percusse's comment when I started. Sorry if you wanted to answer...? – cfr May 3 '15 at 1:32
  • 1
    No apology needed. I noticed as I was commenting that you had made an edit. I figured I'd wait to see if you were going to post an answer too before writing one of my own. And, you posted an answer. All's good. :) – A.Ellett May 3 '15 at 1:36
  • @A.Ellett Thanks. Let me know if you think I missed anything. – cfr May 3 '15 at 1:39
  • @cfr Yes, that is a detailed explanation! I think that I understood what Percusse had said, but the illustrations make for a quicker reference. – user74973 May 4 '15 at 16:44

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.