[Tex/LaTex] Missing number, treated as zero with amsmath and calc

amsmathcalculationscounters

This simple code produces

"Missing number, treated as zero"

\documentclass{article}
\newcounter{test}
\newcounter{testtwo}
\usepackage{calc}
\usepackage{amsmath}
\begin{document}
 \addtocounter{test}{\value{testtwo}*\value{testtwo}}
\end{document}

In the following situations compiles good:

  • Without amsmath
  • When calc is loaded after amsmath
  • using \setcounter instead of \addtocounter

What is happening here?

If a class loads calc and we need amsmath, what can we do? I know that a solution in this case would be loading amsmath before the class with \RequirePackage. It is possible another workaround between both \usepackage?

Best Answer

A quicker workaround, regardless which package is loaded first, is to use \numexpr from e-tex extensions (which should be available for basically any TeX distribution nowadays).

It expands the values of the calculation before it's advance by \addtocounter

In the following MWE the result is, as expected, 100

\documentclass{article}
\newcounter{test}
\newcounter{testtwo}
\usepackage{calc}
\usepackage{amsmath}
\begin{document}
\setcounter{testtwo}{10}

\addtocounter{test}{\numexpr\value{testtwo}*\value{testtwo}}

\thetest
\end{document}