为了标记目的,我想打印带有扩展名的文件名,并提供完整的文件路径(以\tt
字体形式)
根据Egreg的回答仅获取列表的文件名和扩展名,而不是完整路径我编写了这个命令,但是我不能使用下划线,并且到目前为止我修改它的所有尝试都失败了:
\documentclass{article}
\usepackage{listings}
\usepackage{url}
\makeatletter
\DeclareRobustCommand{\filename}[1]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#1}%
\texttt{\filename@base.\filename@ext}%
\endgroup
}
\makeatother
\begin{document}
Works: \filename{path/to/file.txt} % prints: file.txt
Fails: \filename{path/to/file_2.txt} % should print: file_2.txt
\end{document}
我也尝试过构建类似的命令包xstring
,但对我来说也没有用。谢谢你的帮助!
答案1
您可以\filename@base
在使用它之前进行字符串化:
\documentclass{article}
\usepackage{listings}
\usepackage{url}
\makeatletter
\DeclareRobustCommand{\filename}[1]{%
\begingroup
% \lstname seems to change hyphens into \textendash
\def\textendash{-}%
\filename@parse{#1}%
\edef\filename@base{\detokenize\expandafter{\filename@base}}%
\texttt{\filename@base.\filename@ext}%
\endgroup
}
\makeatother
\begin{document}
\filename{path/to/file.txt} % prints: file.txt
\filename{path/to/file_2.txt} % should print: file_2.txt
\end{document}