带下划线的文件名比较

带下划线的文件名比较

我们已经测试过\filename{XYZ-2100011}输入\include{XYZ-2100011}的 tex 文件名是否匹配,否则我们会在宏中传递错误消息。

在我的案例中如何支持输入文件名包含下划线“XYZ_2100011”。

\filename{XYZ_2100011}例如,\include{XYZ_2100011}

请找到以下 MWE 文件:

\documentclass{article}

\makeatletter
\def\filename#1{\gdef\@filename{#1}}

\def\include#1{\relax
  \def\@tmp{#1}%
  \ifx\@filename\@tmp
    \ifnum\@auxout=\@partaux
      \@latex@error{\string\include\space cannot be nested}\@eha
    \else \@include#1 \fi
  \else
    \@latex@error{\string Filenames don't match}\@eha
  \fi}

\makeatother

\filename{XYZ-2100011}%it should be XYZ_2100011

\begin{document}

\include{XYZ-2100011}%it should be XYZ_2100011

\end{document}

答案1

使用当前的乳胶,您可以使用钩子添加测试include/before

\documentclass{article}

\makeatletter
\newcommand\filename[1]{\edef\mypackage@filename{\detokenize{#1}}}
\newcommand\mypackage@filename{}
\AddToHook{include/before}{%
  \ifx\CurrentFile\mypackage@filename 
  \else
  \PackageError{mypackage}{filename '\mypackage@filename' doesn't match with \CurrentFile}{}%
  \fi}
\makeatother  
\filename{XYZ_2100011}

\begin{document}

\include{XYZ_2100011}
\include{XYZ_2100011-X}
\end{document}

相关内容