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}
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
).
Best Answer
Here is a way:
However this solution is a little impractical for inline
math mode
. I suggest to use this trick only fordisplay math mode
, and use a contracted extra command when inline. Doing this with\everymath
and\everydisplay
is unsafe, as mentioned here:\everymath
and author color