我正在尝试存储变量及其相关信息,对变量号进行计算,并使用 siunitx 正确显示。当仅将数字保存为变量时,这种方法效果很好,但尝试将变量保存为 fpeval 的输出会产生错误。似乎它实际上并没有存储 fpeval 的输出,而是公式本身,尽管我不太确定。在下面的代码中,我注释掉了产生错误的行。
\documentclass{article}
\usepackage{amsmath}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\usepackage[per-mode=fraction]{siunitx} % also loads xparse and expl3
\sisetup
{
exponent-to-prefix = true ,
round-mode = figures ,
round-precision = 3 ,
scientific-notation = engineering,
}
\ExplSyntaxOn
\NewDocumentCommand{\varSet}{mmmm}
{
\prop_gclear_new:c { g_giacomo_var_#1_prop }
\prop_gput:cnn { g_giacomo_var_#1_prop } { value } { #2 }
\prop_gput:cnn { g_giacomo_var_#1_prop } { unit } { #3 }
\prop_gput:cnn { g_giacomo_var_#1_prop } { name } { #4 }
}
\NewDocumentCommand{\varChange}{mm}
{
\prop_gput:cnn { g_giacomo_var_#2_prop } { value } { #1}
}
\NewDocumentCommand{\varPrint}{sm}
{
\IfBooleanTF{#1}
{
\exp_args:Nnx \SI { \__giacomo_get:nn { #2 } { value } }
{ \__giacomo_get:nn { #2 } { unit } }
}
{
\__giacomo_get:nn { #2 } { name }
}
}
% syntactic sugar
\DeclareExpandableDocumentCommand{\varGet}{m}
{
\__giacomo_get:nn { #1 } { value }
}
\cs_new:Npn \__giacomo_get:nn #1 #2
{
\prop_item:cn { g_giacomo_var_#1_prop } { #2 }
}
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\varSet{Area}{1}{\metre\squared}{A_{rmin}}
\varSet{Width}{200}{\metre}{W_{sq}}
\varSet{Length}{10}{\metre}{L_{sq}}
\begin{document}
Input Length = $\varPrint*{Length}$\\
Input Width = $\varPrint*{Width}$\\
Calculated area: \fpeval{\varGet{Width}*\varGet{Length}}\\
%\varSet{Area}{\fpeval{\varGet{Width}*\varGet{Length}}}{\metre\squared}{A_{rmin}}
Saved Area: $\varPrint*{Area}$
\end{document}
答案1
如果您计划在的“值”参数中执行操作\varSet
,则需要\prop_gput:cnx
在相应的行中使用;当值明确时,这也只会进行无用的扩展,但无需担心。
\NewDocumentCommand{\varSet}{mmmm}
{
\prop_gclear_new:c { g_giacomo_var_#1_prop }
\prop_gput:cnx { g_giacomo_var_#1_prop } { value } { #2 }
\prop_gput:cnn { g_giacomo_var_#1_prop } { unit } { #3 }
\prop_gput:cnn { g_giacomo_var_#1_prop } { name } { #4 }
}
这样,l3fp
在将值存储到属性列表之前,第三个参数就完全展开了(使用兼容的语法是可能的)。
\documentclass{article}
\usepackage{amsmath}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\usepackage[per-mode=fraction]{siunitx} % also loads xparse and expl3
\sisetup
{
exponent-to-prefix = true ,
round-mode = figures ,
round-precision = 3 ,
scientific-notation = engineering,
}
\ExplSyntaxOn
\NewDocumentCommand{\varSet}{mmmm}
{
\prop_gclear_new:c { g_giacomo_var_#1_prop }
\prop_gput:cnx { g_giacomo_var_#1_prop } { value } { #2 }
\prop_gput:cnn { g_giacomo_var_#1_prop } { unit } { #3 }
\prop_gput:cnn { g_giacomo_var_#1_prop } { name } { #4 }
}
\NewDocumentCommand{\varChange}{mm}
{
\prop_gput:cnn { g_giacomo_var_#2_prop } { value } { #1}
}
\NewDocumentCommand{\varPrint}{sm}
{
\IfBooleanTF{#1}
{
\exp_args:Nnx \SI { \__giacomo_get:nn { #2 } { value } }
{ \__giacomo_get:nn { #2 } { unit } }
}
{
\__giacomo_get:nn { #2 } { name }
}
}
% syntactic sugar
\DeclareExpandableDocumentCommand{\varGet}{m}
{
\__giacomo_get:nn { #1 } { value }
}
\cs_new:Npn \__giacomo_get:nn #1 #2
{
\prop_item:cn { g_giacomo_var_#1_prop } { #2 }
}
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\varSet{Area}{1}{\metre\squared}{A_{rmin}}
\varSet{Width}{200}{\metre}{W_{sq}}
\varSet{Length}{10}{\metre}{L_{sq}}
\begin{document}
Input Length = $\varPrint*{Length}$
Input Width = $\varPrint*{Width}$
Calculated area: \fpeval{\varGet{Width}*\varGet{Length}}
\varSet{Area}{\fpeval{\varGet{Width}*\varGet{Length}}}{\metre\squared}{A_{rmin}}
Saved Area: $\varPrint*{Area}$
\end{document}