# Compute the elevation of a coordinate in TikZ

I'm wracking my brains over a seemingly simple problem. Given a coordinate, I would like to do some computation with its components. More specifically, I want to compute the elevation of a point.

Consider this M(not)WE:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);

\newdimen\x
\newdimen\y
\pgfextractx{\x}{\pgfpointanchor{A}{center}}
\pgfextracty{\y}{\pgfpointanchor{A}{center}}
\pgfmathparse{atan2(\y,\x)}
\node at (1,1) {\pgfmathresult};
\end{tikzpicture}
\end{document}


My goal is to extract the x and the y component from the point A, compute the elevation, and print this in a node. How can I achieve this?

• Commented Dec 4, 2014 at 21:57
• Remark: the extraction part is correct (same as in the question linked above), but the bug in this specific question is that \pgfmathresult value is overridden by the implementation of \node. Using \pgfmathsetmacro or computing the \pgfmathresult "late" as in the answer below does not suffer from this issue. Commented Dec 23, 2022 at 4:10

You can use

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate (A) at (2,3);
%
\newdimen\x
\newdimen\y
\pgfextractx{\x}{\pgfpointanchor{A}{center}}
\pgfextracty{\y}{\pgfpointanchor{A}{center}}
\node at (1,1) {\pgfmathparse{atan2(\y,\x)}\pgfmathresult};
\end{tikzpicture}
\end{document}


or

\def\myresult{\pgfmathparse{atan2(\y,\x)}\pgfmathresult}
\node at (1,1) {\myresult};


or

\pgfmathsetmacro\myresult{atan2(\y,\x)}
\node at (1,1) {\myresult};

• Thanks! One question though. Say I want to use \myresult as a coordinate in a polar coordinate, as in \node at (\myresult:1) {test};, how can I do that?
– Ingo
Commented Apr 25, 2014 at 12:23
• Nevermind, with \pgfmathsetmacro it works.
– Ingo
Commented Apr 25, 2014 at 12:26

There is a standard way to do it using let like this :

\begin{tikzpicture}
\coordinate (A) at (2,3);
\path let \p1=(A) in node{\x1};
\end{tikzpicture}


And if you want it as a number (or in cm) you can see this answer.