# Use a variable as a coordinate in pgfplots

I have the following code:

\begin{tikzpicture}
\begin{axis}[domain=0:40]
\def\a{\pgfmathparse{1+1}\pgfmathresult}
%\draw (\a,20)--(axis cs:20,0);
\node at (axis cs:30,30) {\a};
\end{axis}
\end{tikzpicture}


The node gives me the correct output, while the draw command won't work as expected. Why? (Uncommenting \draw breaks the code.)

-

The \pgfmathparse macro is not expandable and can't be used inside coordinates. Placing \pgfmathresult behind it, inside another macro like \a or on its own doesn't change anything on that. You need to place the result, like \pgfmathresult, which is expandable in the coordinates. However \pgfmathresult is overwritten by any internal math operation and therefor should not be used! Either use \let\a=\pgfmathresult (the = is optional) or \pgfmathsetmacro{\a}{1+1} (recommended) as already posted by you and Jake, respectively, or \edef\a{\pgfmathresult} to store the current value of \pgfmathresult into \a which then can be used safely inside an coordinate.
The reason why your original \a works inside the node content ({\a}) is that this part is typeset as a box. The content of a box do not need to be fully expandable, but is executed, i.e. the \pgfmathparse is executed as normal and then \pgfmathresult is typeset right away before it can be changed by any other code.
\pgfmathparse{1+1}\let\a=\pgfmathresult