This is an expansion problem.
That said, I can give you an introduction into the expansion control of TeX - which is what you are doing here.
While I write this, I have two other questions in mind: the first is asking myself if there is some kind of feature request behind all that. Perhaps the whole handling of programmatical loops inside of an axis needs to be thought through by an expert (i.e. by me as author of pgfplots). Obviously, the special requirements of the axis cause confusion (and you are not the first to suffer from it).
The second is: are you interested in learning TeX to be able to script things? Loops are "scripts". And, unfortunately, require special handling inside of an axis as you already knew. If so, my answer below might be adequate.
If you are not really interested in this sort of scripting (and you are only doing it for this elementary problem at hand), then I might point out the nodes near coords feature, combined with an \addplot3 table and/or \addplot3 {<expression>} might do what you need - without resorting to scripts, only by means of high-level methods.
I am coming back to your question as such now:
SOLUTION 1
Since your loops actually only add annotations and no plots, you can postpone the complete processing to the visualization phase.
This is what happens if your write \draw or \node inside of an axis: pgfplots automatically collects the (unexpanded) arguments up to the next semicolon ; and postpones the execution until the visualization phase.
The \edef construction is necessary to mix visualization phase and survey phase of the axis (survey == involves plots where limits need to be updated etc).
Pgfplots offers a simple approach to postpone things to the visualization phase:
\pgfplotsextra{<material which is postponed to the visualization phase>} .
Within this material, you can use \foreach as in TikZ - without all that \edef, \noexpand, etc:
\pgfplotsextra{
\foreach \i in {-2.4,-1.2,...,2.4}
{\foreach \j in {-2.4,-1.2,...,2.4}
{\node at (axis cs:\i,\j) {\pgfmathparse{\i+\j}\pgfmathprintnumber{\pgfmathresult}};}
}
}
Note that I added \pgfmathprintnumber here.
SOLUTION 2
To fix your text, get an answer for the immediate question, and learn something about TeX scripting, proceed as follows:
The solution is to add \noexpand in front of all macros inside of your \edef construct -- unless you explicitly know that the value should be expanded immediately.
The correct solution is
\foreach \i in {-2.4,-1.2,...,2.4}
{\foreach \j in {-2.4,-1.2,...,2.4}
{\edef\temp{\noexpand \node at (axis cs:\i,\j) {\noexpand\pgfmathparse{\i+\j}\noexpand\pgfmathresult};}
\temp}}
And here is the explanation:
\edef means "expanded definition". It defines \temp to contain the complete expansion of everything that follows - where "complete expansion" means to expand everything until it cannot be expanded further.
In TeX, there is a distinction between "expandable" and "unexpandable" material. Learning to decide which is what seems difficult and technical - but there is a very simple heuristics which is true in 99% of the cases: if something is "executable", you can be sure that it cannot be expanded. If something "simply contains a value", you can almost surely expand it.
Here, \node and \pgfmathparse are both executable. Consequently, they cannot be expanded - and need to be protected from expansion. Omitting the protection will cause unanticipated and unpredictable results (as in your case).
On the contrary, \i and \j are single values - and can be expanded (that was the whole idea here).
- "Protecting something from expansion inside an
\edef" can be accomplished by prefixing the macro in question with \noexpand.
that explains the \noexpand\node and \noexpand\pgfmathparse.
- Finally, you have to add
\noexpand\pgfmathresult as well -- because the \pgfmathresult should be the result of your \pgfmathparse, and not the current value of \pgfmathresult.
Ah - you may want to consider using \noexpand\pgfmathprintnumber{\noexpand\pgfmathresult} here as in Solution 1.
See also http://pgfplots.sourceforge.net/TeX-programming-notes.pdf for details on TeX programming.
\pgfplotsforeachungroupedfor different types offoreachcommands. – percusse Apr 07 '12 at 18:28\pgfmathparsewe can probably help you with the error there. A typical error is that you have to use\pgfmathresultafterwards:\pgfmathparse{1+2}\pgfmathresult->3. – nickpapior Apr 07 '12 at 20:31