# avoid printing the filename when \includegraphics return a warning “file not found”

I am using \includegraphics within table like this:

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}

\begin{document}
\begin{table}
\centering
\begin{tabular}{ccc}
\includegraphics[width=.3\textwidth]{image1.png} &
\includegraphics[width=.3\textwidth]{image2.png} &
\includegraphics[width=.3\textwidth]{image3.png} \\
\includegraphics[width=.3\textwidth]{image3.png} &
\includegraphics[width=.3\textwidth]{image4.png} &
\includegraphics[width=.3\textwidth]{image3.png} \\
\end{tabular}
\end{table}
\end{document}


Latex usually throws a Warning Error that doesn't prevent to compile but prints the file name as the result, which leads to something like this :

It is usually really helpfull but in this particular case I would like to avoid that and just leave the cell blank. Is there a simple way to do it?

• Welcome to TeX.SE!. You can use {example-image}, or {example-image-a}, {example-image-b} instead of the file names. – Cragfelt Dec 12 '17 at 8:57
• Please check if you have the draft option enabled with the class or the graphicx package. With this option graphics are shown like this. – Martin Scharrer Dec 12 '17 at 9:29
• As i commented in another answer that has been deleted now, the draft option seems to behave similarly to the demo one: it removes all images. But I want to leave the cell blank if the file is not found and include the graphics if the file is found. To remove the extension as said in the answer I accepted (and i think it's also what @Cragfelt is suggesting) does just right. – Simon C. Dec 12 '17 at 9:45
• @SimonC. The answer you accepted does not work. It brings errors that cannot be avoided during compilation. And it is different of my suggestion. – Cragfelt Dec 12 '17 at 9:59
• When compiling with pdflatex it throws warnings that I can ignore and go through all the compilation (or just use the -interaction=nonstopmode). The final output is exactly what I want. It's true that it may not the most elegant answer as it throws lot of warning (and I am not sore how it will react if I use some complex filenames with points and space etc. Maybe your suggestion is better but I am not sur i understood it then. – Simon C. Dec 12 '17 at 10:12

If you remove the extension of the filename it should work as you want:

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}

\begin{document}
\begin{table}
\centering
\begin{tabular}{ccc}
\includegraphics[width=.3\textwidth]{image1} &
\includegraphics[width=.3\textwidth]{image2} &
\includegraphics[width=.3\textwidth]{image3} \\
\includegraphics[width=.3\textwidth]{image3} &
\includegraphics[width=.3\textwidth]{image4} &
\includegraphics[width=.3\textwidth]{image3} \\
\end{tabular}
\end{table}
\end{document}

• thanks, if you know how I can get rid of the warnings during the compilation time that would be awesome – Simon C. Dec 12 '17 at 9:07
• It does not work since LaTeX brings errors for every image that does not find. – Cragfelt Dec 12 '17 at 9:50

You can use \IfFileExits{<file>}{<what to do if so>}{<what to do otherwise>} to check if the file exists before using \includegraphics.

Note that the following example only works when the file extension is used. A macro without this limitation would be possible but way more complication.

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}

\newcommand\includeexistinggraphics[2][]{%
\IfFileExists{#2}{%
\includegraphics[#1]{#2}%
}{}%
}

\begin{document}
\begin{table}
\centering
\begin{tabular}{ccc}
\includeexistinggraphics[width=.3\textwidth]{image1.png} &
\includeexistinggraphics[width=.3\textwidth]{image2.png} &
\includeexistinggraphics[width=.3\textwidth]{image3.png} \\
\includeexistinggraphics[width=.3\textwidth]{image3.png} &
\includeexistinggraphics[width=.3\textwidth]{image4.png} &
\includeexistinggraphics[width=.3\textwidth]{image3.png} \\
\end{tabular}
\end{table}
\end{document}

• Thanks but yes it seems to be pretty ehavy. Maybe the best and proper way to do it would be to handle the throwned error by myself but I have no clue how tex's handling errors and I am afraid it may took me a long time to dig that way. – Simon C. Dec 12 '17 at 9:47
• @SimonC.: Well, the best way would be to only include images you actually have! Your whole workflow is not clear to me. – Martin Scharrer Dec 12 '17 at 11:48
• I use automatically generated tables that I put in beamer presentations. This allows me to present to my colleagues the results of thousand of paramaters combinations, and integrate it in a normal presentation. But as this represent lot of computational time, it often happen that some simulations haven't finished yet, or that part of the parameter space is not done yet to save time. Thus some cells in my matrices of images are linking to pictures that doesn't exists yet. – Simon C. Apr 4 '18 at 14:24