# Skip foreach-frame when missing content

I automatically generate slides with foreach:

\foreach \n in {1,...,200} {%
\begin{frame}{Slide with jpeg number \n}
\includegraphics[height=3cm]{FileA_\n.jpg}
\includegraphics[height=3cm]{FileB_\n.jpg}
\end{frame}%
}


The .jpg's come from another source, and sometimes a few of them are missing which causes the whole pdf-generation to break: "Package pdftex.def Error.File 'FileB_17.jpg' not found".

If any of the two .jpg dont exist, I would like that the slide it belongs to will be skipped but that the compilation continues with the rest of the slides. How can I program that in LaTex?

I work in RStudio with knitr.

-

You can use \IfFileExists conditional within the loop

\documentclass{beamer}
\usepackage{tikz}
\usepackage{mwe} %<-- For dummy images
\begin{document}
\foreach \n in {a,...,e,a,b} {%
\IfFileExists{example-image-\n.jpg}{
\begin{frame}{Slide with prefix \n}
\includegraphics[height=3cm]{example-image-\n}
\end{frame}%
}{}
}
\end{document}


In the mwe package there are only a,b,c example images so the loop skips d,e images.

-

I don't know about missing the frame but you can avoid the error with

\IfFileExists{FileA_\n.jpg}}
{\includegraphics{FileA_\n.jpg}}}
{\typeout{missing: FileA_\n.jpg}}

-