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 was recently in charge of organizing all of the references and labels in a very large scientific proposal document. My workflow typically involved compiling the document, seeing a "There were undefined references." statement, and then searching the resultant PDF file for the usual "?" or "??."

Sometimes those missing references were in regard to labels for figures that were used throughout the text and not something in our bibliography. It got me wondering:

Is it possible to have a "dummy" or "default" figure to use in \includegraphics statements if the requested file is missing?

Perhaps a use case would be if in the LaTeX document there were a command such as \includegraphics{scaling.pdf}, but scaling.pdf doesn't exist in the current directory, so we use the existing dummy.pdf automatically. I assume this involves renewcommand'ing the includegraphics command, but how should one go about doing it for this scenario?

share|improve this question
Just to say that it can be useful, to avoid error due to filename with underscore, to add \detokenize in the message box for missing file : \newcommand{\includegraphicsmaybe}[1]{\IfFileExists{#1}{\includegraphics{#1}} {\makebox[0pt]{\detokenize{File #1 is missing}}}} – user51717 May 14 '14 at 14:40
up vote 36 down vote accepted

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:


Of course, you can add syntactic sugar to this:

share|improve this answer
Great, exactly what I was looking for! – cm2 Jan 4 '12 at 1:50
instead of (or in addition to) using a dummy figure, it would be informative to show the name of the missing figure in the output. – barbara beeton Jan 4 '12 at 13:41
@barbarabeeton: Indeed! My next step was going to write a tikz code that would write the filename overtop of the image. – cm2 Jan 4 '12 at 17:08
You do not need tikz for this; just put in the "else" part of \IfFileExists 'File #1 is missing`. – Boris Jan 4 '12 at 17:28
You can have both text and image, for example,\newcommand{\includegraphicsmaybe}[1]{\IfFileExists{#1}{\includegraphic‌​s{#1}}{\makebox[0pt]{File #1 is missing}\includegraphics{dummy.pdf}}} – Boris Jan 4 '12 at 17:47

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:

enter image description here

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
  \fbox{\phantom{\rule{150pt}{100pt}}}% Framed box

  {\begingroup}% <search>
  {\begingroup\renewcommand{\@latex@error}[2]{\noimage}}% <replace>
  {}% <success>
  {}% <failure>

\includegraphics[width=150pt]{tiger} \par

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


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:

       \@warning{File `#1' not found}%
         \@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
            \@latex@error{Unknown graphics extension: \Gin@ext}\@ehc
             \csname Gin@rule@\Gin@ext\endcsname{\Gin@base\Gin@ext}}%

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.

share|improve this answer
Actually it is better to patch \Gin@getbase command. In one of my (yet unpublished) packages I patched it to select print version of a graphics for print version, web version of graphics for web version and the default version if the special version did not exist. – Boris Jan 4 '12 at 15:55
@Werner: Thanks for this alternative approach. Although it is more involved, I do like that fact that I don't have to modify the \includegraphics macro. – cm2 Jan 4 '12 at 17:10
@cm2: Remember, the only code required to do this is everything between \makeatletter and \makeatother. Showing \Ginclude@graphics at the end of my answer was just an illustration of what \patchcmd modifies within the command. I don't think it's that much more involved... – Werner Jan 4 '12 at 17:20
@Werner: Indeed, you are correct. I guess by "more involved," I simply meant that you had to modify some internal functionality. The amount of work involved is similar. – cm2 Jan 4 '12 at 17:41

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.