[Tex/LaTex] What’s the difference between TikZ anchors and coordinates

tikz-pgf

In his answer to Accessing the logic values of a TikZ coordinate, Jake proposes a code to get the value of TikZ coordinate.

Today I've tried to use it to answer Make a polygon with automatically labelled nodes according to their coordinates
where I wanted to print coordinate values of some node anchors

\documentclass[tikz, margin=5pt]{standalone}

\makeatletter
\newcommand\xcoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@x/\pgf@xx}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@y/\pgf@yy}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2}

\begin{document}
\begin{tikzpicture}
\node[draw, minimum size=2cm] (A) {A};
\node[above right] at (A.north east) {(\xcoord{A.north east},\ycoord{A.north east})};
\end{tikzpicture}
\end{document}

but compilation stops with next error message

! Package pgf Error: No shape named A.north east is known.

See the pgf package documentation for explanation. 
Type  H <return> for immediate help.  
...                                              
l.22 ... at (A.north east) {(\xcoord{A.north east}
                                 ,\ycoord{A.north east})};

Then I tried with a real coordinate

\coordinate (aux) at (A.north east);
\node[above right] at (A.north east) {(\xcoord{aux},\ycoord{aux})};

and it worked. Could you explain me why?

Best Answer

If you look at Jake's code, it has an optional argument, which by default is center, used in \pgfpointanchor. A quote from the manual:

\pgfpointanchor{ node }{ anchor }

This command is another “point command” like the commands described in Section 97. It returns the coordinate of the given anchor in the given node.

So you should really use those macros as

\node[above right] at (A.north east) {(\xcoord[north east]{A}, ycoord[north east]{A})};

As you've used them, \pgfpointanchor looks for a node called A.north east but the name of the node is A.

The reason this works with a coordinate is that a coordinate is really a node.

\documentclass[tikz, margin=5pt]{standalone}

\makeatletter
\newcommand\xcoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@x/\pgf@xx}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\newcommand\ycoord[2][center]{{%
    \pgfpointanchor{#2}{#1}%
    \pgfmathparse{\pgf@y/\pgf@yy}%
    \pgfmathprintnumber{\pgfmathresult}%
}}
\makeatother

\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2}

\begin{document}
\begin{tikzpicture}
\node[draw, minimum size=2cm] (A) {A};
\node[above right] at (A.north east) {(\xcoord[north east]{A},\ycoord[north east]{A})};
\end{tikzpicture}
\end{document}

enter image description here