Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the following code:

\begin{tikzpicture}
  \begin{axis}[domain=0:40]
    \addplot {x};
    \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.)

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

I can't really say why, but the following works fine:

\pgfmathparse{1+1}\let\a=\pgfmathresult
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.