请考虑以下文档:
\documentclass{article}
\usepackage{xstring}
\makeatletter
\newcommand{\mycommand@firstchar}[1]{%
\StrRemoveBraces{#1}[\FirstChar]%
\StrChar{\FirstChar}{1}[\FirstChar]%
\FirstChar%
}
\newcommand{\mycommand@removespaces}[1]{\zap@space#1 \@empty}
\newcommand{\mycommand@lowercase}[1]{\expandafter\MakeLowercase\expandafter{#1}}
\newcommand{\makecode}[3]{\mycommand@removespaces{\mycommand@lowercase{%
\mycommand@firstchar{#1}\StrRemoveBraces{#2}:\StrRemoveBraces{#3}%
}}}
\makeatother
\begin{document}
\makecode{First}{Second}{Third}
%should produce fsecond:third
\end{document}
我想要做的是编写一个接受 3 个参数的命令,保留第一个字母,将其与第二个字母结合,添加冒号分隔符并添加第三个参数,同时删除所有空格和括号并将整个内容变为小写。
但是上面显示的当前代码无法编译,我不明白为什么以及如何解决这个问题。
答案1
\MakeLowercase
不以小写形式返回其参数,而是返回印刷参数小写。
您需要一个可扩展的版本\MakeLowercase
来expl3
救援:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\makecode}{mmm}
{
\vincent_makecode:nnn { #1 } { #2 } { #3 }
}
\tl_new:N \l__vincent_makecode_word_tl
\cs_new_protected:Nn \vincent_makecode:nnn
{
\vincent_makecode_process:Nn \tl_head:N { #1 }
\vincent_makecode_process:Nn \tl_use:N { #2 }
:
\vincent_makecode_process:Nn \tl_use:N { #3 }
}
\cs_new_protected:Nn \vincent_makecode_process:Nn
{
% store the lowercased argument for further processing
\tl_set:Nx \l__vincent_makecode_word_tl { \tl_lower_case:n { #2 } }
% remove braces and spaces
\regex_replace_all:nnN { (\cB.|\cE.|\s) } { } \l__vincent_makecode_word_tl
% issue the result, #1 is either \tl_head:N or \tl_use:N
#1 \l__vincent_makecode_word_tl
}
\ExplSyntaxOff
\begin{document}
\makecode{First}{Second}{Third}
\makecode{{Fi}rs{t}}{Se co{nd}}{T h i r d}
\end{document}
为了完整性,提供一个xstring
版本:
\documentclass{article}
\usepackage{xstring}
\makeatletter
\newcommand{\mycommand@firstchar}[1]{%
\StrRemoveBraces{#1}[\v@First]%
\StrDel{\v@First}{ }[\v@First]
\StrChar{\v@First}{1}[\v@First]%
}
\newcommand{\mycommand@remove}[2]{%
\StrRemoveBraces{#1}[#2]%
\StrDel{#2}{ }[#2]%
}
\newcommand{\makecode}[3]{%
\mycommand@firstchar{#1}%
\mycommand@remove{#2}{\v@Second}%
\mycommand@remove{#2}{\v@Third}%
\MakeLowercase{\v@First\v@Second:\v@Third}%
}
\makeatother
\begin{document}
\makecode{First}{Second}{Third}
\makecode{{Fi}rs{t}}{Se co{nd}}{T h i r d}
\end{document}