In python, and in most other programming languages I am familiar with, I can assign values to an array-like data structure, loop through each element and pass the element to a function on each iteration. Here's an example:
def f(x):
return "This is {}".format(x)
if __name__=="__main__":
x = ['param1', 'param2', 'param3']
for item in x:
print(f(item))
Running the above will print the following to standard output:
This is param1
This is param2
This is param3
There are several advantages to this:
f
oncef
Recently I have been playing around with plain TeX, and I understand that TeX is a macro oriented, expansion based language. The closest thing I could get to the above was this:
\def\f#1{This is #1 \par}
\f{param1}
\f{param2}
\f{param3}
\end
This is suboptimal because:
\f
multiple timesIs there anything I can do to refactor the above TeX code to something more DRY? How can I emulate an array like structure and a for each loop in Plain TeX?
In latex, you could use the pgffor
package:
\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \x in {param1, param2, param3}{
This is \x\par
}
\def\foo{param1, param2, param3}
\foreach \x in \foo {
This is \x\par
}
\end{document}