I'd like to print the remainder of the Euclidean division by 4 of a latex counter. How do I do this?
2 Answers
In OpTeX, you can use \expr macro:
\def\remainderofdiv#1#2{\expr[0]{#1\%#2}}
\remainderofdiv{12}{5}
\bye
or, you can use \directlua directly:
\def\remainderofdiv#1#2{\directlua{tex.print(#1\%#2)}}
If you want to implement \mycountermodfour then you can use the previously defined macro:
\newcount\mycounter
\def\mycountermodfour{\remainderofdiv{\the\mycounter}{4}}
\mycounter=7
\mycountermodfour
\bye
- 74,238
I take it that you are actually looking for calculating a mod b. There is probably a wide range of options how to solve this. Using Expl3 syntax, you could do:
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\remainderofdiv}{ m m }{
\int_mod:nn { #1 } { #2 }
}
\ExplSyntaxOff
\begin{document}
\remainderofdiv{5}{2}
\remainderofdiv{12}{2}
\remainderofdiv{12}{5}
\end{document}
So, given that you have a counter that contains x and now you want to output x mod 4:
\documentclass{article}
\newcounter{mycounter}
\ExplSyntaxOn
\NewDocumentCommand{\mycountermodfour}{ }{
\int_mod:nn { \value{mycounter} } { 4 }
}
\ExplSyntaxOff
\begin{document}
\setcounter{mycounter}{7}
\mycountermodfour
\end{document}
- 48,848
