# How do I get different colors for math and for text?

I want to include something in my LaTeX article that will make all mathematical formulae appear in black, while all text (including everything inside \text{}s in the middle of mathematical formulae) appear in red. Ideally I would like this to be something I could include in the preamble or in a .cls file or something like that. What would be the quickest way to do that?

A quick way is using \everymath, \everydisplay and the everysel package:

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{everysel}
\EverySelectfont{\color{red}}
\everymath{\color{black}}
\everydisplay{\color{black}}
\begin{document}
text $x=0$
$\text{Text in math, }y= 1$
\end{document}


However, with more complicated amsmath environments such as align there could could be problems with \everydisplay, see: Modifying \everydisplay causes the align environment to stop working. If you would like to go this way, perhaps omit \everydisplay as align uses inline math internally, and redefine basic displayed math otherwise for using the desired color.

• But you're right; this does produce an error when I combine it with the IEEEeqnarray from the IEEEtrantools package. I tried redefining equation s and the $...$ so that they behaved as gather s and I could get away without the \everydisplay, but that didn't work very well. What sort of thing did you have in mind when you talked about redefining basic displayed math otherwise? Dec 7 '11 at 16:01
• Update: replacing the everydisplay with \let\originaldisplaystyle\displaystyle \renewcommand\displaystyle{\color{black}\originaldisplaystyle} stopped the errors, and everything inside align s or IEEEeqnarray s is beautiful and black (or red, if it's marked as being text); however, normal equation s now render in red rather than in black, since displaystyle is not part of the definition for equation. Is there some way of suppressing everydisplay so that it only affects the equation environment? Dec 7 '11 at 18:41
• @Donkey_2009: Redefining equation would be an option. Dec 7 '11 at 18:49
• Thanks - I've now done that (using \let\oldeq\equation \def\equation{\oldeq\color{black}} ) but for some reason I can't do the same for the equation* environment: I keep getting errors. What is the problem here? Dec 8 '11 at 8:19
• @Donkey_2009: This could be a news question on the site, better than in a comment - how to redefine the equation* environment of amsmath. Dec 8 '11 at 8:20

If you can use xelatex or lualatex to compile the document, a combination of fontspec and unicode-math can help:

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Color=FF0000]{Latin Modern Roman}
\setsansfont[Color=FF0000]{Latin Modern Sans}
\setmonofont[Color=FF0000]{Latin Modern Mono}

\usepackage{lipsum}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont[Color=000000]{lmmath-regular.otf}

\begin{document}
A bunch of text, then an equation.
$$f(x) = \sin (x) \text{ and } g(x) = e^x\cos(x)$$
Some \textsf{inline} math $$a = b$$, and then an \texttt{align}
\begin{align}
N^2 &= -\frac{g}{\rho_0} \frac{\partial \rho}{\partial z} \\
N &= \sqrt{-\frac{g}{\rho_0} \frac{\partial \rho}{\partial z}}
\end{align}
\end{document}


• When I am trying to compile your example, but setting black for text and red for math I get funny result when most math is red, but some elements (horizontal lines in fractions and roots) are still black. Is this a bug of unicode-math? Jul 27 '12 at 18:24
• @Misha I honestly don't know, perhaps it has to with how those lines are rendered. Edit: I would recommend asking a follow-up question about this. Jul 27 '12 at 18:35