检测图像路径是否包含字符串失败

检测图像路径是否包含字符串失败

对于一个项目,我想检查我包含的图像是否位于子文件夹中graphs。为了检查,我写了一个小的 MWE:

\documentclass[%
reprint,
amsfonts,
floatfix,
amsmath,amssymb,
aps,
pra,
]{revtex4-2}
\usepackage{graphicx}% Include figure files
\usepackage{letltxmacro}
\usepackage{xstring}
\usepackage[labelformat=simple]{subcaption}
\usepackage{float}
\usepackage{xparse}
\usepackage{substr}

\usepackage{hyperref}

\makeatletter
\DeclareRobustCommand{\filename}[1]{%
    \begingroup
    % \lstname seems to change hyphens into \textendash
    \def\textendash{-}%
    \filename@parse{#1}%
    \edef\filename@base{\detokenize\expandafter{\filename@base}}%
    \filename@base
    \endgroup
}
\makeatother

\ExplSyntaxOn

\NewDocumentCommand{\instringTF}{mmmm}
{
    \oleks_instring:nnnn { #1 } { #2 } { #3 } { #4 }
}

\tl_new:N \l__oleks_instring_test_tl

\cs_new_protected:Nn \oleks_instring:nnnn
{
    \tl_set:Nn \l__oleks_instring_test_tl { #1 }
    \regex_match:nnTF { \u{l__oleks_instring_test_tl} } { #2 } { #3 } { #4 }
}

\ExplSyntaxOff

\makeatletter
\AtBeginDocument{%
    \LetLtxMacro{\includegraphicsorig}{\includegraphics}
    \RenewDocumentCommand{\includegraphics}{O{} m }{%
        \filename@parse{#2}%
        \IfSubStr{\expandafter\detokenize{\filename@area}}{\detokenize{graph}}{Path \filename@area contains graph}{Path \filename@area does not contain graph}%
%       \IfSubStringInString{\detokenize{graph}}{\filename@area}{Path \filename@area contains graph}{Path \filename@area does not contain graph}%
    }%
}
\makeatother
\begin{document}
    \begin{figure}[htpb]
        \centering
        \includegraphics[width=0.7\linewidth]{graphs/sub.tikz}
        \caption{Image I}
    \end{figure}
    \begin{figure}[htpb]
        \centering
        \includegraphics[width=\linewidth]{example-image-a}
        \caption{Image II}
    \end{figure}
    \IfSubStr{graphs/}{graphs}{Contains graphs}{Does not contain graph}
\end{document}

不幸的是,重新定义版本中的函数includegraphics始终求值为false,而位于主体本身中的函数求值为true。我在这里遗漏了什么?

答案1

expandafter\detokenize{

扩展{不可扩展的,你打算

\detokenize\expandafter{

扩展 token 后{

相关内容