I had a look at TeX-FAQ on Non-letters in macro names and tried to implement the approach #2. It looks like I'm missing something. When trying to refer later to the new commands the result is not what I expected.
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\newcommand{\DefineNode}[2]
{
\expandafter\newcommand\csname node-#1\endcsname{#2}
}
\newcommand{\GetNode}[1]{\csname node-#1\endcsname}
\begin{tikzpicture}
\def \intra {1}
\def \inter {3}
% Calculate values
\foreach \x [count=\xi from 0] in {a,...,c}
{
\pgfmathsetmacro\tmpi{\xi * \inter}
\DefineNode{n\x.0}{\tmpi}
\pgfmathsetmacro\tmpii{\xi * \inter + \intra}
\DefineNode{n\x.1}{\tmpii}
\message{\GetNode{n\x.0} ^^J}
\message{\GetNode{n\x.1} ^^J}
}
% Doing something with them later
\foreach \x in {a,...,c}
{
\message{\GetNode{n\x.0} ^^J}
\message{\GetNode{n\x.1} ^^J}
}
\end{tikzpicture}
\end{document}
The result is:
0.0
1.0
3.0
4.0
6.0
7.0
\node-na.0
\node-na.1
\node-nb.0
\node-nb.1
\node-nc.0
\node-nc.1
Instead of repeating numbers twice.
EDIT: Solved the group issue by forcing names to be globals.
\newcommand{\DefineNode}[2]
{
\globaldefs=1\relax
\expandafter\newcommand\csname node-#1\endcsname{#2}
}
Result currently:
0.0
1.0
3.0
4.0
6.0
7.0
6.0
7.0
6.0
7.0
6.0
7.0
Instead of repeating first 6 lines, now 6.0 and 7.0 are repeated. This is due to using only two macros \tmpi and \tmpii. How to solve this?
\foreachdefines a group? In this case the definition would be local to the body of\foreach. – Stephan Lehmke Jul 09 '14 at 14:12\globaldefs=1. Use\expandafter\gdef\csname node-#1\endcsname{#2}instead. – egreg Jul 09 '14 at 17:51It will produce:
– karu Jul 09 '14 at 18:16\globaldefs!\newcommandis a macro which does temporary assignments that don't need to be done globally. – Ulrich Diez Sep 30 '22 at 13:20