I need a macro to extract the x and y coordinates from an arbitrary point, like (3,4)
, or like (A)
, or like ([xshift=-2pt] A.north west)
, where A
is the name of a node.
I've seen the solution
\newdimen\mydim
\newcommand\getx[1]{
\pgfextractx\mydim{\pgfpointanchor{#1}{center}}
}
elsewhere on StackExchange, but this obviously won't work for all the cases described above. I need to be able to call \getx{(3,4)}
and \getx{(A)}
and \getx{([xshift=-2pt] A.north west)}
, and for them all to work equally well, in this case putting the x-coordinate of the argument into the variable \mydim
.
I'm kind of stunned how hard this seems to be! Surely I've missed something…
Best Answer
You can use
\pgfgetlastxy{\XCoord}{\YCoord}
to extract thex
,y
coordinate of the most recently used point into the dimension registers\XCoord
and{\YCoord}
.To make the point the most recently used, I use
\path
macro just before extraction. Here is an example where I define points, extract the x and y coordinates, and then label them via the extracted coordinates. The pointC
is placed at the x-coordainte ofA
and the y-coordiante ofB
.Notes:
This has been updated to work properly when a
scale=
factor is applied to thetikzpicture
. To see what the output is without tweaking for a scale factor uncomment the lineCode: