Is it possible to use a macro to name a TikZ node?

Is it possible to use a macro to name and reference TikZ nodes. For example:

\begin{tikzpicture}
\node (0) at (0,0) {X};
\node (\mymacro{2}{3}) at (1,2) {Y};
\draw (\mymacro{2}{3}) -- (0);
\end{tikzpicture}


Where \mymacro is (for example):

\newcount\counterA
\def\mymacro#1#2%
{\counterA=#1\relax%
\the\counterA}


The actual calculation that my macro computes uses \loop\ifnum (which I think) means I cannot use the solution in Modular arithmetic on node names in TikZ? without defining a new \pgfmathMyMacro function.

Is this a limitation of TikZ or is there some \edef or \expandafter black magic I could use?

-
Never put \newcount in a definition of this kind: you are reassigning every time the same name to a new counter register. It's difficult to say something more, you should offer more details about what you're trying to do. –  egreg Sep 7 '11 at 13:25
I am trying to compute a number and then use the number as the name for a TikZ node. I hoped that how the macro is implemented would not matter. I have implemented the function in several different ways, such as recursive macro definitions with edef, TikZ maths, and Tex counters (as shown here). None worked. However <code>\def\myothermacro#1#2{#1AAA#2}</code> works fine I must be doing something bad with Tex. –  Niall Murphy Sep 7 '11 at 14:50

The problem is due to expansion. TikZ names the node via an \edef so your macro for determining the node has to be expandable in an \edef. I don't know the exact rules of what is and isn't expandable, but your macro using \count is not expandable. You can see this by doing:

\def\test#1#2{%
\countA#1
\the\countA}
\edef\testit{\test{2}{3}}
\show\testit


The result is:

> \testit=macro:
->\countA 2 \advance \countA by 3 0.


Anything using \pgfmathparse is also not expandable, and I'd guess that whatever else you tried was not expandable.

Fortunately, those LaTeX3 gurus are masters of expansion and their integer stuff seems to be expandable. Thus:

\usepackage{expl3}
\ExplSyntaxOn
\def\mymacro#1#2%
{\int_eval:n {#1 + #2}}
\ExplSyntaxOff

\edef\testit{\mymacro{2}{3}}
\show\testit


Result:

> \testit=macro:
->5.


And, indeed, the following works. The node is correctly labelled (5).

\documentclass{article}
%\url{http://tex.stackexchange.com/q/27769/86}
\usepackage{expl3}
\usepackage{tikz}

\ExplSyntaxOn
\newcount\countA
\def\mymacro#1#2%
{\int_eval:n {#1 + #2}}
\ExplSyntaxOff

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

-
Great, nice to know! In one implementation I built the answer in binary with \pretocmd and \edef which worked. Now I know why converting to decimal with \pgfmathbasetodec broke it. I see lots of interesting stuff in expl3. Thanks. –  Niall Murphy Sep 9 '11 at 12:11

This seems to work:

\documentclass{article}
\usepackage{tikz}

\newcommand*{\mymacro}[2]{#1+#2}%

\begin{document}
\begin{tikzpicture}
\node (0) at (0,0) {X};
\node (\mymacro{2}{3}) at (1,2) {Y};
\draw (\mymacro{2}{3}) -- (0);
\end{tikzpicture}
\end{document}


As Andrew mentions, this unfortunately results in the node being named 2+3 and not 5. To fix this, I attempted to use the solution from this question on pgfmath expansion - call a command from within a pgfmath environment as follows:

\newcommand{\mathresult}[1]{%
\pgfmathparse{#1}\pgfmathsetmacro\mymathresult{\pgfmathresult}%
}
\newcommand{\mymacro}[2]{%
\mathresult{#1}%
\pgfmathparse{#2 + \mymathresult}%
\pgfmathsetmacro\mymathresult{\pgfmathresult}\mathresult%
}%


but this results in a Incomplete \iffalse when it is used. I simplified the above as much as I could but could not eliminate that error message.

See follow up question: Incomplete \iffalse using pgfmath naming a TikZ node which shows that you'll need to be careful in only choosing integer values, as decimal values do not work as labels.

-
But the node is called 2+3, not 5! (If you add the line \draw (5) -- +(3,0); then it complains about the name.) –  Loop Space Sep 7 '11 at 15:38