How to have ConTeXt ignore any `\externalfigure[]` code when a graphic is missing?
I could not find an option for that, so the only alternative is to just delete the low-level command's body.
\unprotect
\def\grph_include_replacement#1#2#3{}
\protect
\starttext
\externalfigure[nop]
\stoptext
Output is empty page (as expected).
You can also use \doifelsefigure
. The advantage over \doiffileexistselse
is that this honours the resolver settings for images.
\setupexternalfigures[location={local,global,default}]
\starttext
\doifelsefigure{nop}{\externalfigure[nop]}{}
\stoptext
You could use the \doiffileexistselse
command (or \doifelsefileexists
which does exactly the same thing), which has the syntax
\doiffileexistselse{file name}{true code}{false code}
Thus your MWE becomes
\starttext
\doiffileexistselse {001.png} {\externalfigure[001.png]} {}
\stoptext
You could define a wrapper around it, for example I've defined a \tryexternalfigure
command which seems to work alright. Then in the main text you can use it with the same syntax as \externalfigure
, and it is smart enough to handle the 1 to 3 arguments \externalfigure
expects.
\def \tryexternalfigure {\dotripleempty \dotryexternalfigure}
\def \dotryexternalfigure [#1][#2][#3]{%
\iffirstargument
\doiffileexistselse{#1} {\externalfigure[#1][#2][#3]} {}%
\fi
}
\starttext
\tryexternalfigure[001.png]
\tryexternalfigure[002.png][scale=2000]
\stoptext