0

I have a piece of code where I'm iterating over a list of items, but I do this in several places and would like to avoid having to redefine that list every time. This is what I have working:

\foreach \f in {193, 87, 88, 85, 80, 81, 79, 89}{                                      
     \includegraphics{\f.png}  
} 

I'd like instead be able to something like this:

\foreach \f in \allfs{                                      
     \includegraphics{\f.png}
}   

Is there a way to define \allfs such that I can iterate over it? I tried just doing a \newcommand\allfs{{193, 87, 88, 85, 80, 81, 79, 89}} but that doesn't work. It instead looks for the file {193, 87, 88, 85, 80, 81, 79, 89}.png

Sorry if this is a duplicate question, I was having trouble figure out the right way to phrase things to search

anf3
  • 1

1 Answers1

0

Instead of \foreach I would rather use the /.list key, which avoids the need for a macro like \f as loop variable and avoids groups. In this context, it is particularly convenient because it can be paired with the /.expanded key.

\documentclass{article}
\usepackage{pgffor}
\usepackage{graphicx}
\begin{document}
\newcommand\allfs{{a,b,c,duck}}
\pgfkeys{include graphics/.code={\includegraphics{example-image-#1.pdf}\par}}% \par is only added for this example
\pgfkeys{include graphics/.list/.expanded=\allfs}
\end{document}