![在添加 \addtocounter 时打印其值](https://linux22.com/image/328708/%E5%9C%A8%E6%B7%BB%E5%8A%A0%20%5Caddtocounter%20%E6%97%B6%E6%89%93%E5%8D%B0%E5%85%B6%E5%80%BC.png)
在下面的例子中,我希望生成的 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}