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](https://i.stack.imgur.com/rYQ8O.png)
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
If you load
xcolor
, you can use\colorbox
. This uses a regular "framed box", so you can set the values of\fboxsep
to decrease the border.In most cases, text-related formatting of this sort also transfers to math-related content. However, a verbatim switch is sometimes required since boxing reverts back to text mode. As such, use
and not
Here's a small example in LaTeX illustrating the use of
\colorbox
together with a modification of\fboxsep
: