TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't want to display the modulo symbol, I want to programmatically calculate n modulo 3 and display the result.

share|improve this question

migrated from stackoverflow.com Nov 11 '11 at 0:54

This question came from our site for professional and enthusiast programmers.

up vote 23 down vote accepted

You can also use \intcalcMod from the intcalc package:


  {$\themycount\pmod 3=\intcalcMod{\value{mycount}}{3}$\\\stepcounter{mycount}}

\noindent A little example: $8\pmod 3=\intcalcMod{8}{3}$

\noindent And a little loop:\\


enter image description here

The code that appears in the link posted in a comment, there are some spurious blank spaces producing an undesired indentation of the first line; here's a corrected version:


\noindent\forloop{i}{1}{\value{i} < 101}{%
share|improve this answer
! Missing number, treated as zero. <to be read again> v l.32 } – mcandre Nov 11 '11 at 1:13
Thanks, that helps. I'm writing FizzBuzz for LaTeX, so I need to use the mod result in further calculations, not just display it. – mcandre Nov 11 '11 at 1:14
@mcandre: nothing prevents you from using the result in your calculations! – Gonzalo Medina Nov 11 '11 at 1:15
Hmm. When I do this, I get ! Missing number, treated as zero. github.com/mcandre/mcandre/blob/master/latex/fizzy.tex – mcandre Nov 11 '11 at 1:30
You are using "value" in your code and you should use \value (with a backslash). Also, you could have included that code in an edit to your original question. – Gonzalo Medina Nov 11 '11 at 1:34

There are several nice answers using different packages. I'd like to note that TeX uses integer arithmetics, so it is easy to program the standard formula a-(a/b)*b, where / means integer division.

Plain TeX solution:

        \divide\tmpcnta by #2
        \multiply\tmpcnta by #2
        \multiply\tmpcnta by -1
        \advance\tmpcnta by #1\relax

LaTeX solution:

        \divide\@tempcnta by #2
        \multiply\@tempcnta by #2
        \multiply\@tempcnta by -1
        \advance\@tempcnta by #1\relax
share|improve this answer
I used a similar solution in a calendar. – starblue Nov 11 '11 at 14:21
Why can't you write \divide#1 by #2 or \divide\number\numexpr#1\relax by #2? – A.Ellett Dec 19 '13 at 7:52

The fp package is small and provides the functionality to do quite complex arithmetic. In the minimal example below the macro \modulo{<a>}{<b>} stores the result of <a> mod <b> in the macro \result, which is then directly printed:

enter image description here

\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
Some modular arithmetic:
  \item $512 \pmod{7}=\modulo{512}{7}$
  \item $6 \pmod{4}=\modulo{6}{4}$
  \item $15 \pmod{4}=\modulo{15}{4}$
  \item $1234567 \pmod{3}=\modulo{1234567}{3}$

Since the result is stored in \result, it can be used later in the text as well, until another execution of \modulo will overwrite \result.

Similar functionality in terms of mathematical functions is provided with pgf as well.

share|improve this answer
! Undefined control sequence. \FP@@upn ...on \string "#2\string "}\edef \FP@tmp {[#2]}\expandafter \FP@upn... l.38 } – mcandre Nov 11 '11 at 0:42
Do you receive this error when compiling my MWE? – Werner Nov 11 '11 at 0:47
Yes. Specs: TeXworks on Mac OS X 10.7.2. – mcandre Nov 11 '11 at 0:53
What distribution of TeX do you have installed? I have TeX Live 2011 with fp verion 1995/04/02. – Werner Nov 11 '11 at 0:58
I like that a LaTeX solution to doing modular arithmetic is to load a package designed to do floating point calculations. WHAT!? – Seamus Feb 2 '12 at 17:08

The "expandable" version, using e-TeX's \numexpr:


\truncdiv and \moduloop can be plugged into other expressions. It's necessary to do like this because \numexpr performs rounded integer division.

share|improve this answer
the same idea was used in the calendarweek TeX package. – ogerard Jun 5 '13 at 9:27

Another solution is to use pgfmath


\pgfmathparse{mod(20,6)} \pgfmathresult %displays 2.0
\pgfmathtruncatemacro{\myint}{ \pgfmathresult}  

\myint %displays 2 
share|improve this answer

LaTeX3 (the expl3 package) also has a facility for computing modulus (moduli?), namely \int_mod:nn.

  The residue of $45$ modulo $19$ is $\mymod{45}{19}$.
share|improve this answer

You can use the calculator package then, type this code:


share|improve this answer
Nice, a new package! But you might want to make it clearer that you are the package author... – clemens Jun 12 '12 at 10:35
How nice! :) Feel free to add your package to our list. And by the way, welcome to TeX.sx! :) – Paulo Cereda Jun 12 '12 at 10:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.