6

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?

7

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 Apr 25 '14 at 12:23
  • Nevermind, with \pgfmathsetmacro it works. – Ingo Apr 25 '14 at 12:26
2

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}

enter image description here

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

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.