在设置计数器之前打印其值(由另一个计数器索引)

在设置计数器之前打印其值(由另一个计数器索引)

为了 MWE 的目的,我将问题简化为基本上显示水果店中的水果数量水果被计数了。我知道 totcount 包,这是我使用它的尝试。我有一种感觉,由于计数器是通过水果店编号进行索引的,因此在其实现中会出现一些问题。

事实上,如果我声明

\newtotcounter{fruitcounter1}
\newtotcounter{fruitcounter2}

而不是

\newtotcounter{fruitcounter\thefruitshop}

但目标是让水果店对柜台进行索引(假设我们不知道要访问多少家水果店)。

我知道考试课上有类似的应用,但通过 \fruits{x} 函数计算水果数量的能力对我的应用来说非常重要。理想情况下,主文档中的代码不能可以改变。有人有计算水果数量的解决方案吗?我的代码如下所示。

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{totcount}


\newcounter{fruitshop}
\setcounter{fruitshop}{1}

\newcommand{\visit}{
\vspace{0.5cm}
\newtotcounter{fruitcounter\thefruitshop}

\textbf{Fruit Shop Number \arabic{fruitshop}} that contains \total{fruitcounter\thefruitshop} fruits): 

\renewcommand{\fruits}[1]{
    #1\addtocounter{fruitcounter\thefruitshop}{#1}
}
}
\newcommand{\leave}{
    \vspace{0.5cm}
    We saw a total of \arabic{fruitcounter\thefruitshop} fruits! Would have been nice to see that number before entering the shop...
    \stepcounter{fruitshop}
}

\title{Fruit Counter}
\begin{document}

\visit 
There are \fruits{6} berries and \fruits{2} apples here. 

\leave

\visit 
Wow, there are \fruits{5} bananas here! 
\leave

\end{document}

例子

答案1

主要问题与 的使用相关的\newtotcounter本质上是扩展。您需要\thefruitshop在将其传递给 之前进行扩展\newtotcounter

有一些代码中的其他问题我也在此过程中修复了这个问题。

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{totcount}


\newcounter{fruitshop}
\setcounter{fruitshop}{1}

\newcommand\fruits{}

\newcommand{\visit}{
\vspace{0.5cm}
\begingroup\edef\x{\endgroup\noexpand\newtotcounter{fruitcounter\thefruitshop}}\x

\textbf{Fruit Shop Number \arabic{fruitshop}} that contains \total{fruitcounter\thefruitshop} fruits): 

\renewcommand{\fruits}[1]{
    ##1\addtocounter{fruitcounter\thefruitshop}{##1}
}
}
\newcommand{\leave}{
    \vspace{0.5cm}
    We saw a total of \arabic{fruitcounter\thefruitshop} fruits! Would have been nice to see that number before entering the shop...
    \stepcounter{fruitshop}
}

\title{Fruit Counter}
\begin{document}

\visit 
There are \fruits{6} berries and \fruits{2} apples here. 

\leave

\visit 
Wow, there are \fruits{5} bananas here! 
\leave

\end{document}

答案2

修复错误后(#1应该是##1两次并且\fruits必须在序言中收到定义),.aux文件显示

\relax
\expandafter\ifx\csname c@fruitcounter3@totc\endcsname\relax\newcounter{fruitcounter3@totc}\fi\setcounter{fruitcounter3@totc}{0\c@fruitcounter3 }
\expandafter\ifx\csname c@fruitcounter3@totc\endcsname\relax\newcounter{fruitcounter3@totc}\fi\setcounter{fruitcounter3@totc}{0\c@fruitcounter3 }

所以你看到总计数器的名称乱了。你需要的是

\ExpandArgs{e}\newtotcounter{fruitcounter\thefruitshop}

因此\thefruitshop在定义时就已充分展开。参见https://tex.stackexchange.com/a/587615/4427对于类似的问题。

完整代码,包含其他一些修复,特别是计数器步进的时刻(以及结束线的格式化和保护):

\documentclass[12pt]{article}
\usepackage{totcount}

\newcounter{fruitshop}

\newcommand{\fruits}[1]{%
  #1\addtocounter{fruitcounter\thefruitshop}{#1}%
}

\newcommand{\visit}{%
  \par\addvspace{0.5cm}%
  \stepcounter{fruitshop}%
  \ExpandArgs{e}\newtotcounter{fruitcounter\thefruitshop}%
  \textbf{Fruit Shop Number \arabic{fruitshop}} (that contains 
  \total{fruitcounter\thefruitshop} fruits): 
}

\newcommand{\leave}{%
  \par\addvspace{0.5cm}
  We saw a total of \arabic{fruitcounter\thefruitshop} fruits! Would have been
  nice to see that number before entering the shop...\par
}

\title{Fruit Counter}
\begin{document}

\visit 
There are \fruits{6} berries and \fruits{2} apples here. 

\leave

\visit 
Wow, there are \fruits{5} bananas here! 
\leave

\end{document}

相关内容