MathOverflow is a question and answer site for professional mathematicians. 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

This question may end up [closed], but I'm going to ask and let the people decide. It's certainly the kind of question that I'd ask people at tea, and it's not one whose answer I've been able find with Google.

TeX, I have heard, is Turing complete. In theory, this means that we can do modular arithmetic with LaTeX programs. I'd like to know how this can be done in practice.

Background: I've been using the \foreach command in TikZ to draw NxN arrays of nodes, indexed by pairs of integers (m,n). I'd like to be able to use modular arithmetic and an ifthenelse statement to put different decorations on the nodes, depending on the value of (m+n) mod p. Obviously, one can just do this by hand. But that's not the world I want to live in.

share|cite|improve this question
TeX is Turing complete, but LaTeX may not be (without getting your hands dirty and writing actual TeX). In any case, I wasn't even aware of the TikZ and PGF packages. Allegedly they interact okay with Beamer? I've got to try it out for my next talk. – Willie Wong Mar 20 '10 at 1:21
They were written so as to develop Beamer, in fact. – Mariano Suárez-Alvarez Mar 20 '10 at 1:21
Haha, I thought this question was about typsetting a paper in $\LaTeX$ – Dimensio1n0 Nov 8 '13 at 11:34
up vote 22 down vote accepted

Get a current version of TikZ and use \pgfmathmod!

share|cite|improve this answer
The best proof I can imagine of of TeX's Turing completeness is that TikZ is at all possible... – Mariano Suárez-Alvarez Mar 20 '10 at 1:14
...and, of course, Til Tantau's existance is ample proof that one can be amazingly smart and nice at the same time! – Mariano Suárez-Alvarez Mar 20 '10 at 1:21
There is an \pgfmathifthenelse function in TikZ, too. You should really browse the documentation, the first half of Part VII. – Mariano Suárez-Alvarez Mar 20 '10 at 1:40
You can always use Google's computers!… – Mariano Suárez-Alvarez Mar 20 '10 at 1:48
With Mariano's help, I got my code to work out. I'm posting an example here, so that people don't have to guess how it's done. \begin{tikzpicture} \foreach \x in {0,...,4} \foreach \y in {0,...,4} \pgfmathparse{mod(\x+\y,2)} \let\tf\pgfmathresult \ifthenelse{\equal{\tf}{1.0}}{\draw (\x,\y) node [circle,draw] {}}{\draw (\x,\y) node [circle,draw,fill] {}}; \end{tikzpicture} – userN Mar 20 '10 at 3:38

I find TikZ unnecessary complicated for such an easy task as modular arithmetic. TeX has commands for addition/subtraction, multiplication, and division (\advance, \multiply, \divide). To perform modular addition or subtraction you need to perform ordinary addition or subtraction and then subtract or add p if necessary. Modular multiplication is only slightly more complicated: Perform ordinary multiplication, divide and multiply the product by p and subtract the result from the original product, obtaining the remainder.

share|cite|improve this answer
Of course, he wants modular arithmetic to facilitate producing figures with certain features. He's using TikZ anyway, in order to produce these figures; that's the point of his question. --- Sure he can use e.g. \ifnum and \advance to perform modular arithmetic by hand with TeX, rather than \pgfmathmod as described in another answer. But unless there is a substantial difference in performance, the best programming practise (i.e. the best practise in any form of documented problem-solving) is to use the more 'abstract' rather than the more 'explicit' technique. – Niel de Beaudrap Mar 20 '10 at 9:57

I've had to implement a lot of code in TeX while writing a package for drawing spectral sequences (sseq.sty). pgf makes computations in TeX easier, but if you really have a significant amount of code, I recommend you take a look at luatex/lualatex, which is included in many distributions and merges TeX with an interpreter for the easy-and-fast language Lua, which you can learn in a couple of hours. It's much more readable and speeds up the typesetting enormously.

TeX may be Turing complete (LaTeX as well, by the way), but so is a Turing machine, and you wouldn't want to implement modular arithmetic on a Turing machine, would you?

share|cite|improve this answer

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.