2

Consider this code:

\documentclass{report}
\newcounter{counterA}
\newcounter{counterB}
\begin{document}
...
\setcounter{counterA}{2}
\setcounter{counterB}{3}
\addtocounter{counterB}{counterA}
\end{document}

When I compile I get the following error:

! Missing number, treated as zero.
<to be read again> 
                   c
l.8 \addtocounter{counterB}{counterA}

How to add the value in counterA to counterB?

  • 4
    \addtocounter{counterB}{\value{counterA}} – moewe Apr 5 at 15:37
  • 3
    In case that this is related to your page counter problem: due to the asynchronic page building of tex, the page counter is often not reliable in the text body. – Ulrike Fischer Apr 5 at 15:39
  • 1
    In case Ulrike's hunch is right, you may be interested in texfaq.org/FAQ-wrongpn – moewe Apr 5 at 15:41
7

You can access the value of a counter for arithmetic or other situations where LaTeX expects a number with \value{<countername>}, so

\addtocounter{counterB}{\value{counterA}}

should be what you are looking for.

You would use \the<countername> to print (a representation of) the value.

\thecounterB

Gives

5

in the example.

In rare situations or when you define \the<countername> you may have to use

\arabic{<countername>}
\roman{<countername>}
\Roman{<countername>}
\alph{<countername>}
\Alph{<countername>}

to force a particular printed representation.

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.