4

I am trying to compile a large collaborative document which includes the output of modelling in images which are updated very often but by other collaborators. I do not get these images. I still want to compile the overall document.

I have been using \usepackage[demo]{graphicx} which is good because the document will compile even when images are completely missing. This is as suggested in: Compile a file when images are missing/not available.

However I do want to include some images (my ones which I have the data for). I want to do something like adding \includegraphics[draft=false]{image} to specific images when using \usepackage[draft]{graphicx} as per Turning off and on images in figures. However, this option doesn't seem available for \usepackage[demo]{graphicx}.

Note that the team has to omit the image extension due to cross-platform collaboration leading to some images being generated with .PDF extensions and some with .pdf.

Anyone know a good solution here?

2
  • 3
    \IfFileExists{foo.png}{\includegraphics{foo.png}{no foo today} ? Jan 18, 2017 at 16:36
  • Thanks @DavidCarlisle. The documents have many hundreds of images in them and also not all members of the collaborative team have this same problem (only off site members don't have the full array of images). Therefore I think the majority may be a little miffed at this addition to benefit the minority!!
    – cyuut
    Jan 25, 2017 at 17:24

1 Answer 1

2

It seems like you're supplying an image file extension by default. This is helpful, since graphics handles this differently: If you [don't] supply an image extension and the file doesn't exist, it only creates a warning [error]. That makes a difference in terms of compilation, since a warning is not critical.

Therefore, I suggest using

\usepackage[draft]{graphicx}

Here's a minimal example:

enter image description here

\documentclass{article}

\usepackage[draft]{graphicx}

\begin{document}

\includegraphics[width=100pt]{some-bizarre-image.png}% This does not exist

\includegraphics[width=150pt,draft=false]{example-image.png}% This exists (http://ctan.org/pkg/mwe)

\end{document}

If you're not supplying the image file extension by default, then one may have to update \includegraphics to search for a possible file to include. Assuming you're compiling with pdfLaTeX, we can cycle through the possible image file extensions:

enter image description here

\documentclass{article}

\usepackage[draft]{graphicx}
\usepackage{pgffor,letltxmacro}

% https://tex.stackexchange.com/q/72930/5764
% .png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2

\newif\iffilefound
\LetLtxMacro\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
  \global\filefoundfalse
  \foreach \fext in {,.png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2} {%
    \iffilefound\else\IfFileExists{#2\fext}{\global\filefoundtrue\xdef\imgfile{#2\fext}}{}\fi%
  }%
  \iffilefound
    \oldincludegraphics[#1]{\imgfile}%
  \else
    \oldincludegraphics[#1]{example-image}%
  \fi
}

\begin{document}

\includegraphics[width=100pt]{some-bizarre-image}% This does not exist

\includegraphics[width=150pt,draft=false]{example-image}% This exists (http://ctan.org/pkg/mwe)

\end{document}

We cycle through all possible extensions (including no extension at all, if you did supply that manually as part of \includegraphics) and identify the first available extension found as the combination \imgfile. We insert the image of a made-up (but existing) .png filename if the image doesn't exist.

3
  • Thanks @werner. How about if I'm not providing the extension? This is mostly the case and I am getting Errors which stop pdf printing.
    – cyuut
    Jan 25, 2017 at 15:59
  • @cyuut: Yes, this completely changes things (hence the specific mention of you using extensions at the start of my answer). I'll see if I can come up with something that suits your needs.
    – Werner
    Jan 25, 2017 at 16:37
  • Thanks for continued help @werner. I've edited the question to make it more specific following your comments.
    – cyuut
    Jan 25, 2017 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .