# Multiplication and division with counters while also formatting

See the code that follows.

The user gives some pairs of numbers. Each time they happen also to be displayed.

Finally they are added up in a specific way.

\documentclass{article}

%\def\Result...

\newcount{\A}\A=0

\newcount{\B}\B=0

\newcount{\C}\C=0

\begin{document}
\XYZ{4,30}

\XYZ{7,30}

\XYZ{9,40}

%\Result

\end{document}


This kind of code prevents the document from ever compiling.

1. What is wrong with it? How can it be fixed?
2. Can it be rewritten using \newcounter, \setcounter, \countainedwithin*, and other LaTeX methods? I am interested in knowing both the correct TeX and LaTeX methods to achieve the result.
3. How does one write a macro \Result that takes \C and divides by 100 and if the result is three digits EFG, writes \textbf{E},\textbf{FG}, and if the result is four digits EFGH, writes \textbf{EF},\textbf{GH} instead?
4. In case it has fewer than three digits, to add 0 for G or 00 for FG and use the three digit case.

I am looking for something that does not involve the calc package by the way. A result rounded to the nearest FG or GH in \Result is also fine.

For the \Result

\def\Result{\divide\C by 100\textbf{\the\C}}


is working now - but now sure how to handle rounding up to nearest H in the EF.GH.

If it did work, not sure how to handle the fractional part and cut off to at most two digits on each side of a dividing marker.

• It does not combine independent of what \Result is defined as being. So the first problem is in the counters. – Gottfried William Apr 6 '20 at 21:45
• Also the issue may be in using {#1,#2} in the definition. However it is not ideal to have to enter pairs by \XYZ{X,Y.} in the use document, if doing something like \def\XYZ#1,#2.{... – Gottfried William Apr 6 '20 at 21:50
• \def\XYZ{#1,#2} is a syntax x error. – David Carlisle Apr 6 '20 at 21:53
• for part 3 of your question \C / 100 in this example is 21 so neither three nor 4 digits, what output do you want in this case? – David Carlisle Apr 6 '20 at 22:07
• Usual syntax for \newcount is to give the argument without braces (it's a plain TeX macro). Not that it doesn't work with the braces, but I find it a bit strange (if you were to play this game with primitives such as \let or \ifx, you'd get bitten). – frougon Apr 7 '20 at 0:13

It's not at all clear what calculation or output you want so I fixed the syntax errors, left the calculation, then made it typeset the values of A B and C

\documentclass{article}

%\def\Result...

\newcount{\A}\A=0

\newcount{\B}\B=0

\newcount{\C}\C=0

\def\XYZ#1{\XYZa#1\relax}
\def\XYZa#1,#2\relax{\textbf{#1}:\textbf{#2}%
\A=#1\relax
\B=#2\relax
\multiply\A by 100\relax
[\the\A,\the\B,\the\C]%
}

\begin{document}
\XYZ{4,30}

\XYZ{7,30}

\XYZ{9,40}

%\Result

\end{document}

• I see, \relax is the "magic" in these cases. – Gottfried William Apr 6 '20 at 22:01
• @GottfriedWilliam not really: mostly they are not needed but you had not terminated any of the assignments \A=#1 would set \A to be #1 followed by whatever digits the following tokens expand to, you were just lucky that the following tokens don't expand to digits here. Using \relax makes the end of the assignment explicit. The main change is fixing the \def syntax and making it output some text. – David Carlisle Apr 6 '20 at 22:04