3

In the following example, I want the resulting pdf to show the values of 300 and 500, but they do not print.

\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}

Is there a way I can print this value without the redundant:

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

Thanks.

1
  • There's no command within \fund that prints the current value of the counter! Either use #1 for the increment value to be printed or \thefund for the total counter.
    – user31729
    Feb 21 '16 at 20:59
3

I am not sure this is meant (and I don't recommend it anyway), but here is the definition of \fund that adds an increment value and prints this value of the fund counter.

\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}

enter image description here

Update

Here's a slightly different version:

\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}
2
  • This works! Why don't you recommend it?
    – CephBirk
    Feb 21 '16 at 21:02
  • 1
    @CephBirk: It mixes 'computation' and output. And you can't use this for $100.50$, for example
    – user31729
    Feb 21 '16 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.