2

Consider this MEW,

\nonstopmode
\documentclass{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}                                       
    \draw [gray!10](-0.2, -0.2) grid[step=.25] (3.2,3.2) ;
    \draw [gray!25](-0.2, -0.2) grid[step=.5]  (3.2,3.2) ;
    \path
        (0, 3) node[left]  {$y$}
        (3, 0) node[below] {$x$} ;
\draw [-latex](-.2, 0) -- (3,0);
\draw [-latex](0, -.2) -- (0,3);

\draw [thick, cyan!50!black, -latex]
(0,0)    node [coordinate](origin){}
-- 
(60:2.5) node [coordinate](pa){}
         node [above, black]{$p_a$}
(origin)
--
(30:2.5) node [coordinate](pb){}
         node [above, black]{$p_b$};

\newdimen\pax
\newdimen\pay
\pgfextractx{\pax}{(pa)}
\pgfextracty{\pay}{(pa)}
\draw [yellow!75!black](pa) -- (\pax, 0);
\draw [yellow!75!black](pa) -- (0, \pay);
\draw [magenta](pb) -- (\pax, 0);
\draw [magenta](pb) -- (0, \pay);

\end{tikzpicture}
\end{document}

which produces this picture below:

pictures

At \pgfextractx{\pax}{(pa)}, I am trying to extract the x component of the node coordinate (pa) (same with y component), and trying to draw perpendicular with yellow lines to their respective axes. But pgfextractx is giving me the x/y component of node coordinate (pb), which I have drawn with the magenta lines.

How can I extract the correct x/y values from the node coordinate (pa) with \pgfextractx? I only looked at pgfmanual.pdf which doesn give a lot of examples of \pgfextracttx.

sigsegv
  • 583
  • 1
    Please extend your example to a full minimal document others can copy and test as is. – daleif Jan 31 '24 at 09:53
  • 1
    Replace \pgfextractx{\pax}{(pa)} with \pgfextractx{\pax}{\pgfpointanchor{pa}{center}}. You are not in the frontend layer here, so you cannot use the parentheses syntax. – Jasper Habicht Jan 31 '24 at 10:00
  • Note that \pgfextractx does not do wht you think it does, see https://tex.stackexchange.com/a/273746/3929 for an alternative. A point in this context is probably a low level pgf point not something named as (pa). I'm guessing you're getting something like this in your log: Missing character: There is no ( in font nullfont! which is an indicator that something is wrong. – daleif Jan 31 '24 at 10:01
  • @daleif Yes, I was wondering what that Missing character error was. – sigsegv Jan 31 '24 at 10:25

1 Answers1

4

You are in the basic layer of PGF which does not parse the syntax like the frontend layer (aka TikZ) does. In particular, the syntax using parentheses to denote a coordinate does not work here. Therefore, you need to use \pgfpointanchor{pa}{center} instead of (pa):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\newdimen\pax \newdimen\pay

\begin{document} \begin{tikzpicture} \draw[gray!10] (-0.2,-0.2) grid[step=.25] (3.2,3.2); \draw[gray!25] (-0.2,-0.2) grid[step=.5] (3.2,3.2); \path (0,3) node[left] {$y$} (3,0) node[below] {$x$} ;

\draw[-latex] (-.2,0) -- (3,0); 
\draw[-latex] (0,-.2) -- (0,3); 

\draw[thick, cyan!50!black, -latex] 
    (0,0) node[coordinate] (origin) {} 
    -- 
    (60:2.5) node[coordinate] (pa) {} 
        node[above, black] {$p_a$} 
    (origin) 
    -- 
    (30:2.5) node[coordinate] (pb) {} 
        node[above, black] {$p_b$}; 

\pgfextractx{\pax}{\pgfpointanchor{pa}{center}} 
\pgfextracty{\pay}{\pgfpointanchor{pa}{center}} 
\draw[yellow!75!black] (pa) -- (\pax,0); 
\draw[yellow!75!black] (pa) -- (0,\pay); 
\draw[magenta] (pb) -- (\pax,0); 
\draw[magenta] (pb) -- (0,\pay);

\end{tikzpicture} \end{document}

enter image description here

  • 1
    +1, but, please, move \newdimen outside the tikzpicture (best is in the preamble). In a standalone document this makes no difference, but in a standard document with several similar pictures it would. – egreg Jan 31 '24 at 10:24
  • @egreg why? A \newdimen in one tikzpicture environment leaks into another? – sigsegv Jan 31 '24 at 10:27
  • @egreg Thanks for the notice! – Jasper Habicht Jan 31 '24 at 10:27
  • @sigsegv \newdimen permanently allocates a register. So in case of multiple such calls inside tikzpicture you waste two registers per call. Not a big deal, because there are more than 32000 registers available, but it's conceptually wrong. – egreg Jan 31 '24 at 10:43