I am collaborating on a project that started off with images stored on a server. Since everyone had a different path to get to this server I took advantage of a \graphicspath
declaration in the main tex file so that everyone could access the files. Recently, people have started working offline, but still expect the document to compile.
There are several good questions around that demonstrate how to do a IfFileExists
check and replace with an alternative. Unfortunately I haven't yet found a version that successfully iterates through the specified graphics paths. Both file paths and names contain underscores and spaces. I am not a fan of this but I am stuck with this.
I was reading here: https://tex.stackexchange.com/a/3132/69378 a very clear answer that when \includegraphics{<thegraphic>}
is called it actually iterates through each \graphicspath
and checks it with \IfFileExists{<thepath><thegraphic>.pdf}
.
The function is in the MWE below, and it will only return the missing figures even if a file exists (but in a graphicspath specified base), because I accidentally overlooked the fact that just because the path passed to to the function works for \includegraphics
doesn't mean the same iteration will apply to IfFileExists
.
I tried to go to the sty files for graphicx
and graphics
but I didn't see any looping syntax I recognized.
Is it difficult to add a loop to check each path in graphics path to this new command that contains the IfFileExists
request?
Note: I am going the newcommand approach because I couldn't get the patch proposed here: https://tex.stackexchange.com/a/39987/69378 working with file extensions and spaces. But it's possible that time would be better spent getting the proposed patch working because it patches an already iterated command.
\documentclass{article}
\usepackage{todonotes}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{caption}
\usepackage[format=hang,singlelinecheck=0,font={sf,small},labelfont=bf]{subfig}
\graphicspath{%
{S://}
{../../../}
}
%S:\latex\MWE_Tex-StackExchange\2016-04-25 missing figures
%https://tex.stackexchange.com/questions/75014/is-it-possible-to-make-a-reference-to-a-subfigure-to-appear-figure-2a-with-cle
\captionsetup[subfigure]{subrefformat=simple,labelformat=simple,listofformat=subsimple}
\renewcommand\thesubfigure{\Alph{subfigure}}
\DeclareDocumentCommand{\MyIncludeGraphics}{ O{} +m }
{%
\IfFileExists{"#2"}
{%
\includegraphics[#1]{"#2"}%
}{%
\missingfigure[figwidth=7.0cm]{Missing "#2"}%
}%
}
\makeatletter
\renewcommand{\missingfigure}[2][]{% modified from todonotes.sty
\setkeys{todonotes}{#1}%
\addcontentsline{tdo}{todo}{\@todonotes@MissingFigureText: \protect\detokenize{"#2"}}%
\par
\noindent
\begin{tikzpicture}
\draw[fill=\@todonotes@currentfigcolor, draw = black!40, line width=2pt]
(-2, -2.5) rectangle +(\@todonotes@currentfigwidth, \@todonotes@currentfigheight);
\draw (2, -0.3) node[right, text width=\@[email protected]] {\detokenize{#2}};
\draw[red, fill=white, rounded corners = 5pt, line width=10pt]
(30:2cm) -- (150:2cm) -- (270:2cm) -- cycle;
\draw (0, 0.3) node {\@todonotes@MissingFigureUp};
\draw (0, -0.3) node {\@todonotes@MissingFigureDown};
\end{tikzpicture}\hfill
}
\makeatother
\begin{document}
\begingroup
\begin{figure}[ht!]
\subfloat{\label{fig:A}\MyIncludeGraphics[width=0.5\textwidth]{figure_A_On_S.jpg}}%
\subfloat{\label{fig:B}\MyIncludeGraphics{example_image_b.jpg}}%
\caption[CAPTION UNDER DEVELOPMENT Grin lens design and performance]{%
FIGURE NOT FINAL - CAPTION UNDER DEVELOPMENT \\
}
\end{figure}
\endgroup
\listoftodos
\end{document}
Update
The solution below works very well for every instance except file names with spaces.
I think I have tried every possible permutation I could find of \detokenize
and "
around variables etc, with no avail. Adding the quotes seem to be read literaly when checking if a file exists.
I have loaded \usepackage[space]{grffile}
, but if I understand how it works, this won't help unless I am using \includegraphics. The modification to allow spaces wont work unless I can find a way to maybe duplicate this code with my new imagetest
function, thanks to Ulrike Fischer in the answer below.
let\grffile@org@Ginclude@graphics\Ginclude@graphics
\renewcommand*{\Ginclude@graphics}{%
\ifx\grffile@filenameencoding\@empty
\else
\ifx\grffile@inputencoding\@empty
\expandafter\ifx\csname inputencodingname\endcsname\relax
\expandafter\ifx\csname
CurrentInputEncodingOption\endcsname\relax
\else
\let\grffile@inputencoding\CurrentInputEncodingOption
\fi
\else
\let\grffile@inputencoding\inputencodingname
\fi
\fi
\ifx\grffile@inputencoding\@empty
\else
\grffile@extendedcharstrue
\fi
\fi
\ifnum0\ifgrffile@babel 1\fi\ifgrffile@extendedchars 1\fi>\z@
\begingroup
\ifgrffile@babel
\csname @safe@activestrue\endcsname
\edef~{\string~}%
\fi
\ifgrffile@extendedchars
\grffile@inputenc@loop\^^A\^^H%
\grffile@inputenc@loop\^^K\^^K%
\grffile@inputenc@loop\^^N\^^_%
\grffile@inputenc@loop\^^?\^^ff%
\fi
\expandafter\grffile@extchar@Ginclude@graphics
\else
\expandafter\grffile@Ginclude@graphics
\fi
}
graphics.sty
???:\def\Ginclude@graphics#1{% \begingroup \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}%