5

In the following document I would like for the numbers to have a thousandths separator comma.

\documentclass{article}
\usepackage{totcount}
\begin{document}

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

In total, my funding was \$\total{fund}.

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

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

\end{document}

I have tried the sistyle and siunitx packages in the Preamble, but to no avail.

\usepackage{sistyle}
    \SIthousandsep{,}

or

\usepackage[group-separator={,}]{siunitx}

How can I get a comma between these values that won't break the code?

6

Solution to the Updated Question

The question was updated after I posted my answer. Here is the solution to the updated one. This uses the refcount package.

You need to save the total in a label whenever you wish and then use this whenever you want.

\documentclass{article}
\usepackage{refcount}

\usepackage[group-separator={,}]{siunitx}

\makeatletter
\def\refcounter#1{%
  \protected@edef\@currentlabel
  {\csname p@#1\endcsname\csname the#1\endcsname}%
}
\makeatother

\newcounter{fund}
\def\fund#1{\addtocounter{fund}{#1}\$\num{#1}}
\def\SaveTotal#1#2{\refcounter{#1}\label{#2}}
\newcounter{showtotal}
\def\ShowTotal#1{\setcounterref{showtotal}{#1}\${\num{\arabic{showtotal}}}}

\begin{document}

After the second one, my total funding was \ShowTotal{totalOne}.

And my third and fourth funding made my total funding  \ShowTotal{totalTwo}.

My first funding was worth \fund{3000}.

My second funding was worth \fund{50000}.

\SaveTotal{fund}{totalOne}

Then, I got a third funding of \fund{150000}.

And finally got a fourth funding of \fund{50000000}.

\SaveTotal{fund}{totalTwo}

\end{document}

The Updated Output

enter image description here


Solution to the Original Question

The Basics

Merely using the siunitx package does not ensure (it should not) that all your numbers are automatically formatted. You need to explicitly call the \num command in order to have a number formatted.

So, I modified your \fund command little, and defined the \Total command to display the counter value.

(I took the liberty of putting in the $ symbol inside the macros.)

The Code

\documentclass{article}
\usepackage{totcount}

\usepackage[group-separator={,}]{siunitx}

\begin{document}

\newtotcounter{fund}
\newcommand{\fund}[1]{\addtocounter{fund}{#1}\$\num{#1}}
\def\Total#1{\$\num{\arabic{#1}}}

My first funding was worth \fund{3000}.

My second funding was worth \fund{50000}.

Thus, in total, my funding was \Total{fund}.

\end{document}

The Output

enter image description here

6
  • I've updated my question with one key difference. The total should be first. In order to get the total first, I'm using the totcount package. It seems your \def\Total#1{\$\num{\arabic{#1}}} breaks the behavior of the total line when it is placed above the first and second funding lines.
    – CephBirk
    Feb 22 '16 at 5:09
  • @CephBirk Yes, it should. The total value is being updated by the totcount package in the fund counter. When used first, the counter is yet to be set and has a value zero.
    – Masroor
    Feb 22 '16 at 5:16
  • @CephBirk I am not saying that what you are asking for is not doable. But your original question was about comma separating numbers which has already been addressed by my answer. Now, you have edited your original question to something like printing a future value in counter.
    – Masroor
    Feb 22 '16 at 5:44
  • @CephBirk See the updated answer.
    – Masroor
    Feb 22 '16 at 10:04
  • 1
    I apologize for the added challenge, but thanks for rising to the occasion.
    – CephBirk
    Feb 22 '16 at 13:34
1

You can implement your own totcount-like setup using \label-\ref:

enter image description here

\documentclass{article}

\usepackage{siunitx}
\sisetup{group-separator={,}}

\makeatletter
\newcommand{\newtotcounter}[1]{%
  \newcounter{tot:#1}% Create new counter
  \g@addto@macro\@enddocumenthook{% ~ \AtEndDocument...
    \def\@currentlabel{\protect\num{\arabic{tot:#1}}}% ... update \@currentlabel and ...
    \label{tot:#1}}}% ... mark with \label.
\makeatother

\newtotcounter{fund}

\newcommand{\fund}[1]{\addtocounter{tot:fund}{#1}\$\num{#1}}% Increment tot:fund counter
\newcommand{\total}[1]{\$\ref{tot:#1}}% Recall tot:#1 \label

\begin{document}

In total, my funding was \total{fund}.

My first funding was worth \fund{3000}.

My second funding was worth \fund{50000}.

\end{document}

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.