How do I get different colors for math and for text?
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.
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.
\begin{equation}
f(x) = \sin (x) \text{ and } g(x) = e^x\cos(x)
\end{equation}
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}