Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
7  
Have mercy for colorblinds please. –  percusse Mar 2 '13 at 22:08
    
See tex.stackexchange.com/q/335/86 (possibly a duplicate) –  Loop Space Mar 2 '13 at 23:49

2 Answers 2

up vote 13 down vote accepted

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

share|improve this answer
    
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). –  user700902 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.

\documentclass{article}
\usepackage{unicode-math,color}
\setmathfont[Color=0000FF]{xits-math.otf}
\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
\everymath{\color{blue}}
\everydisplay{\color{blue}}
\begin{document}
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}\)
\[
\frac{\sqrt{x+1}}{x+1}
\]

\end{document}

enter image description here

share|improve this answer
    
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...) –  user700902 Mar 2 '13 at 23:09
    
I tried, and after adding explicitely \usepackage{color} to the preamble, it does work. –  user700902 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. –  user700902 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...) –  user700902 Mar 3 '13 at 21:24

Your Answer

 
discard

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.