![在 `expl3` 上下文中将参数转换为](https://linux22.com/image/406347/%E5%9C%A8%20%60expl3%60%20%E4%B8%8A%E4%B8%8B%E6%96%87%E4%B8%AD%E5%B0%86%E5%8F%82%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%B8%BA%20.png)
\newlistof{<entry>}{<ext>}{<list of names>}
我经常用和<entry>
来调用宏,<ext>
只是大小写不同。因此,我试图自动执行此约定,但我的两次尝试都失败了:
\documentclass{report}
%\usepackage[T1]{fontenc}
\usepackage{tocloft}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Nn \__erw_newlistof_i:nnn
{%
\newlistof{#1}{#2}{#3}
}
\cs_new:Nn \__erw_newlistof_ii:nnn
{%
\newlistof{#2}{#1}{#3}
}
%\__erw_newlistof:nnn{Foo}{foo}{Title} % OK
%\exp_last_unbraced:Nx \__erw_newlistof_i:nnn
%{%
% {Foo}%
% {\MakeLowercase{Foo}}%
% {Title}%
%}%ERROR: Argument of \@fileswith@pti@ns has an extra }.
%\exp_args:Nf \__erw_newlistof_ii:nnn
% {\MakeLowercase{Foo}}%
% {Foo}%
% {Title}% ERROR: Missing \endcsname inserted.
\ExplSyntaxOff
%\newlistof{Foo}{\MakeLowercase{Foo}}{Title}% ERROR: Missing \endcsname inserted.
\newlistof{Foo}
{foo}
{Title} % OK
\begin{document}
\listofFoo
\end{document}
答案1
我不太确定编写代码来处理少数情况是否有用。
\documentclass{article}
\usepackage{tocloft} % for newlistof
\usepackage{xparse}
\ExplSyntaxOn
\cs_new_protected:Nn \erw_newlistof:nnn
{
\newlistof{#1}{#2}{#3}
}
\cs_generate_variant:Nn \erw_newlistof:nnn { ne }
\cs_new_protected:Nn \erw_newlistof:nnnn
{
\newlistof[#1]{#2}{#3}{#4}
}
\cs_generate_variant:Nn \erw_newlistof:nnnn { nne }
\NewDocumentCommand{\NewListOf}{omO{#2}m}
{
\IfNoValueTF{#1}
{
\erw_newlistof:nen { #2 } { \str_lowercase:n { #3 } } { #4 }
}
{
\erw_newlistof:nnen { #1 } { #2 } { \str_lowercase:n { #3 } } { #4 }
}
}
\ExplSyntaxOff
\NewListOf{Foo}{Title foo}
\newcommand{\Foo}[1]{\addcontentsline{foo}{section}{#1}\par}
\NewListOf[section]{Baz}[zab]{Title baz}
\newcommand{\Baz}[1]{\addcontentsline{zab}{section}{#1}\par}
\begin{document}
\listofFoo
\listofBaz
\Foo{x}
\Baz{y}
\end{document}