Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 numbered 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?

share|improve this question
    
Probably (A.north east) has to be parsed char by char before finding which coordinate refers it to. This is done probably by tikz, and thus it cannot be done by \pgfpointanchor. –  JLDiaz May 23 at 9:24

2 Answers 2

up vote 7 down vote accepted

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

share|improve this answer

If you don't mind using internal macros \tikz@scan@one@point will permit the use of TikZ coordinates. It will also take care of the default center anchor:

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

\makeatletter
\def\xcoord#1{%
    % \tikz@scan@one@point takes an argument before the
    % coordinate which is the command to be executed after
    % the coordinate is parsed. \pgf@process just keeps
    % the coordinate calculation local and globally
    % sets \pgf@x and \pgf@y to the appropriate value.
    %
    % NB. Must use \nullfont to prevent naughty spaces.
    %
    {\nullfont\tikz@scan@one@point\pgf@process(#1)}%
    \pgfmathparse{\pgf@x/\pgf@xx}%
    \pgfmathprintnumber{\pgfmathresult}%
}
\def\ycoord#1{%
   {\nullfont\tikz@scan@one@point\pgf@process(#1)}%
   \pgfmathparse{\pgf@y/\pgf@yy}%
   \pgfmathprintnumber{\pgfmathresult}%
}
\makeatother

\pgfkeys{/pgf/number format/.cd,fixed,fixed zerofill,precision=2}
\tikzset{mark cross at/.pic={
  \draw [red, shift={(#1)}, scale=1/20] (1,1) -- (-1,-1) (1,-1) -- (-1,1);
  \node [above=5pt,fill=blue!20, fill opacity=0.75, node font=\tiny\tt]
     at (#1) {(\xcoord{#1},\ycoord{#1})};
}}
\begin{document}

\begin{tikzpicture}

\node[draw, minimum size=4cm, outer sep=0pt] (A) {A};

\path (A) pic {mark cross at={A}}; 
\foreach \a in {north, south, east, west, north west, south west, south east, north east}
  \path pic {mark cross at={A.\a}}; 
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.