在添加 \addtocounter 时打印其值

在添加 \addtocounter 时打印其值

在下面的例子中,我希望生成的 pdf 显示300和的值500,但它们不会打印。

\documentclass{article}
\begin{document}

\newcounter{fund}
\newcommand{\fund}[1]{\addtocounter{fund}{#1}}

My first funding was worth \$\fund{300}.

My second funding was worth \$\fund{500}.

Thus, in total, my funding was \$\thefund.

\end{document}

有没有办法可以打印此值而不需要冗余:

My first funding was worth \$300. \fund{300}

谢谢。

答案1

我不确定这是否意味着(并且无论如何我也不推荐这样做),但是这里是\fund添加增量值并打印fund计数器的这个值的定义。

\documentclass{article}
\newcounter{fund}
\newcommand{\fund}[1]{\addtocounter{fund}{#1}#1}

\begin{document}


My first funding was worth \$\fund{300}.

My second funding was worth \$\fund{500}.

Thus, in total, my funding was \$\thefund.

\end{document}

在此处输入图片描述

更新

这是一个略有不同的版本:

\documentclass{article}

\newcounter{incfund}

\renewcommand{\theincfund}{\textdollar{}\arabic{incfund}}

\newcounter{fund}
\newcommand{\fund}[1]{%
  \setcounter{incfund}{#1}\theincfund%
  \addtocounter{fund}{#1}%
}

\begin{document}


My first funding was worth \fund{300}.

My second funding was worth \fund{500}.

Thus, in total, my funding was \textdollar\thefund.

\end{document}

相关内容