Check for a valid file before using \includegraphics
Second attempt
This here should do the same tests than \includegraphics
. That means if will give yes if the graphics/file exists and if the extension can be handled be the engine (so tiger.eps
will give different results with pdflatex
and latex
: (The code is from the definition of \Ginclude@graphics
in graphics.sty).
\documentclass[]{article}
\usepackage{graphicx}
\makeatletter
\newif\ifgraphicexist
\catcode`\*=11
\newcommand\imagetest[1]{%
\begingroup
\global\graphicexisttrue
\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
\global\graphicexistfalse
\def\Gin@base{\filename@area\filename@base}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
\ifx\Gin@ext\relax
\global\graphicexistfalse
\else
\@ifundefined{Gin@rule@\Gin@ext}%
{\global\graphicexistfalse}%
{}%
\fi
\ifx\Gin@ext\relax
\gdef\imageextension{unknown}%
\else
\xdef\imageextension{\Gin@ext}%
\fi
\endgroup
\ifgraphicexist
\expandafter \@firstoftwo
\else
\expandafter \@secondoftwo
\fi
}
\catcode`\*=12
\makeatother
\begin{document}
\imagetest{tiger}{Yes, \imageextension}{No, \imageextension}
\imagetest{tiger.eps}{Yes, \imageextension}{No, \imageextension}
\imagetest{fail}{Yes, \imageextension}{No, \imageextension}
\imagetest{failxxx.eps}{Yes, \imageextension}{No, \imageextension}
\imagetest{bib.bib}{Yes, \imageextension}{No, \imageextension}
\end{document}
it needs the full name of the file. You can scan the logfile for Imagefile
to get all missing images files.
\documentclass{book}
\usepackage{graphicx}
\newcommand\IG[2][]{\IfFileExists{#2}
{\includegraphics[#1]{#2}}
{\fbox{File #2 doesn't exist}%
\message{Imagefile #2 doesn't exist^^J}}}% write a message into the log file
\begin{document}
\IG{foo}
\IG[width=3cm]{tiger.pdf}
\end{document}
\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage{graphicx}
\newwrite\writer
\immediate\openout\writer=missing.txt\relax
\newcommand\IG[2][]{\IfFileExists{#2}
{\includegraphics[#1]{#2}}
{\immediate\write\writer{\unexpanded{#2\newline}}}}
\begin{document}
\IG{foo.ext}
\IG[width=3cm]{example-image-a.pdf}
\IG{goo.eps}
\IG{hoo.png}
\section*{Missing Graphics}
\immediate\closeout\writer
\input{missing.txt}
\end{document}