Tikz-PGF – Detailed Guide on Using PGFgetlastxy Inside a Scope

tikz-pgf

I am using the hack here to draw an axis in a tikzpicture and "recreate" the coordinate system in a scope after leaving the axis environment (in order to be able to use foreach commands). However, it seems that \pgfgetlastxy returns the coordinates in the wrong coordinate system. Consider this example

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[ymin=0, ymax=1, xmin=0, xmax=1]
    \coordinate (O) at (axis cs:0,0);
    \coordinate (X) at (axis cs:1,0);
    \coordinate (Y) at (axis cs:0,1);
  \end{axis}
  \begin{scope}[x={($(X)-(O)$)}, y={($(Y)-(O)$)}, shift={(O)}]
    \coordinate (A) at (0.5, 0.5);
    \path (A);
    \pgfgetlastxy{\XCoord}{\YCoord};
    \show\XCoord % Shows 97.49985pt
    \show\YCoord % Shows 80.99976pt
  \end{scope}
\end{tikzpicture}
\end{document}

How do I transform (97.49985pt, 101.2497pt) into the original (0.5,0.5)? Or better yet, is there a command that returns those coordinates?

Best Answer

Note that your question is about PGF/TikZ, not pgfplots. The command \pgfgetlastxy returns numbers in pt. To get some number in cm, you can use

\pgfmathsetmacro{\dABcm}{\dAB*(1pt)/(1cm)} 

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[gray!30] (-1,-1) grid (5,4);
\path
(0,0) coordinate (A) node[below left]{A}
(4,3) coordinate (B) node[above right]{B};

\path (A); \pgfgetlastxy{\xA}{\yA};
\path (B); \pgfgetlastxy{\xB}{\yB};

\pgfmathsetmacro{\dAB}{veclen(\xB-\xA,\yB-\yA)}
\draw (A)--(B) node[midway,above,sloped]{$\dAB$ pt};

\pgfmathsetmacro{\dABcm}{\dAB*(1pt)/(1cm)}
\path (A)--(B) node[midway,below,sloped]{$\dABcm$ cm};
\end{tikzpicture}
\end{document}

You may want to control precision of numbers. To do that, set precision in \pgfkeys

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

and write out with \pgfmathprintnumber as explained in Section 97 Number Printing in pgfmanual

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\pgfkeys{/pgf/number format/.cd,fixed,precision=2}
\draw[gray!30] (-1,-1) grid (5,4);
\path
(0,0) coordinate (A) node[below left]{A}
(4,3) coordinate (B) node[above right]{B};

\path (A); \pgfgetlastxy{\xA}{\yA};
\path (B); \pgfgetlastxy{\xB}{\yB};

\pgfmathsetmacro{\dAB}{veclen(\xB-\xA,\yB-\yA)}
\draw (A)--(B) node[midway,above,sloped]{$\pgfmathprintnumber{\dAB}$ pt};

\pgfmathsetmacro{\dABcm}{\dAB*(1pt)/(1cm)}
\path (A)--(B) node[midway,below,sloped]{$\pgfmathprintnumber{\dABcm}$ cm};
\end{tikzpicture}
\end{document}

Update Note that if we use some explicit or implicit scaling, then all numbers (coordinates) are also be scaled with them same rate. For example,in the above code, use [scale=1.5] then printing the distance of AB will give 7.5 cm instead 5 cm. Unfortunately, pgfplots use implicit scaling, that is, there are some scaling factor that users does not know; therefore, this solution can not be applied for pgfplots. My suggestion is using plain TikZ without any scaling option.

Related Question