# Include graphic if the file is present [duplicate]

I am trying to create a latex document that has a graphic: temp.pdf

\begin{figure}
\centering
\includegraphics[width=30cm,scale=1]{temp.pdf}
\end{figure}


This works fine if the graphic file is present, however if it is not, I get the text:

temp.pdf in the document.

How can I switch this off so that if temp.pdf is not present, then latex simply ignores the includegraphics command.

• – Werner Nov 28 '15 at 20:31

Redefine the command:

\documentclass{article}
\usepackage{graphicx}
\let\IG\includegraphics
\renewcommand\includegraphics[2][]{\IfFileExists{#2}{\IG[#1]{#2}}{\ignorespaces}}
\begin{document}

\includegraphics[width=3cm]{example-image.pdf}
\includegraphics[width=5cm]{somefile.pdf}
\includegraphics[width=3cm]{example-image-a.pdf}

\end{document}

• Thank you very much. I tried this, but I now get a blank page if there is no graphic. How can I remove this blank page? – adam.888 Nov 28 '15 at 20:43
• How should I know what you did? As you can see in my example there is no blank page, if you run it. – user2478 Nov 28 '15 at 20:47