However, the function definitions made within the loop disappear when the loop finishes.
Is there a way to make the newly defined control sequences permanent globally?
Code: Select all
\def\Element{
1 / H / Hydrogen / + / 1+,
11 / Na / Sodium / + / 1+,
17 / Cl / Chlorine / - / 1-,
19 / K / Potassium / + / 1+,
20 / Ca / Calcium / ++ / 2+,
}
\foreach \Z/\atom/\name/\charge/\ions in \Element{
\global\expandafter\def\csname ion\trimspaces\atom\endcsname{${\trimspaces\atom}^{\trimspaces\charge}$}
\immediate\write16{def{ion\trimspaces\atom}{\csname ion\trimspaces\atom \endcsname}}
}
% Without using \ionH in the text, the output looks like this:
% def{ionH}{${H}^{+}$}
% With using \ionH like below, the following error is generated:
%! Undefined control sequence.
%<argument> \atom
%
%l.3 \ionH
% uncomment the next line to see the error.
% \ionH