[Tex/LaTex] Incomplete \iffalse using pgfmath naming a TikZ node


In trying answer Is it possible to use a macro to name a TikZ node?, I attempted to use the solution provided at pgfmath expansion – call a command from within a pgfmath environment, but this yielded the error message

Incomplete \iffalse; all text was ignored after line 67.
<inserted text>

from the example


    \pgfmathparse{#2 + \mymathresult}%

    \node (0) at (0,0) {X};
    \node (\mymacro{2}{3}) at (1,2) {Y};
    \draw (\mymacro{2}{3}) -- (0);
    \draw (5) -- +(3,0) {Z};

Best Answer

The problem in pgfmath expansion - call a command from within a pgfmath environment is the same as here: a \pgfmathparse command, which is not expandable, is being used somewhere where it wants to be expanded. In that case it is within another \pgfmathparse. In this case, the real problem is in the node name itself. The solution avoids the expansion issue by first processing the macro outside an "expansion context" (not sure if that's the right term! I mean that it is in the normal run of TeX and not in an \edef.) and then using the result of that in the expansion place. Thus:


fails but the solution is to do:


That solution would work here as well, but its implementation would be:


% You wait.  Time passes.  Thorin sits down and starts singing about gold.

\node (\nodename) at (1,2) {Y};

The point here is that we have to avoid putting anything unexpandable in the brackets. So all the \pgfmathparse routines have to be executed before we get to that point. No matter how you dress it up, \node (\mymacro{2}{3}) executes \mymacro in the \edef that sets the node name and so all the workings of \mymacro are evaluated at that point.

To do it all in one step would require an expandable version of \pgfmathparse. For particular circumstances, such as that in the original question, one can use the integer routines from LaTeX3 (texdoc source3).