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:So you should really use those macros as
As you've used them,
\pgfpointanchor
looks for a node calledA.north east
but the name of the node isA
.The reason this works with a
coordinate
is that acoordinate
is really anode
.