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?
Asked
Active
Viewed 4,293 times
26
Torbjørn T.
- 206,688
John Gowers
- 497
2 Answers
24
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.
Stefan Kottwitz
- 231,401
16
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}

Torbjørn T.
- 206,688
-
1When 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? – Misha 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. – Torbjørn T. Jul 27 '12 at 18:35
IEEEeqnarrayfrom theIEEEtrantoolspackage. I tried redefiningequations and the\[ ... \]so that they behaved asgathers 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? – John Gowers Dec 07 '11 at 16:01everydisplaywith\let\originaldisplaystyle\displaystyle \renewcommand\displaystyle{\color{black}\originaldisplaystyle}stopped the errors, and everything insidealigns orIEEEeqnarrays is beautiful and black (or red, if it's marked as being text); however, normalequations now render in red rather than in black, sincedisplaystyleis not part of the definition forequation. Is there some way of suppressingeverydisplayso that it only affects theequationenvironment? – John Gowers Dec 07 '11 at 18:41equationwould be an option. – Stefan Kottwitz Dec 07 '11 at 18:49\let\oldeq\equation \def\equation{\oldeq\color{black}}) but for some reason I can't do the same for theequation*environment: I keep getting errors. What is the problem here? – John Gowers Dec 08 '11 at 08:19equation*environment of amsmath. – Stefan Kottwitz Dec 08 '11 at 08:20