LaTeX forum ⇒ Math & ScienceHow do I use a loop to generate a set of related control sequences

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
jlettvin
Posts: 4
Joined: Mon Oct 15, 2018 12:03 am

How do I use a loop to generate a set of related control sequences

Postby jlettvin » Mon Oct 15, 2018 12:11 am

This loop works beautifully for extracting data from the \Element text block.
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?

  1. \def\Element{
  2. 1 / H / Hydrogen / + / 1+,
  3. 11 / Na / Sodium / + / 1+,
  4. 17 / Cl / Chlorine / - / 1-,
  5. 19 / K / Potassium / + / 1+,
  6. 20 / Ca / Calcium / ++ / 2+,
  7. }
  8.  
  9. \foreach \Z/\atom/\name/\charge/\ions in \Element{
  10. \global\expandafter\def\csname ion\trimspaces\atom\endcsname{${\trimspaces\atom}^{\trimspaces\charge}$}
  11. \immediate\write16{def{ion\trimspaces\atom}{\csname ion\trimspaces\atom \endcsname}}
  12. }
  13.  
  14. % Without using \ionH in the text, the output looks like this:
  15. % def{ionH}{${H}^{+}$}
  16.  
  17. % With using \ionH like below, the following error is generated:
  18. %! Undefined control sequence.
  19. %<argument> \atom
  20. %
  21. %l.3 \ionH
  22.  
  23. % uncomment the next line to see the error.
  24. % \ionH

jlettvin
Posts: 4
Joined: Mon Oct 15, 2018 12:03 am

Postby jlettvin » Mon Oct 15, 2018 8:34 pm

The answer was to use xdef. :roll:

  1. % xdef defines the new control sequence in the global dictionary.
  2. \foreach \Z/\atom/\name/\charge/\ions in \Element{
  3. \expandafter\xdef\csname
  4. ion\trimspaces\atom\endcsname{${\trimspaces\atom}^{\trimspaces\charge}$}
  5. \immediate\write16{def{ion\trimspaces\atom}{\csname ion\trimspaces\atom \endcsname}}
  6. }

User avatar
Stefan Kottwitz
Site Admin
Posts: 9413
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Tue Oct 16, 2018 10:42 am

Hi,

welcome to the forum!

I would use \gdef or \global\def. Here it may not matter, it's just that \xdef is equivalent to \global\edef and \edef expands the arguments at running time.

Stefan
Site admin

jlettvin
Posts: 4
Joined: Mon Oct 15, 2018 12:03 am

Postby jlettvin » Tue Oct 16, 2018 6:16 pm

Hi Stefan,

I have so many questions, it is hard to limit myself to the few that are most challenging.
In this case, I replaced xdef with gdef (which I had tried earlier) and this code failed.
I went back to xdef and it worked again. If you have a better alternative to xdef I will try it.
But, for now, xdef actually works and I don't see where argument expansion is a problem.
I have needs for other automated control sequence construction from data arrays.
Getting this right and re-using a "best" solution would be more satisfying.
So, what would you recommend?

Stefan Kottwitz wrote:Hi,

welcome to the forum!

I would use \gdef or \global\def. Here it may not matter, it's just that \xdef is equivalent to \global\edef and \edef expands the arguments at running time.

Stefan

rais
Posts: 220
Joined: Sun Nov 16, 2014 8:51 pm

Postby rais » Tue Oct 16, 2018 9:42 pm

Why, what's best depends on the context.
In the case above, \gdef isn't enough, because you've put two local variables into the argument of your constructed variable, in this case \atom and \charge, both of which are only valid inside this \foreach statement. That's why \xdef works here (because of its argument expansion, both of \atom and \charge get expanded) and \gdef doesn't work (because both of \atom and \charge are left in your constructed variable's definition, so outside this \foreach statement's, those two are lost---hence the error about \atom).
Which could lead to weird output, if you (or some package) globally define \atom and \charge somewhere (then you won't get an `undefined control sequence' error, but the result will most probably be wrong anyway without expansion).
OTOH, if the context changes, so may my answer ;)

KR
Rainer

jlettvin
Posts: 4
Joined: Mon Oct 15, 2018 12:03 am

Postby jlettvin » Wed Oct 17, 2018 8:24 pm

So, to be brutally clear,
are you saying that my code is a competent solution in this context?
If so, thank you for your explanations.
Otherwise, I need an alternative that would meet with approval.


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 1 guest