2

I have been trying to pass a file name that contains underscores to a couple of nested commands. Since escaping each and every _ isn't really an option, I thought a group \catcode_=12 (had to remove the back tick there because I couldn't escape it in this question text) adjustment would help but interestingly and in a completely unexpected result, the compiler error results are telling me the file doesn't exist. Why? Because the filename it's looking for has no underscores anymore... I didn't think this is what it did. Because I have used this approach previously to escape # signs in file names being passed as arguments.

I have tried to reduce the number of nested commands to produce a simpler MWE. I will update if this ends up being too oversimplified and just coincidentally causing the same error messages.

\documentclass{article}
\usepackage{todonotes}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{xpatch} 

\usepackage{caption}
\usepackage[export]{adjustbox}
\usepackage[format=hang,singlelinecheck=0,font={sf,small},labelfont=bf]{subfig}

%http://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}}


\newcommand{\cmd}{\begingroup
    \catcode`_=12 \cmdint}
\newcommand{\cmdint}[1]{%
    \texttt{\scantokens{#1\noexpand}}%
    \endgroup}


\DeclareDocumentCommand{\MyIncludeGraphics}{ O{} +m }
{
    \IfFileExists{#2}
    {
        \includegraphics[#1]{#2}%
    }
    {
        \missingfigure[figwidth=7.0cm]{Missing #2}%
    }
}

\begin{document}

\begingroup
%   \catcode`_=12 % can't use this because it essentially changes the file name so the file can't be found ... I didn't think this is what it did..

    \begin{figure}[ht!] 
    \subfloat{\label{fig:A}\MyIncludeGraphics[width=0.5\textwidth]{First_File_Does_Not_Exist.png}}%
    \subfloat{\label{fig:B}\MyIncludeGraphics{Second_File_Does_Not_Exist.jpg}}%
    \caption[CAPTION UNDER DEVELOPMENT Grin lens design and performance]{%
        FIGURE NOT FINAL - CAPTION UNDER DEVELOPMENT \\ 
    }
\end{figure}
\endgroup
\end{document}
EngBIRD
  • 3,985

1 Answers1

2

This will not typeset the file name correctly if the file is missing, but it will find and use the image if present and will typeset something similar to the file name otherwise.

\documentclass{article}
\usepackage{todonotes}
\usepackage{graphicx}
\usepackage{xparse}
\usepackage{caption}
\usepackage[format=hang,singlelinecheck=0,font={sf,small},labelfont=bf]{subfig}
%http://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=\@todonotes@currentfigwidth-4.5cm] {\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]{example_image_a.png}}%
    \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}

not quite right output

Hopefully, you can improve on this.

cfr
  • 198,882
  • Thanks. I over simplified my MWE. I was so sure my problem was the underscores (because the MWE above and other attempts like with scantoken) kept failing, but now I think the crux may have been \IfFileExists{"#2"} with my forgotten reliance on \graphicspath definitions. I mistakenly thought IfFileExists might have iteratively checked the names with graphicspaths similar to the way includegraphics does. Random alternative - does LaTeX have an equivalent to a try catch block that could be used to catch the error in includegraphics if the file doesn't exist? – EngBIRD Apr 28 '16 at 05:42
  • Sorry, but I'm not a programmer so asking me if LaTeX has something equivalent to 'a try catch block' won't work ;). – cfr Apr 28 '16 at 13:19