Help with looping through a folder to insert all images within

for loopforeachgraphicsloops

I have a folder within overleaf of images, which I'd like to include as figures in the document.
However, I'm having some trouble with calling the "for" loop. May I ask for some assistance in finding what I've done wrong please?

I get the following error:

Package pdftex.def Error: File `figures/Test/ AFCO 01-JAN-19.png

and it lists all the files. I believe its caused by the space between the "Test/" and the file name, although I'm uncertain of how to fix this!

\newcommand*{\samples}
{AFCO 01-JAN-19.png,AFCO 02-NOV-18.png,AFCO 03-APR-19.png,AFCO 03-JAN-19.png,%
AFCO 03-OCT-18.png,AFCO 05-DEC-18.png,AFCO 05-JUL-18.png,AFCO 05-MAR-19.png,% 
AFCO 05-NOV-18.png,AFCO 06-FEB-19.png,AFCO 09-JAN-19.png,AFCO 12-DEC-18.png,% 
AFCO 13-DEC-18.png,AFCO 14-MAR-19.png,AFCO 14-OCT-18.png,AFCO 17-DEC-18.png,% 
AFCO 24-NOV-18.png,AFCO 27-DEC-18.png,AFCO 27-NOV-18.png,AFCO 28-FEB-19.png,% 
AFCO 30-DEC-18.png,AFCO 30-JAN-19.png,AUS 100 ##1.png,BLUESCOPE.png,
BUSTY BLEND #1.png,BUSTY FINES ##2.png,BUSTY IN BASE #1.png,%
COKE BREEZE BLEND ##1.png,COLOMBIAN.png,FYF.png,%
IJmuiden 50CRI.png,MORFA,02-FEB-21.png,MORFA 14-JAN-21.png,%
MORFA 16-JAN-21.png,MORFA 18-JAN-21.png,MORFA 21-JAN-21.png,MORFA 29-JAN-21.png,% 
MORFA FLOAT 02-FEB-21.png,MORFA FLOAT 23-02-21.png,MORFA FLOAT 3-FEB-21.png,% 
MORFA FLOAT 27-JAN-21.png,MORFA FLOAT 28-JAN-21.png,MORFA FLOAT 30-JAN-21.png,% 
MORFA GROUND 1.png,MORFA GROUND 2.png,MORFA GROUND 3.png,MORFA GROUND 4.png,% 
MORFA GROUND 5.png,MORFA GROUND 6.png,MORFA GROUND 7.png,MORFA SINK 23-FEB-21.png,% 
MORFA SINK 24-FEB-21.png,MORFA SINK 27-JAN-21.png,MORFA SINK 28-JAN-21.png,%
MORFA SINK 30-JAN-21.png,MORFA WK 1.png,MORFA WK 39.png,MORFA WK 4.png,%
MORFA WK 8.png,POLISH 100 #2.png,REF 3 BLEND 2.png,RISUN 10-JUL-18.png,%
RISUN ERINI P.png,US LV 50-50.png,US MV.png}
\foreach \x in {\samples}
  {\includegraphics[width=20mm]{figures/Test/\x}}

Best Answer

I don't know your concrete set up since you only provided a code snippet. But actually you can keep the line breaks and you don't even need to delete spaces, you only need to take care about whether or not to use braces around the macro in the \foreach loop.

Here, no additional braces are needed. In fact, if you add them, the \foreach loop will only see one item (with a very long and very wrong file name).

Something like this should work:

\documentclass{article}
\usepackage{graphicx, pgffor}

\newcommand*{\samples}
{
example-image-a,
example-image-b,
example-image-c
}

\begin{document}

\foreach \x in \samples
  {\includegraphics[width=20mm]{\x}}

\end{document}

enter image description here

Related Question