Tikz/PGF has an extremely useful implementation of \foreach, which allows for instance the construction
\foreach \x / \y in {1/a,2/b,3/c,4/d} {\x is a lot like \y}
in which you can iterate over a pair of lists. This command only works within a tikz environment, so in particular doesn't work in the preamble, where I want to use it for defining a huge number of very similar looking macros, with say \newcommand{\task\x}{\function\y}; (really I'm defining new shapes in tikz, each of which is fifty or more lines of code, so just repeating it all a few hundred times is genuinely impractical).
Is there a function in tex that will easily do something similar? I am aware that I could hack together this functionality using ForArray functions or nested forloops, but the results seemed like they would be comparatively inelegant, and I hoped someone here might have a clean approach (or a clean implementation using arrays or loops).
\foreachdoesn't have to be in atikzpicture, it also works in the preamble. The problem is that you won't be able to define new commands inside the loop, as each iteration is executed inside a local group, so definitions won't be global. Note also that you have the syntax wrong, it should bein {1/a,2/b,3/c,4/d}. Maybe you could add some more detail about what kinds of shapes you're trying to define? I'm sure that if they can be defined in a for-loop, they can also be parametrised, which would probably be a better approach. – Jake May 18 '11 at 14:50\gdefor say\newcommand\foo{...}\global\let\foo\foo, but in both cases you would need to expand\xand\ybefore the definition. – Martin Scharrer May 18 '11 at 14:53\foreach, take a look at the answers to this question of mine: http://tex.stackexchange.com/q/15204/86 – Andrew Stacey May 18 '11 at 18:00