[Tex/LaTex] Extracting x, y coordinates of an arbitrary point in TiKZ while using yscale and xscale

coordinatesscalingtikz-pgf

Inspired by Extract x, y coordinate of an arbitrary point in TikZ, I am trying to use the x coordinate of a point to draw another element.

However, when I use the yscale and xscale commands, the extracted coordinates do not similarly scale up. Here is an example:

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

\begin{document}

\begin{tikzpicture} % First picture
  \draw[->] (0,0) -- (2,0) ;
  \draw[->] (0,0) -- (0,2) ;    

  \coordinate (z) at (0.5,0);
  \path (z) node[below] {$z$};
  \pgfgetlastxy{\XCoord}{\YCoord};

  \coordinate (kink) at (\XCoord,\XCoord);
  \draw[gray] (0,0) -- (kink) node[above, red] {$c$};
\end{tikzpicture}

\begin{tikzpicture}[yscale=4,xscale=6] % Second picture
  \draw[->] (0,0) -- (2,0) ;
  \draw[->] (0,0) -- (0,2) ;    

  \coordinate (z) at (0.5,0);
  \path (z) node[below] {$z$};
  \pgfgetlastxy{\XCoord}{\YCoord};

  \coordinate (kink) at (\XCoord,\XCoord);
  \draw[gray] (0,0) -- (kink) node[above, red] {$c$};
\end{tikzpicture}

\end{document}

First picture with no xscale/yscale
Second picture with xscale/yscale

In the first picture, the extracted coordinate works as expected. In the second picture, I try to scale up the entire picture, but the extracted coordinate now extends off of the page. How do I get the extracted coordinate to scale correctly?

Best Answer

I believe this happens because you're mixing TikZ transformations with PGF commands for extracting the coordinates. One way to fix this is to reset the transformations when using the dimensions extracted using \pgfgetlastxy. You can do this by using \begin{scope}[reset cm] (or by using \path [reset cm] coordinate instead of just \coordinate):

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[yscale=4,xscale=6]

\draw[->] (0,0) -- (2,0) ;
\draw[->] (0,0) -- (0,2) ;  

\coordinate (z) at (0.5,0);
\path (z) node[below] {$z$};
\pgfgetlastxy{\XCoord}{\YCoord};

\begin{scope}[reset cm]
    \coordinate (kink) at (\XCoord,\XCoord);
\end{scope}

\draw[gray] (0,0) -- (kink) node[above, red] {$c$};

\end{tikzpicture}

\end{document}