# Many errors given by the calc package

I know that the title is not clear at all; the issue is that I'm trying to use the calc package to compute the value of a certain counter, and I receive two different error messages, according to some little changes that I do in the code. I have the impression both these issues come from something common that I'm not getting. I'll divide this thread into two points, each of them describing a different error, with the relative MWE.

1) The following code should print on the page the value 3 (because the decimal numbers disappear, according to the package documentation). Instead I receive the Missing number, treated as zero error. If I run the code despite the warning, on the paper will be printed a string composed by the characters 2! 5.

\documentclass[11pt, a4paper]{article}

\usepackage{calc}

\newcounter{a}

\begin{document}

\setcounter{a}{\ratio{7}{2}}
\thea

\end{document}


The complete message that appears in the terminal if I run this code is the following. I'll include what goes after I tell it to ignore the error and run either way (r).

alex@Sargon:~/Documenti/Progetto Perturbator/bin/mwe$pdflatex mwe.tex This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex) restricted \write18 enabled. entering extended mode (./mwe.tex LaTeX2e <2017/01/01> patch level 3 Babel <3.9r> and hyphenation patterns for 83 language(s) loaded. (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size11.clo)) (/usr/share/texlive/texmf-dist/tex/latex/tools/calc.sty) (./mwe.aux) ! Missing number, treated as zero. <to be read again> \ratio l.9 \setcounter{a}{\ratio{7}{2}} ? r OK, entering \nonstopmode... ! Package calc Error: 7' invalid at this point. See the calc package documentation for explanation. Type H <return> for immediate help. ... l.9 \setcounter{a}{\ratio{7}{2}} [1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./mwe.aux) ) (\end occurred inside a group at level 1) ### semi simple group (level 1) entered at line 9 (\begingroup) ### bottom level (see the transcript file for additional information)</usr/share/texlive/texmf-d ist/fonts/type1/public/amsfonts/cm/cmr10.pfb> Output written on mwe.pdf (1 page, 10687 bytes). Transcript written on mwe.log.  2) If I try to multiply the result of the \ratio command I receive an error which says Illegal unit of measure (pt inserted). The MWE, very similar, is the following: \documentclass[11pt, a4paper]{article} \usepackage{calc} \newcounter{a} \begin{document} \setcounter{a}{2*\ratio{7}{2}} \thea \end{document}  Notice that in this case, despite the warning, if I run the code ignoring the alert I will actually see the expected value (7) printed on the paper. Again I'll include all the terminal's speech, including the part after (r). alex@Sargon:~/Documenti/Progetto Perturbator/bin/mwe$ pdflatex mwe.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Debian) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./mwe.tex
LaTeX2e <2017/01/01> patch level 3
Babel <3.9r> and hyphenation patterns for 83 language(s) loaded.
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size11.clo))
(/usr/share/texlive/texmf-dist/tex/latex/tools/calc.sty) (./mwe.aux)
! Illegal unit of measure (pt inserted).
<to be read again>
!
l.9     \setcounter{a}{2*\ratio{7}{2}}

? r
OK, entering \nonstopmode...
! Illegal unit of measure (pt inserted).
<to be read again>
!
l.9     \setcounter{a}{2*\ratio{7}{2}}

[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./mwe.aux) )
(see the transcript file for additional information)</usr/share/texlive/texmf-d
ist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on mwe.pdf (1 page, 8364 bytes).
Transcript written on mwe.log.


How to proceed? I carefully read all the parts related to the \ratio command on the package documentation, at least the ones I could get, and I really don't know what's wrong with this.

• \ratio requieres a dimension, not a number. See documentation. – Marco Daniel Mar 19 '17 at 21:49
• Depending on the use - case, \fpeval from xfp would be easier, perhaps – user31729 Mar 19 '17 at 22:07
• Counters only accept integer values to begin with. What's the intended usage? This seems like an XY problem. – egreg Mar 19 '17 at 22:19
• the value is converted to an integer at the end of the calculation, by cutting out the decimal part – il mietitore Mar 20 '17 at 8:59

\ratio needs to use dimension values, e.g. \ratio{7pt}{2pt}, but even this is not sufficient. In order to apply it with \setcounter or \setlength, there must be an explicit multiplication, with either some number or a length, see the examples below.

\documentclass[11pt, a4paper]{article}

\usepackage{calc}

\newlength{\foo}
\newcounter{a}
\newlength{\foobar}
\newlength{\foobarother}
\setlength{\foobar}{7pt}
\setlength{\foobarother}{2pt}

\begin{document}

\setcounter{a}{1*\ratio{7pt}{2pt}}
\setlength{\foo}{1pt*\ratio{\foobar}{\foobarother}}

\the\foo\ or \thea

\end{document}


The result is 3.5pt for \the\foo and 3 for \thea.

• Brilliant! The terminal saying that the measure unit is wrong instead of it being missing is quite misleading, but anyway, cool that I figured this out – il mietitore Mar 20 '17 at 9:03
• @ilmietitore: I am not sure I do understand you. The terminal says the measure unit is wrong? – user31729 Mar 20 '17 at 22:53
• well, in the mwe it said Illegal unit of measure (pt inserted)`. If instead of being it's "illegal" it said it was plainly "missing" probably finding this sort of error would have been much simpler. – il mietitore Mar 21 '17 at 11:23