What is the difference between points, coordinates, and anchors in TikZ/PGF and how can I deal with them?

I think the confusion comes from the fact that TikZ works with referrals not objects. When certain node name a is mentioned. It usually assumes that the user probably means its center coordinate. Or in case of drawing things between two things if one of them is a node then it computes the point on its border magically so the user doesn't notice.

But all of this is done internally. And nothing is related to nodes or else. A node has a name that you can refer to and predefined places where it understands.

None of these are stored. They are simply checked for existence or just executed. In other words, if you write shape=duck, then it doesn't go through all possible shape names, it simply does an existence check of the sort (I'm using nonsense names for the actual macros)

\pgfutil@ifdefined\pgf@Shape@#1@...{<if it exists use>}{<if not err>}

Anchors are the same deal, if you say anchor=heel, it asks for that name via some \pgf@sh@#1@anchor@#2 and so on.

Now where do these come from? They are defined at the shape declaration via \savedanchor and \anchor and so on.

So they are there and meticulously arranged so that when you refer to it everything from the textbox width height to shape path is painfully hand coded. That's why it is a very tedious job to define new shapes. And when you refer to them they are arranged in such a way that the anchor position is written (globally!) inside the length registers \pgf@<x,y>

Anyway long story short, when you refer to a node anchor actually there is a pretty involved procedure to get a coordinate out of it.

Furthermore, there is a difference between a coordinate which is \pgf@<x,y> and a coordinate type node which is \coordinate.

Finally, when you refer to a node, TikZ try to help you by assuming that you meant its center anchor so that you can save some keystrokes but occasioanlly you need to make a finer surgery such as distance measurements.

You cannot get away with referral names. You need actual coordinates (again not \coordinates). As Mark Wibrow commented, you have to somehow understand the context and that is done via \pgf@process to answer such questions: is it a literal coordinate(1,1), is it a node name, is it a node anchor etc. Then you can use the resulting \pgf@<x,y> registers.

Then you can do whatever you want with them. Your example let syntax also does an amazing job to simplify this but it is still doing what you have described. Actually your question is basically why TikZ exists on top of PGF. It is a very well designed front end to a very verbose but powerful syntax of PGF.


\def\pointA{\pgfpoint{0cm}{0cm}}

Use this only if you are familiar with PGF (The foundation of TikZ). Anyway, it is not important what \pgfpoint does in TeX; this command never exists alone. Usually it is part of the syntax, for instance

\coordinate (A) at (current page.north east);

gives a point a name. It is not important what the actual x- and y-coordinates are. We know where (A) is, and that is all we need to know.

\coordinate (A) at (1,1);

Similar to the previous one. Usually we name points to (A) cut down the code length/complexity and (B) improve legibility. For instance

(current page.north east)

With parenthesis, it issues a move to action. It is much like you, holding a pen, move your hand to the next place you want to draw something. For instance

This code makes TikZ move its pen all around the canvas and declare nodes/coordinates.

\node [anchor=east] {}

place a node "near" the pen's current position. By "near", I mean that the anchor you give should coincide the pen's position. It is much like you, on a boat, can throw the anchor to where you want and that anchor will then fix the boat.


practical part

  • you need \makeatletter.
  • you probably want \pgfmathsetlength instead of \pgfmathsetmacros or \pgfmathparse.
    • but remember, all dimensions must be declared prior to its usage.
  • be careful when you are using the node (current page). Usually you need [overlay].
  • you need to be inside a tikzpicture environment to retrieve anything you made in a tikzpicture environment.
    • if you are not doing the job in the same tikzpicture environment, you need [remember picture].

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\newcommand{\getdistance}[3]{
% Syntax: <length variable/macro> <coordinate1> <coordinate2>
  \pgfpointdiff{\pgfpointanchor{#2}{center}} % http://tex.stackexchange.com/a/39325/13552
               {\pgfpointanchor{#3}{center}} % http://tex.stackexchange.com/a/39325/13552
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgfmathparse{veclen(\pgf@xa,\pgf@ya)} 
  \global\let#1\pgfmathresult % <-- I want the pgfmathresult to be a dimension like 510 pt
}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\coordinate (rightmidtest) at ($ (current page.north east) + (0,-10cm) $); % use tikz calc library
\coordinate (rightmid) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
\end{tikzpicture}

\tikz[remember picture,overlay]{
    \getdistance{\globalresulta}{rightmidtest}{rightmid}
    \getdistance{\globalresultb}{current page}{rightmid}
}

\globalresulta

\globalresultb

\end{document}

  1. \def\pointA{\pgfpoint{0cm}{0cm}}

    This is basically the same as \coordinate (A) at (0,0);

  2. \coordinate (A) at (current page.north east);

    Here you're just naming the top right corner of the page as A. It's pointless in the sense that the location is already named, but it can be helpful not having to type all of that and just write A.

  3. \coordinate (A) at (1,1);

    Coordinates in TikZ are basically nodes with the coordinate shape, they don't require a caption and, especially important, they don't have sep. So it's a point. Still, writing \node[coordinate] (a) at (0,0) {}; equals to \coordinate (a) at (0,0);

    See also TikZ: difference between \node and \coordinate?

  4. (current page.north east)

    This indicates the top-right corner of the current "page". Typically this would be an A4 page, but it can be any dimension. Page in this case is not necessarily intended as the usual piece of paper, but it can be any designated area.

  5. \node [anchor=east] {} <-- an anchor?

    No, this is not an anchor. It's a node whose positioning is determined by placing the designed anchor at the coordinate. So \node [anchor=east] at (4,0) {}; will place the node having the east anchor coincide with the coordinate (4,0).