# Get value of a counter

Consider this code:

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


When I compile I get the following error:

! Missing number, treated as zero.
c


How to add the value in counterA to counterB?

• \addtocounter{counterB}{\value{counterA}} Apr 5 '19 at 15:37
• 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. Apr 5 '19 at 15:39
• In case Ulrike's hunch is right, you may be interested in texfaq.org/FAQ-wrongpn Apr 5 '19 at 15:41

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.