It seems like you're supplying an image file extension by default. This is helpful, since graphics
handles this differently: If you [don't] supply an image extension and the file doesn't exist, it only creates a warning [error]. That makes a difference in terms of compilation, since a warning is not critical.
Therefore, I suggest using
\usepackage[draft]{graphicx}
Here's a minimal example:
\documentclass{article}
\usepackage[draft]{graphicx}
\begin{document}
\includegraphics[width=100pt]{some-bizarre-image.png}% This does not exist
\includegraphics[width=150pt,draft=false]{example-image.png}% This exists (http://ctan.org/pkg/mwe)
\end{document}
If you're not supplying the image file extension by default, then one may have to update \includegraphics
to search for a possible file to include. Assuming you're compiling with pdfLaTeX, we can cycle through the possible image file extensions:
\documentclass{article}
\usepackage[draft]{graphicx}
\usepackage{pgffor,letltxmacro}
% https://tex.stackexchange.com/q/72930/5764
% .png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2
\newif\iffilefound
\LetLtxMacro\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\global\filefoundfalse
\foreach \fext in {,.png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2} {%
\iffilefound\else\IfFileExists{#2\fext}{\global\filefoundtrue\xdef\imgfile{#2\fext}}{}\fi%
}%
\iffilefound
\oldincludegraphics[#1]{\imgfile}%
\else
\oldincludegraphics[#1]{example-image}%
\fi
}
\begin{document}
\includegraphics[width=100pt]{some-bizarre-image}% This does not exist
\includegraphics[width=150pt,draft=false]{example-image}% This exists (http://ctan.org/pkg/mwe)
\end{document}
We cycle through all possible extensions (including no extension at all, if you did supply that manually as part of \includegraphics
) and identify the first available extension found as the combination \imgfile
. We insert the image of a made-up (but existing) .png
filename if the image doesn't exist.
\IfFileExists{foo.png}{\includegraphics{foo.png}{no foo today}
?