I'm wondering if it's possible to do something like this in LaTeX:

For all numbers (0, 1, ..., 9) in math mode, color it red. For alphabets (a, b, ..., z, A, B, ..., Z) in math mode, color it green. For everything else in math mode, color it blue.

I want to make my document more colorful, but doing manual coloring takes too much time!


With no warranty of any kind!


\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \toks@\expandafter{\csname orig:math:#1\endcsname}%



Hello $world$. Do you know that $E=mc^2$? 

\[ \widehat f(\omega) = \int_{-\infty}^\infty f(x) e^{-2\pi i \omega x}\,dx\]

\[ (I - M)^{-1} = \sum_{k=0}^\infty M^k\]

colorized math

Let me add, with regards to \everymath and \everydisplay that it would have been better to do:

\everymath\expandafter{\the\everymath \color{blue}}
\everydisplay\expandafter{\the\everydisplay \color{blue}}

This preserves, rather than erases, the previously stored data in these token lists. (I just checked and Lamport's book does not have a single mention of token list, and even the word token is not to be found (it seems) in the entire book...). Admittedly, packages who put things in them should do that At Begin Document so even the brutal way used in my initial code, as long as it is in the preamble, is maybe not that destructive. People interested in token lists can learn about it in, for example, TeX by Topic by Victor Eijkhout (texdoc topic).

  • 1
    ooh I just did one very similar to this but you beat me by 20 seconds so I#ll +1 yours instead of posting mine:-) – David Carlisle Mar 2 '13 at 22:52
  • @DavidCarlisle ... I was wondering who was commenting at the speed of light! ;-) note: I should have written \everymath\expandafter{\the\everymath \color{blue}}... as some package do use it (idem for \everydisplay). – user4686 Mar 2 '13 at 22:54
  • 1
    Nope, it's still readable. One should randomly rotate every letter CW and number CCW to make it unambigiously math. We should also blur the text. It's trivially readable with black. Too easy. :) – percusse Mar 2 '13 at 23:48
  • 1
    @percusse Did someone say Cthulhu-worshipping madman? ;) – Qrrbrbirlbel Mar 3 '13 at 3:06
  • @Qrrbrbirlbel More like Blur text so it's not readable – percusse Mar 3 '13 at 10:18

With unicode-math you can customize colors quite a lot, by specifying the font for different ranges, where a range can be a font command, e.g. \mathit, \mathbf etc., or a Unicode range. Color settings won't affect fraction lines and the horizontal line in square roots, as explained in color of unicode-math in xetex, but adding \everymath{\color{blue}} \everydisplay{\color{blue}} as in user700902's answer fixes that. No idea why the hat is red.

unicode-math requires that one compiles with xelatex or lualatex.

\setmathfont[range={\mathit,\mathup,\mathbfup},Color=00FF00]{xits-math.otf} % mathup is used for operators I think, \mathbf actually gives \mathbfup it seems 
\setmathfont[range=\mathit/{greek,Greek},Color=0000FF]{xits-math.otf} % sets greek letters to blue
\setmathfont[range="0030-"0039,Color=FF0000]{xits-math.otf} % unicode hex range of 0-9
Hello $world$. Do you know that $E=mc^2$? 
\widehat f(\omega) = \int_{-\infty}^\infty f(x) e^{-2\pi i \omega x}\,dx
And \( \tan x = \sin x /\cos x \).
\[ (I - M)^{-1} = \sum_{k=0}^\infty M^k\]

Even fractions and square roots \(\frac{\sqrt{x+1}}{x+1}\)


enter image description here

  • if you add \color{blue} to the \everydisplay and \everymath token lists, will it still work for letters and digits (hence fixing the square root sign and fraction problems)? (I guess I should try...) – user4686 Mar 2 '13 at 23:09
  • I tried, and after adding explicitely \usepackage{color} to the preamble, it does work. – user4686 Mar 2 '13 at 23:17
  • The hat is red, but I guess using a Unicode range for the Latin letters rather than the scope of math alphabets commands would fix that. – user4686 Mar 2 '13 at 23:21
  • @user700902 I'll add the \everydisplay/\everymath thing to the answer. For the hat, the only red comes from the unicode range of the numbers, using the unicode range of latin letters didn't change that, IIRC from my test. – Torbjørn T. Mar 2 '13 at 23:30
  • sorry for the red hat confusion... it was late and I was tired. Your proposed method seems to me safer than the approach using math activation (the main danger with math activation is if someone redefines the active character to expand to its catcode 12 self. Infinite loop...) – user4686 Mar 3 '13 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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