函数参数中的下划线似乎会改变文件名

函数参数中的下划线似乎会改变文件名

我一直试图将包含下划线的文件名传递给几个嵌套命令。由于转义每个命令_并不是一个真正的选择,我认为组 \catcode_=12(必须删除那里的反勾号,因为我无法在此问题文本中转义它)调整会有所帮助,但有趣的是,在完全出乎意料的结果中,编译器错误结果告诉我该文件不存在。为什么?因为它要查找的文件名不再有下划线……我不认为这是它所做的。因为我以前曾使用过这种方法来转义#作为参数传递的文件名中的符号。

我尝试减少嵌套命令的数量以生成更简单的 MWE。如果结果过于简单并且恰好导致相同的错误消息,我会进行更新。

\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}

答案1

如果文件丢失,它将无法正确排版文件名,但如果存在,它将找到并使用该图像,否则将排版类似于文件名的内容。

\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=\@[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]{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}

输出不太正确

希望您能改进这一点。

相关内容