我正在使用if =\str_if_eq:xxTF{\A}{\Target}{<true>}{<false}
来执行<true>
或代码。<false>
\A
\Target
与此对应的列表版本是什么?
所以我正在寻找诸如
\str_if_in_list:xxTF{\A}{\TargetList}{<true>}{<false}
执行适当的<true>
/ <false>
ifA
在列表格式 (CSV) \TargetList
?
我必须承认,我还没有尝试太长时间,因为我还看不懂expl3
语法,但是使用 Expl3 标记列表变量,其中调用标记列表看起来很有趣,但我无法让它们工作。
因此,在下面的 MWE 中,通过更改\GetTexFilePath
最后\GetTexFilePathExpandable
三行应该不是在目录路径中有一个xxx
。当前输出是:
笔记:
- 始终是
\TargetList
一个包含列表的宏。 - 比较的来源是通常根据下面的 MWE声明的值
\newtoks
(不确定是否重要)。
参考:
在人们产生误解并认为我实际上理解了一些expl3
语法之前,这里是该代码的来源:
可能的解决方案线索:
代码:
\documentclass{article}
\usepackage{xparse}
\newcommand{\NotSpecialDirectory}{foo}
%% These directories should never have an "xxx" in the path.
%% The values here do NOT change at run time.
%% https://tex.stackexchange.com/questions/46323/how-to-expand-values-stored-in-a-token-defined-by-newtoks
\protected\def\SpecialDirectory{AAA}
\protected\def\SpecialDirectoryB{BBB}
\protected\def\SpecialDirectoryC{CCC}
%% The list of members here do NOT change at run time.
\newcommand{\ListOfSpecialDirectories}{%
\SpecialDirectory,%
\SpecialDirectoryB,%
\SpecialDirectoryC,% <-- would prefer to be allowed extra comma here, but
% can do without that if it adds too much complexity
}
\newtoks{\CurrentDirectory}
\ExplSyntaxOn%%% These work fine...
% Determine path: Always #1/xxx#2/#3 unless #1=\SpecialDirectory
\NewDocumentCommand{\GetTexFilePathOld}{m m m}{%
\str_if_eq:xxTF{#1}{\SpecialDirectory}{#1/#2/#3}{#1/xxx#2/#3}%
}%
\DeclareExpandableDocumentCommand{\GetTexFilePathExpandableOld}{m m m}{%
\str_if_eq:xxTF{#1}{\SpecialDirectory}{#1/#2/#3}{#1/xxx#2/#3}%
}%
%%% how do I define these ...
% Determine path: Always #1/xxx#2/#3 unless #1 is in \ListOfSpecialDirectories
% Don't work: % \tl_if_in:nnTF \str_if_in:nnTF, \str_if_in:xxTF
\NewDocumentCommand{\GetTexFilePath}{m m m}{%
\str_if_eq:xxTF{#1}{\ListOfSpecialDirectories}{#1/#2/#3}{#1/xxx#2/#3}%
}%
\DeclareExpandableDocumentCommand{\GetTexFilePathExpandable}{m m m}{%
\str_if_eq:xxTF{#1}{\ListOfSpecialDirectories}{#1/#2/#3}{#1/xxx#2/#3}%
}%
\ExplSyntaxOff
\begin{document}
\noindent\textbf{These work fine:}
\CurrentDirectory={\NotSpecialDirectory}
Should have xxx in path:
\GetTexFilePathOld{\the\CurrentDirectory}{2012}{01},
\GetTexFilePathExpandableOld{\the\CurrentDirectory}{2012}{02}
\CurrentDirectory={\SpecialDirectory}
\medskip\par Should \emph{not} have xxx:
\GetTexFilePathOld{\the\CurrentDirectory}{2013}{03},
\GetTexFilePathExpandableOld{\the\CurrentDirectory}{2013}{04}
\bigskip\hrule\bigskip
\noindent\textbf{Paths starting with AAA, BBB, or CCC should not have an xxxx in the path}
\medskip\par
\CurrentDirectory={\NotSpecialDirectory}
Should have xxx in path:
\GetTexFilePath{\the\CurrentDirectory}{2012}{05},
\GetTexFilePathExpandable{\the\CurrentDirectory}{2012}{06}
\CurrentDirectory={\SpecialDirectory}
\medskip\par Should \emph{not} have xxx:
\GetTexFilePath{\the\CurrentDirectory}{2013}{07},
\GetTexFilePathExpandable{\the\CurrentDirectory}{2013}{08}
\CurrentDirectory={\SpecialDirectory}
\medskip\par Should \emph{not} have xxx:
\GetTexFilePath{\the\CurrentDirectory}{2013}{09},
\GetTexFilePathExpandable{\the\CurrentDirectory}{2013}{10}
\CurrentDirectory={\SpecialDirectoryB}
\medskip\par Should \emph{not} have xxx:
\GetTexFilePath{\the\CurrentDirectory}{2013}{01},
\GetTexFilePathExpandable{\the\CurrentDirectory}{2013}{11}
\bigskip
\end{document}
答案1
我不知道这是否真的能满足您的需求,因为我无法准确理解。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\GetTeXFilePath}{m m m}
{
\grill_str_if_eq_x_in:nVTF { #1 } \ListOfSpecialDirectories {#1/#2/#3} {#1/xxx#2/#3}
}
\bool_new:N \l__grill_path_bool
\prg_new_protected_conditional:Npnn \grill_str_if_eq_x_in:nn #1 #2 { T,F,TF }
{
\bool_set_false:N \l__grill_path_bool
\clist_map_inline:nn { #2 }
{
\str_if_eq_x:nnT { #1 } { ##1 }
{ \clist_map_break:n { \bool_set_true:N \l__grill_path_bool } }
}
\bool_if:NTF \l__grill_path_bool
{ \prg_return_true: }
{ \prg_return_false: }
}
\cs_generate_variant:Nn \grill_str_if_eq_x_in:nnTF { nV }
\ExplSyntaxOff
\def\SpecialDirectory{AAA}
\def\SpecialDirectoryB{BBB}
\def\SpecialDirectoryC{CCC}
\newcommand{\ListOfSpecialDirectories}{\SpecialDirectory, \SpecialDirectoryB, \SpecialDirectoryC}
\begin{document}
\GetTeXFilePath{AAA}{2012}{05}
\GetTeXFilePath{\SpecialDirectoryB}{2012}{05}
\GetTeXFilePath{DDD}{2012}{05}
\end{document}
这将打印
AAA/2012/05
BBB/2012/05
DDD/xxx2012/05
但这当然不能在可扩展的上下文中使用。但是,如果你想使用结果,只需说
\NewDocumentCommand{\GetTeXFilePath}{m m m}
{
\grill_str_if_eq_x_in:nVTF { #1 } \ListOfSpecialDirectories
{ \tl_set:Nn \l_grill_result_tl {#1/#2/#3} }
{ \tl_set:Nn \l_grill_result_tl {#1/xxx#2/#3} }
}
并用于\l_grill_result_tl
将其传递给其他宏。