[Tex/LaTex] LaTeX color setting for math mode

colormath-mode

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!

Best Answer

With no warranty of any kind!

\documentclass{article}
\usepackage{color}


\makeatletter
\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \mathcode`#1="8000
    \toks@\expandafter{\csname orig:math:#1\endcsname}%
    \begingroup
       \lccode`~=`#1
       \lowercase{%
    \endgroup
       \edef~{{\noexpand\color{#2}\the\toks@}}}%
   }
\@for\@tempa:=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\do{%
    \expandafter\colorizemath\@tempa{green}}
\@for\@tempa:=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\do{%
    \expandafter\colorizemath\@tempa{green}}
\@for\@tempa:=0,1,2,3,4,5,6,7,8,9\do{%
    \expandafter\colorizemath\@tempa{red}}
\makeatother

\everymath{\color{blue}}
\everydisplay{\color{blue}}

\begin{document}\thispagestyle{empty}

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\]
\end{document}

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).

Related Question