计算多个枚举环境中 \item 的数量

计算多个枚举环境中 \item 的数量

我正在尝试自动生成多个环境中的项目总数enumerate,但不知何故这种方法对我来说不起作用:

\documentclass{article}

\begin{document}
There are \ref{lst:num1} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \label{lst:num1}
\end{enumerate}

\begin{document}
There are \ref{lst:num2} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \label{lst:num2}
\end{enumerate}

A simple addition of the two numbers does not work in latex: \ref{lst:num1} + \ref{lst:num2}

\end{document}

有什么方法可以生成\ref{lst:num1}和的总和吗\ref{lst:num2}?任何建议都将不胜感激。

答案1

\getrefnumber{...}您可以使用包提供的命令 refcount获取可扩展的数字,然后使用计数器将两个数字相加(newcnt此处)。最后使用命令显示结果\thenewcont

更新在后续问题之后。

\setcounter将计数器设置为一个值:单个数字或任意数字组合的总和。

您还可以使用 向其中添加另一个数字\addtocounter

d

\documentclass{article}

\usepackage{refcount}% \getrefnumber{...} <<<<<
\newcounter{newcnt} % added <<<<<

\begin{document}
There are \ref{lst:num1} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \label{lst:num1}
\end{enumerate}

There are \ref{lst:num2} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \item None of above. 
    \label{lst:num2}
\end{enumerate}

\setcounter{newcnt}{\numexpr\getrefnumber{lst:num1}+ \getrefnumber{lst:num2}\relax} % added <<<<<   

A "simple" addition of the two numbers  in latex: \thenewcnt \medskip

There are \ref{lst:num3} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \item None of above. 
    \item All of above. 
    \label{lst:num3}
\end{enumerate}

\addtocounter{newcnt}{\getrefnumber{lst:num3}} % added <<<<<<<<<<<<<<You can add    

Cumulative addition using \verb|\addtocounter|: \thenewcnt \medskip

 \setcounter{newcnt}{\numexpr\getrefnumber{lst:num1}+ \getrefnumber{lst:num2}+ \getrefnumber{lst:num3}\relax}

Or  adding the three numbers: \thenewcnt \medskip

\setcounter{newcnt}{\numexpr \getrefnumber{lst:num2}+ \getrefnumber{lst:num3}\relax}

While the last two add up: \thenewcnt   

\end{document}

如果文档中列举了许多环境,则保存小计的更简单的模板是

\documentclass{article}

\usepackage{refcount}% \getrefnumber{...} <<<<<
\newcounter{newcnt} % added <<<<<
\setcounter{newcnt}{0} % initial value of the acumulator <<<<<

\begin{document}
There are \ref{lst:num1} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \label{lst:num1}
\end{enumerate}

\addtocounter{newcnt}{\getrefnumber{lst:num1}}

Total number of items so far: \thenewcnt \medskip

There are \ref{lst:num2} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \item None of above. 
    \label{lst:num2}
\end{enumerate}

\addtocounter{newcnt}{\getrefnumber{lst:num2}}% <<<<<   

A "simple" addition of the two numbers  in latex: \thenewcnt \medskip

There are \ref{lst:num3} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \item None of above. 
    \item All of above. 
    \label{lst:num3}
\end{enumerate}

\addtocounter{newcnt}{\getrefnumber{lst:num3}} % added <<<<<<<<<<<<<<You can add    

Cumulative addition using \verb|\addtocounter|: \thenewcnt

\end{document}

答案2

这是最简单的方法。 enumi是第一级计数器,enumii是第二级计数器,依此类推。

您可以使用\getrefnumber(hyperref 的 refcount)将标签添加在一起,但这需要运行两次。

\documentclass{article}
\usepackage{refcount}

\newcounter{enumtotal}

\begin{document}
There are \ref{lst:num1} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \label{lst:num1}
    \addtocounter{enumtotal}{\value{enumi}}
\end{enumerate}

There are \ref{lst:num2} ways how a parameter can be given:
\begin{enumerate}
    \item By a constant expression.
    \item By user interaction.
    \item From another database. 
    \label{lst:num2}
    \addtocounter{enumtotal}{\value{enumi}}
\end{enumerate}

\theenumtotal{} total items.

\setcounter{enumtotal}{\getrefnumber{lst:num1}}%
\addtocounter{enumtotal}{\getrefnumber{lst:num2}}%
\theenumtotal{} computed using labels.

\end{document}

相关内容