我创建了一个可以获取和设置的键值列表,但是当我尝试以工程写作的形式显示存储的数字时,它会给出一个错误:包 siunitx 错误:无效的数字输入'\GetField {varB}'。
我不太熟悉 expl3,我想我知道它来自“n”,对于参数的返回方式应该是“c”,但我尝试了不同的配置但没有成功。
如果有人能解释我做错了什么以及我应该改变什么
\documentclass{article}
\usepackage{xparse}
\usepackage[binary-units=true, detect-all]{siunitx}[=v2]
\sisetup {
exponent-to-prefix = true, % convert exponents into belonging prefixes
round-mode = places,
per-mode = symbol,
per-symbol = /
}
\ExplSyntaxOn
\prop_new:N \g_FieldList_prop
\cs_new:Nn \td_store_property:nn {
\prop_gput:Nnn \g_FieldList_prop {#1} {#2}
}
%Defining key-value interface
\keys_define:nn {FieldList} {
varA .code:n = {\td_store_property:nn {varA}{#1} },
varB .code:n = {\td_store_property:nn {varB}{#1} },
varC .code:n = {\td_store_property:nn {varC}{#1} },
varD .code:n = {\td_store_property:nn {varD}{#1} }
}
\NewDocumentCommand{\FieldList}{+m}{
\prop_gclear:N \g_FieldList_prop% Clearing the property list
% Set the keys
\keys_set:nn {FieldList}{#1}
}
\NewDocumentCommand{\GetField}{m}{
\prop_item:Nn \g_FieldList_prop {#1}% Extract the key #1 from the property list
}
\ExplSyntaxOff
\begin{document}
\FieldList{
varA = 500,
varB = {1e11},
varC = {8.54e11},
varD = Si
}
\noindent 1: \GetField{varB} - \num{\GetField{varB}}\\
\end{document}
谢谢!干杯
答案1
您使用保护不当。例如,您的\cs_new:Nn
应该是\cs_new_protected:Nn
,因为它执行赋值。但是,可以省去 函数,因为您可以使用.prop_gput:N
执行完全相同的操作。
用 定义的命令\NewDocumentCommand
是受保护的;对于您想要在 的参数中使用的可扩展命令\num
,请使用\NewExpandableDocumentCommand
(您可以,因为\prop_item:Nn
它是完全可扩展的。
\documentclass{article}
\usepackage{siunitx}
\sisetup{
detect-all,
binary-units = true,
exponent-to-prefix = true,
round-mode = places,
per-mode = symbol,
per-symbol = /,
}
\ExplSyntaxOn
\prop_new:N \g_drstu_FieldList_prop
%Defining key-value interface
\keys_define:nn {drstu/FieldList}
{
varA .prop_gput:N = \g_drstu_FieldList_prop,
varB .prop_gput:N = \g_drstu_FieldList_prop,
varC .prop_gput:N = \g_drstu_FieldList_prop,
varD .prop_gput:N = \g_drstu_FieldList_prop,
}
\NewDocumentCommand{\FieldList}{m}
{
% Clearing the property list
\prop_gclear:N \g_drstu_FieldList_prop
% Set the keys
\keys_set:nn {drstu/FieldList} {#1}
}
\NewExpandableDocumentCommand{\GetField}{m}
{
% Extract the key #1 from the property list
\prop_item:Nn \g_drstu_FieldList_prop {#1}
}
\ExplSyntaxOff
\begin{document}
\FieldList{
varA = 500,
varB = {1e11},
varC = {8.54e11},
varD = Si
}
\noindent 1: \GetField{varB} --- \num{\GetField{varB}}
\end{document}