Latex3 数组项之和

Latex3 数组项之和

我曾经expl3对给定数组的项求和,但是正如您所见,该过程非常麻烦。

有没有更优雅的方式来做同样的事情?

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx} 
\ExplSyntaxOn
\fparray_new:Nn \g_nome {6}
\fparray_gset:Nnn \g_nome {1}{0.907}
\fparray_gset:Nnn \g_nome {2}{0.875}
\fparray_gset:Nnn \g_nome {3}{0.845}
\fparray_gset:Nnn \g_nome {4}{0.817}
\fparray_gset:Nnn \g_nome {5}{0.701}
\fparray_gset:Nnn \g_nome {6}{0.613}
%
\fp_new:N \somma
%
\fp_zero:N \somma
%
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {1}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {2}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {3}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {4}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {5}}
\fp_set:Nn \somma {\somma + \fparray_item:Nn \g_nome {6}}

\NewDocumentCommand{\calcnumd}{o m}
  {\IfValueTF{#1}
     {\num[round-mode = figures, round-precision = #1, round-integer-to-decimal]{\fp_to_decimal:n{#2}}}
     {\num{\fp_to_decimal:n{#2}}}
  }

\ExplSyntaxOff
\begin{document}
The sum of the elements of array is:

\[ S = \calcnumd{\somma}  \]

\end{document}

答案1

您可以使用以下方法对数组求和\int_step_function:nN

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx} 

\ExplSyntaxOn

\fparray_new:Nn \g_albystalks_main_fparray {6}
\fparray_gset:Nnn \g_albystalks_main_fparray {1}{0.907}
\fparray_gset:Nnn \g_albystalks_main_fparray {2}{0.875}
\fparray_gset:Nnn \g_albystalks_main_fparray {3}{0.845}
\fparray_gset:Nnn \g_albystalks_main_fparray {4}{0.817}
\fparray_gset:Nnn \g_albystalks_main_fparray {5}{0.701}
\fparray_gset:Nnn \g_albystalks_main_fparray {6}{0.613}

\cs_new:Nn \albystalks_sum_fparray:
 {
  \fp_eval:n
   {
    \int_step_function:nN { \fparray_count:N \g_albystalks_main_fparray }
      \__albystalks_fparray_item:n
   }
 }

\cs_new:Nn \__albystalks_fparray_item:n
 {
  + ( \fparray_item:Nn \g_albystalks_main_fparray { #1 } )
 }

\NewDocumentCommand{\calcnumd}{o}
 {
  \IfValueTF{#1}
   {
    \num
     [
      round-mode = figures,
      round-precision = #1,
      round-integer-to-decimal
     ]
     {
      \albystalks_sum_fparray:
     }
   }
   {
    \num { \albystalks_sum_fparray: } }
 }

\ExplSyntaxOff

\begin{document}

The sum of the elements of array is $S = \calcnumd$

Rounding to two digit precision, we have $S=\calcnumd[2]$

\end{document}

在此处输入图片描述

答案2

您可以使用循环来对项目求和。我还改变了数组的构造方式,只是为了提供另一种想法。

\documentclass{article}
\usepackage{xparse}
\usepackage{siunitx} 


\ExplSyntaxOn

\clist_set:Nn \l_tmpa_clist {0.907, 0.875, 0.845, 0.817, 0.701, 0.613}

\exp_args:NNx \fparray_new:Nn \g_nome {\clist_count:N \l_tmpa_clist}

\int_set:Nn \l_tmpa_int {1}
\int_do_until:nNnn {\l_tmpa_int} > {\fparray_count:N \g_nome} {
    \fparray_gset:Nnn \g_nome {\l_tmpa_int}{\clist_item:Nn \l_tmpa_clist {\l_tmpa_int}}
    \int_incr:N \l_tmpa_int
}

\fp_new:N \somma
\fp_zero:N \somma

\int_set:Nn \l_tmpa_int {1}
\int_do_until:nNnn {\l_tmpa_int} > {\fparray_count:N \g_nome} {
    \fp_add:Nn \somma { \fparray_item:Nn \g_nome {\l_tmpa_int} }
    \int_incr:N \l_tmpa_int
}

\NewDocumentCommand{\calcnumd}{o m}
  {\IfValueTF{#1}
     {\num[round-mode = figures, round-precision = #1, round-integer-to-decimal]{\fp_to_decimal:n{#2}}}
     {\num{\fp_to_decimal:n{#2}}}
  }

\ExplSyntaxOff


\begin{document}
The sum of the elements of array is:

\[ S = \calcnumd{\somma}  \]

\end{document}

相关内容