6

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?

  • 1
    One option is to use [y=4cm,x=6cm] instead of [yscale=4,xscale=6]. – Gonzalo Medina May 27 '13 at 0:32
  • @Qrrbrbirlbel, yes I did forget to include \coordinate (kink) at (\XCoord,\XCoord); in the second picture. When I do that, the node extends way off the page. But, as you say, the answers below solve the problem. – profj May 27 '13 at 20:17
  • Related: this thread (It seems that some consider an alternative to the one-node-path-method \path (z) node[below] {$z$}; \pgfgetlastxy{\XCoord}{\YCoord}; more recommendable. – Peter Heinig Jul 16 '17 at 12:23
8

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}
  • 2
    There is a TikZ way: \path[reset cm] coordinate (kink) at (\XCoord,\XCoord); – Qrrbrbirlbel May 27 '13 at 0:48
  • @Qrrbrbirlbel: Ah yes, thank you! I've edited my answer. – Jake May 27 '13 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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