提供的代码已复制粘贴并编译。
问题:\l_secondIndex_tl
变量似乎没有扩展到行中存储的值\tl_case:NnF
,因此代码无法正确找到案例。(在第一个测试案例中,它输出“未找到案例”而不是“一个”)。
如果我将值硬编码\l_secondIndex_tl
为1
,则代码会为所有测试用例输出“一”,正如预期的那样(因为该值是硬编码的,所以输入并不重要)。
问题:我怎样才能使代码按预期工作?(如何强制变量扩展为其值?)
主意:该命令接收2位数字(中间没有分隔符),检查第二个数字并输出相应情况的消息。
非常感谢您提前提供的任何帮助!
\documentclass{article}
\usepackage{expl3} % To me, it was not necessary to load in.
\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}{
%
\tl_set:Nn \l_firstIndex_tl { \tl_use:N {#1} {1} }
\tl_set:Nn \l_secondIndex_tl { \tl_use:N {#1} {2} }
%
\tl_case:NnF \l_secondIndex_tl
{
{1} { one }
{2} { two }
{3} { three }
}
{No~case~found.}
}
\ExplSyntaxOff
\begin{document}
Test the code:
\par \mycommand{21} \% Not OK - expected output: "one"
\par \mycommand{26} \% OK - expected output: "No case found."
\par \mycommand{2a} \% OK - expected output: "No case found."
\end{document}
答案1
使用\tl:use:N
似乎错误,但从期望的输出来看,我猜你想要
取自
\documentclass{article}
% only needed for old latex \usepackage{expl3}
\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}{
%
\tl_set:Nx \l_firstIndex_tl { \use_i:nn #1 }
\tl_set:Nx \l_secondIndex_tl { \use_ii:nn #1 }
%
\str_case:VnF \l_secondIndex_tl
{
{1} { one }
{2} { two }
{3} { three }
}
{No~case~found.}
}
\ExplSyntaxOff
\begin{document}
Test the code:
\par \mycommand{21} \% Not OK - expected output: "one"
\par \mycommand{26} \% OK - expected output: "No case found."
\par \mycommand{2a} \% OK - expected output: "No case found."
\end{document}
(答案已更新为使用,\str_case:VnF
代替exp_args:NV\tl_case:NnF
whch 更干净,并避免使用带有字符标记的 N 参数(这有效但有点滥用,因为它应该是一个命令名称标记))
答案2
您误解了它的\tl_case:Nn(TF)
工作原理。它将标记字符串变量与标记字符串变量进行比较,而不是与诸如 之类的显式标记列表进行比较\str_case:nn(TF)
。
\documentclass{article}
\usepackage{expl3} % To me, it was not necessary to load in.
\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}
{
\keenontex_index:n { #1 }
}
\tl_new:N \l_keenontex_index_first_tl
\tl_new:N \l_keenontex_index_second_tl
\tl_const:Nn \c_keenontex_index_one_tl { 1 }
\tl_const:Nn \c_keenontex_index_two_tl { 2 }
\tl_const:Nn \c_keenontex_index_three_tl { 3 }
\cs_new_protected:Nn \keenontex_index:n
{
\tl_set:Nx \l_keenontex_index_first_tl { \tl_item:nn {#1} {1} }
\tl_set:Nx \l_keenontex_index_second_tl { \tl_item:nn {#1} {2} }
%
\tl_case:NnF \l_keenontex_index_second_tl
{
\c_keenontex_index_one_tl { one }
\c_keenontex_index_two_tl { two }
\c_keenontex_index_three_tl { three }
}
{No~case~found.}
}
\ExplSyntaxOff
\begin{document}
Test the code:
\mycommand{21} \% OK - expected output: "one"
\mycommand{26} \% OK - expected output: "No case found."
\mycommand{2a} \% OK - expected output: "No case found."
\end{document}
以下是相应的代码\str_case_e:nn(TF)
\documentclass{article}
\usepackage{expl3} % To me, it was not necessary to load in.
\ExplSyntaxOn
\NewExpandableDocumentCommand{\mycommand}{m}
{
\keenontex_index:n { #1 }
}
\cs_new:Nn \keenontex_index:n
{
\str_case_e:nnF { \str_item:nn { #1 } { 2 } }
{
{ 1 } { one }
{ 2 } { two }
{ 3 } { three }
}
{No~case~found.}
}
\ExplSyntaxOff
\begin{document}
Test the code:
\mycommand{21} \% OK - expected output: "one"
\mycommand{26} \% OK - expected output: "No case found."
\mycommand{2a} \% OK - expected output: "No case found."
\end{document}