Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer

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}}
share|improve this answer

Your Answer

 
discard

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.