7

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?

2
  • Related Questions: Extract x, y coordinate of an arbitrary point in TikZ and Extract x value from coordinate in TikZ. 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

2 Answers 2

8

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}; 
2
  • 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? Commented Apr 25, 2014 at 12:23
  • Nevermind, with \pgfmathsetmacro it works. Commented Apr 25, 2014 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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.