我正在尝试实现一个根据输入的预定义字符串计算索引的函数。基本上,它以 2 个字母作为输入,然后计算索引。
我先逐块构建,先编写了计算函数,现在尝试添加字符串提取。我想输入一个字符串,从中提取字母,然后计算索引。
事实上,这没有多大意义,但这是一个更大项目的一部分;)
输入字符串为N
和N
。计算出的索引为8
。计算此索引有效。
失败之处从输入字符串中提取这些字母AV:N/AC:N
,并调用函数计算索引。
如果我使用静态字母调用该函数,则不会出现任何问题
\exp_args:Ne \__XXXX_choose_mapping:nn
{ \fp_eval:n { \__XXXX_compute_value:n {N} } } % N is static
{ \fp_eval:n { \__XXXX_factor_scaling:n {N} } } % N is static
我可以从输入字符串中提取 2 N
(并将它们存储在字符串变量中,但像这样调用函数会失败
\exp_args:Ne \__XXXX_choose_mapping:nn
{ \fp_eval:n { \__XXXX_compute_value:n { \l_XXXXXAV_str } } }
{ \fp_eval:n { \__XXXX_factor_scaling:n { \l_XXXXXAC_str } } }
我应该怎么做才能使这个带有变量的函数调用起作用?
这是我的(失败的)MWE
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new:Npn \__XXXX_parse_value:n #1
{
\str_case:nnF {#1}
{
{ N } { 0.85 } % This value will be selected
{ A } { 0.62 } % This will not
}
{
\msg_expandable_error:nnn { XXXX } { wrong-case } {#1}
1.00 % default value
}
}{}
\cs_new:Npn \__XXXX_compute_value:n #1 { 1 - (1 - \__XXXX_parse_value:n {#1} ) }
\cs_new:Npn \__XXXX_factor_scaling:n #1 { 8.22 * (\__XXXX_parse_value:n {#1} ) }
\NewExpandableDocumentCommand \basicMacro { m }
{
\exp_args:Ne \__XXXX_choose_mapping:nn
{ \fp_eval:n { \__XXXX_compute_value:n {#1} } }
{ \fp_eval:n { \__XXXX_factor_scaling:n {#1} } }
}
\cs_new:Npn \__XXXX_choose_mapping:nn #1 #2
{
\fp_compare:nTF { #1 <= 0 }
{ 0.0 }
{ \fp_eval:n { round( min( 1.08 * (#1 + #2), 10) ) } }
}
%%%%%%%%%%%%%%%%%%%%%%%%%%
% These are the thresholds
\def\scoreCrit{9.0}
\def\scoreLow{5.0}
\NewExpandableDocumentCommand \category { m }{%
%\newcommand{\category}[1]{
\fp_compare:nNnTF {#1}<{\scoreLow}{Info}
{
\fp_compare:nNnTF{#1}<{\scoreCrit}{High}
{Critical}
}%
}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ADDITIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NewExpandableDocumentCommand \computeXXXXX { m }{%
\str_new:N \l_XXXXXAV_str
\str_new:N \l_XXXXXAC_str
% A group allows nesting
\group_begin:
% Split into parts (separated by "/")
\seq_set_split:Nnn \l__tnscalc_splittab_seq { / } { #1 }
% Check that there are 2 items
\int_compare:nNnTF {(\seq_count:N \l__tnscalc_splittab_seq)} = {2}{}{FALSE}
% Get the 1st item out of the list : AV:N
\seq_pop_left:NN \l__tnscalc_splittab_seq \l__tnscalc_tempAV_tl
% Check item's value
\str_case_e:nnF {\l__tnscalc_tempAV_tl}
{
{AV:N} { \str_set:Nn \l_XXXXXAV_str {N} }
{AV:H} { \str_set:Nn \l_XXXXXAV_str {H} }
}
{ \PackageError{XXXXX}{"\l__tnscalc_tempAV_tl"~is~not~a~XXXXX~AV~value}{<help>}}
% Get the 2nd item out of the list : AC:N
\seq_pop_left:NN \l__tnscalc_splittab_seq \l__tnscalc_tempAC_tl
\str_case_e:nnF {\l__tnscalc_tempAC_tl}
{
{AC:N} { \str_set:Nn \l_XXXXXAC_str {N} }
{AC:H} { \str_set:Nn \l_XXXXXAC_str {H} }
}
{ \PackageError{XXXXX}{"\l__tnscalc_tempAC_tl"~is~not~a~XXXXX~AC~value}{<help>}}
% \str_show:N \l_XXXXXAV_str
% \str_show:N \l_XXXXXAC_str
% % THIS WORKS BUT IS STATIC
% \exp_args:Ne \__XXXX_choose_mapping:nn
% { \fp_eval:n { \__XXXX_compute_value:n {N} } }
% { \fp_eval:n { \__XXXX_factor_scaling:n {N} } }
% THIS DOES NOT WORK
\exp_args:Ne \__XXXX_choose_mapping:nn
{ \fp_eval:n { \__XXXX_compute_value:n { \l_XXXXXAV_str } } }
{ \fp_eval:n { \__XXXX_factor_scaling:n { \l_XXXXXAC_str } } }
\group_end:
}%
\ExplSyntaxOff
\begin{document}
\edef\x{\basicMacro{N}}
\texttt{\meaning\x}
\category{\x}
\category{\basicMacro{N}}
------
\computeXXXXX{AV:N/AC:N} % FAILING LINE
\category{\computeXXXXX{AV:N/AC:N}} % FAILING LINE
\end{document}