使用 expl3 对序列的值求和

使用 expl3 对序列的值求和

在下面的代码中, 的定义应该\sumcounters是什么,才能使其将计数器thm和的当前值相加lemma

\documentclass{book}
\usepackage{amsthm}
\usepackage{xparse}
\newtheorem{thm}{Theorem}[chapter]
\newtheorem{lemma}{Lemma}[chapter]
\ExplSyntaxOn
\seq_new:N \g_my_counters
\seq_gput_right:Nn \g_my_counters { thm }
\seq_gput_right:Nn \g_my_counters { lemma }
% \NewDocumentCommand{\sumcounters}{}{< ? >}
\ExplSyntaxOff
\begin{document}
\chapter{Some chapter}
\sumcounters % should print 0

\begin{thm}
A theorem.
\end{thm}
\sumcounters % should print 1

\begin{lemma}
A lemma.
\end{lemma}
\sumcounters % should print 2
\end{document}

答案1

除了执行赋值之外,您还可以完全扩展地计算总和。这样做的好处是您可以在条件语句中使用它,例如

\ifnum\sumcounters=0 ... \fi

我还想提醒大家expl3使用匈牙利命名法对于变量,即变量应该在其名称中携带其保存的数据类型,通常作为后缀。

\documentclass{book}
\usepackage{amsthm}
\usepackage{xparse}
\newtheorem{thm}{Theorem}[chapter]
\newtheorem{lemma}{Lemma}[chapter]

\ExplSyntaxOn

\seq_new:N \g_my_counters_seq
\seq_gput_right:Nn \g_my_counters_seq { thm }
\seq_gput_right:Nn \g_my_counters_seq { lemma }

\cs_new:Npn \my_plus_value:n #1
  {
      + (\value{#1})
  }

\NewExpandableDocumentCommand \sumcounters { }
  {
    \int_eval:n
      {
        ( 0 \seq_map_function:NN \g_my_counters_seq \my_plus_value:n )
      }
  }

\ExplSyntaxOff

\begin{document}
\chapter{Some chapter}
\sumcounters % should print 0

\begin{thm}
A theorem.
\end{thm}
\sumcounters % should print 1

\begin{lemma}
A lemma.
\end{lemma}
\sumcounters % should print 2
\end{document}

答案2

您可能可以使用临时计数器(例如)来执行此操作,\l_tmpa_int但下面的代码定义了一个新计数器\l_counter_sum_int,然后\sumcounters宏使用来添加计数器的\seq_map_inline:Nn当前值,然后打印结果。输出是预期的:\value\g_my_counters

在此处输入图片描述

完整代码如下:

\documentclass{book}
\usepackage{amsthm}
\usepackage{xparse}
\newtheorem{thm}{Theorem}[chapter]
\newtheorem{lemma}{Lemma}[chapter]
\ExplSyntaxOn
\seq_new:N \g_my_counters
\seq_gput_right:Nn \g_my_counters { thm }
\seq_gput_right:Nn \g_my_counters { lemma }
\int_new:N \l_counter_sum_int% local counter for adding counter values
\NewDocumentCommand\sumcounters{}{
  \int_zero:N \l_counter_sum_int% set \l_counter_sum_int to 0
  \seq_map_inline:Nn \g_my_counters {% add counters in \g_my_counters
     \int_add:Nn \l_counter_sum_int {\value{##1}}
  }
  \int_use:N \l_counter_sum_int% print the result
}
\ExplSyntaxOff
\begin{document}
\chapter{Some chapter}
\sumcounters % should print 0

\begin{thm}
A theorem.
\end{thm}
\sumcounters % should print 1

\begin{lemma}
A lemma.
\end{lemma}
\sumcounters % should print 2
\end{document}

答案3

您可以存储\value{...}在序列中并最终使用该序列。

\documentclass{book}
\usepackage{amsthm}
\usepackage{xparse}
\newtheorem{thm}{Theorem}[chapter]
\newtheorem{lemma}{Lemma}[chapter]

\ExplSyntaxOn

\NewDocumentCommand{\definesummation}{mm}
 {
  \seq_new:c { g_noibe_summation_#1_seq }
  \clist_map_inline:nn { #2 }
   {
    \seq_gput_right:cn { g_noibe_summation_#1_seq } { \value{##1} }
   }
  \cs_new:cpn { #1 }
   {
    \int_eval:n { \seq_use:cn { g_noibe_summation_#1_seq } { + } }
   }
 }

\ExplSyntaxOff

\definesummation{sumstatements}{thm,lemma}

\begin{document}

\chapter{Some chapter}
\sumstatements % should print 0

\begin{thm}
A theorem.
\end{thm}

\sumstatements % should print 1

\begin{lemma}
A lemma.
\end{lemma}

\sumstatements % should print 2

\end{document}

在此处输入图片描述

也一样xassoccnt

\documentclass{book}
\usepackage{amsthm}
\usepackage{xassoccnt}

\newtheorem{thm}{Theorem}[chapter]
\newtheorem{lemma}{Lemma}[chapter]

\newcounter{totaltheorems}
\DeclareAssociatedCounters{thm}{totaltheorems}
\DeclareAssociatedCounters{lemma}{totaltheorems}

\begin{document}
\chapter{Some chapter}

\thetotaltheorems

\begin{thm}
A theorem.
\end{thm}

\thetotaltheorems

\begin{lemma}
A lemma.
\end{lemma}

\thetotaltheorems

\end{document}

相关内容