0

The code below is found here. It works normally: if \numexpr\totvalue{<counter>}is replaced by \the<counter> the result is wrong (0).

 \documentclass{article}

 \usepackage{totcount}
 \newtotcounter{totalpoints}
 \setcounter{totalpoints}{0}

 \begin{document}

 Total points are \the\numexpr\totvalue{totalpoints}
 \begin{enumerate}
 \item [5 points]\addtocounter{totalpoints}{5} Here is the first question.
 \item [6 points]\addtocounter{totalpoints}{6} Here is the second.
 \end{enumerate}


 \end{document}

I find that if the usual \the<counter>is placed after the last incrementation it provides the result properly.

Could someone explain what these two commands do?
\totvalueis most likely what allows the command for writing the number to be put anywhere in the source code but it might have some other use in this code, and in that case I'd like to know what it is.
As for \numexpr, in two cases of use that I tried this command is not needed, nor is it needed in the code shown above. Is this a TEX primitive? a LATEX command? What does it do generally? Why has it been added in the code above? In prevision of what possible problem has it been added?

LPH
  • 246
  • \the is a representation, not a value. \the\numexpr\value{<counter>} should work. – Ulrike Fischer May 15 '20 at 17:48
  • 1
    @UlrikeFischer The point is not whether \the\numexpr\value{<counter>} works or not and it surely doesn't when substituted in the above code to \the\numexpr\totvalue{<counter>}, but it is, except that \totvalueis most likely what allows the command for writing the number to be put anywhere in the source code, what is the use of \numexpr? Is that a TEX command?… – LPH May 15 '20 at 19:04

1 Answers1

2

General explanation:

In LaTeX2e,

  • \newcounter{<counter>} creates a count register \c@<counter> with initial value 0.
  • \(ref)stepcounter{<counter>} and \addtocounter{<conter>}{<num>} changes the value stored in \c@<counter>.
  • \the<counter> prints the value stored in \c@<counter>, by default in arabic style (e.g., 1, 2, 3, ...).

Package totcount extends the counter concept by registering two count registers in using \newtotcounter{<counter>}.

  • The first, \c@<counter> is registered by \newcounter{<counter>} and acts the same as LaTeX2e counters.
  • The second, \c@<counter>@totc is registered by \regtotalcounter{<counter>}.
  • The value in \c@<counter>@totc is in sync with that in \c@<counter> and is written to .aux file at the end of document, and is read at the beginning of document.
  • Hence starting from the second run, \c@<counter>@totc holds the value of \c@<counter> at the end of last run.

Specific to the linked answer:

In the linked answer to question Total number of points,

  • When the .tex file is run for the second time, at the beginning of document, the value of \c@totalpoints is 0 (because of \setcounter{totalpoints}{0}), but the value of \c@totalpoints@totc is 11 (5 + 6).
  • When you ues \thetotalpoints to replace \the\numexpr\totvalue{totalpoints}, the value of \c@totalpoints is print, which is 0.
  • (Recommended) When you use \total{totalpoints} to replace \the\numexpr\totvalue{totalpoints}, the value of \c@totalpoints@totc is print, hence you get 11.
  • When you use \totvalue{totalpoints}, you get the value of \c@totalpoints@totc, and you need prefix \the to print that value.
  • The \numexpr, a primitive originally introduced by eTeX (see The eTeX Manual, section 3.5) and now supported by all popular TeX engines, is used to writing number expressions. In the linked answer the \numexpr is not necessary.

To sum up, to print the total value of a counter (registered by \newtotcounter or extended by \regtotalcounter), using \total{<counter>} is enough.

muzimuzhi Z
  • 26,474