TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I automatically generate slides with foreach:

\foreach \n in {1,...,200} {%
  \begin{frame}{Slide with jpeg number \n}

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.

share|improve this question
up vote 8 down vote accepted

You can use \IfFileExists conditional within the loop

\usepackage{mwe} %<-- For dummy images
\foreach \n in {a,...,e,a,b} {%
  \begin{frame}{Slide with prefix \n}

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

share|improve this answer

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

{\typeout{missing: FileA_\n.jpg}}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.