Use "default" figure if file is missing?
Try the standard command \IfFileExists
. It has three arguments: the file name, what to do if it exists, and what to do if it does not:
\IfFileExists{scaling.pdf}{\includegraphics{scaling.pdf}}{\includegraphics{dummy.pdf}}
Of course, you can add syntactic sugar to this:
\newcommand{\includegraphicsmaybe}[1]{\IfFileExists{#1}{\includegraphics{#1}}{\includegraphics{dummy.pdf}}}
The last macro called within graphicx
) before including the image is \Gin@ii
. Due to the structure of \Gin@ii
, it is possible to patch this command and temporarily remove LaTeX error-producing capability. Here's a minimal example:
\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\newcommand{\noimage}{%
\setlength{\fboxsep}{-\fboxrule}%
\fbox{\phantom{\rule{150pt}{100pt}}}% Framed box
}
\makeatletter
\patchcmd{\Gin@ii}
{\begingroup}% <search>
{\begingroup\renewcommand{\@latex@error}[2]{\noimage}}% <replace>
{}% <success>
{}% <failure>
\makeatother
\begin{document}
\includegraphics[width=150pt]{tiger} \par
\includegraphics[width=150pt]{tigers}
\end{document}
In the above example, the command \noimage
is used to represent the output that is generated when no image exists. You could, for example, define \noimage
using
\newcommand{\noimage}{\includegraphics{dummy}}
if you wish to include dummy
instead of my 150pt x 100pt empty rectangle. The redefinition of \@latex@error
(which takes 2 arguments that is gobbles and replaces with \noimage
) occurs inside a group, making it local and is therefore reverted back after \Gin@ii
finishes.
Here is the final macro called within \Gin@ii
called \Ginclude@graphics
; I've highlighted the part that is indirectly affected by the redefinition of \@latex@error
:
\def\Ginclude@graphics#1{%
\begingroup
\let\input@path\Ginput@path
\filename@parse{#1}%
\ifx\filename@ext\relax
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase\Gin@temp
\fi}%
\else
\Gin@getbase{\Gin@sepdefault\filename@ext}%
\ifx\Gin@ext\relax
\@warning{File `#1' not found}%
\def\Gin@base{\filename@area\filename@base}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
\ifx\Gin@ext\relax
\@latex@error{File `#1' not found}% <----------------------------- MODIFIED
{I could not locate the file with any of these extensions:^^J% <-- MODIFIED
\Gin@extensions^^J\@ehc}% <-------------------------------------- MODIFIED
\else
\@ifundefined{Gin@rule@\Gin@ext}%
{\ifx\Gin@rule@*\@undefined
\@latex@error{Unknown graphics extension: \Gin@ext}\@ehc
\else
\expandafter\Gin@setfile\Gin@rule@*{\Gin@base\Gin@ext}%
\fi}%
{\expandafter\expandafter\expandafter\Gin@setfile
\csname Gin@rule@\Gin@ext\endcsname{\Gin@base\Gin@ext}}%
\fi
\endgroup}
The advantage with this approach is that you don't have to modify any of your existing macro definitions, like \includegraphics
. It would be possible to extend this to indicate the offending (missing) file as well.
Based on the other answers, I have come up with the following code to ignore missing image files in a latex document.
The advantage of this answer over the others is that it deals with the optional arguments of \includegraphics
, it doesn't require a dummy.pdf file and it doesn't require changing any of the \includegraphics
commands in the body of the document.
% deal with missing images which are not directly included in the repository
\newcommand{\noimage}{%
\setlength{\fboxsep}{-\fboxrule}%
\fbox{\phantom{\rule{10pt}{10pt}}File missing\phantom{\rule{10pt}{10pt}}}% Framed box
}
\let\includegraphicsoriginal\includegraphics
\renewcommand{\includegraphics}[2][width=\textwidth]{\IfFileExists{#2}{\includegraphicsoriginal[#1]{#2}}{\noimage}}