# colored math with the align environment

I want different colors for the text and math environments. I use the align environment. The code below used to work, but very recently (matter of weeks at most) has broken. Quite frankly, I cannot quite remember why I carry the bit \def\m@th{\mathsurround\z@\normalcolor}, which is now causing a break. I may have read about it there. Because I have forgotten the details, I'm not sure if removing this line of code altogether is the best course.

Objective: To have the math in blue and the text under the braces in black (the braces themselves in black is probably better, but either way will do).

Without the offending code:

With the offending code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

\everymath{\color{blue}}
\makeatletter
\def\m@th{\mathsurround\z@\normalcolor}% Can't remember the reason for this
\makeatother

\begin{document}
Some inline math $C_{0}$.
\begin{align*}
y = {\underbrace{
\left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right]
}_\text{growth rate}} \times
{\underbrace{
\vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0
}_\text{initial}}
\end{align*}
\end{document}


To enforce black-coloured text you can add \color{black} to the definition of \textdef@ from amsmath.sty.

To change the colour of the braces you can add that to the definition of \upbracefill (for \underbrace) and \downbracefill (for \overbrace). But I would go for defining a command, say, \underbraceColor that takes an optional argument to change the color.

I also changed \everymath{\color{blue}} to \everymath\expandafter{\the\everymath\color{blue}} to keep possible previous contents of \everymath there:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

\everymath\expandafter{\the\everymath\color{blue}}
\makeatletter
% To enforce black text (taken from amstext.sty)
\def\textdef@#1#2#3{\hbox{{\color{black}%
\everymath{#1}%
\let\f@size#2\selectfont
#3}}}
% To make the braces black (taken from fontmath.sty)
\newcommand{\overbraceColor}[2][.]{%
\mathop{\vbox{\m@th\ialign{##\crcr\noalign{\kern3\p@}%
\downbracefillColor{#1}\crcr\noalign{\kern3\p@\nointerlineskip}%
$\hfil\displaystyle{#2}\hfil$\crcr}}}\limits}
\newcommand{\underbraceColor}[2][.]{%
\mathop{\vtop{\m@th\ialign{##\crcr
$\hfil\displaystyle{#2}\hfil$\crcr
\noalign{\kern3\p@\nointerlineskip}%
\upbracefillColor{#1}\crcr\noalign{\kern3\p@}}}}\limits}
\def\downbracefillColor#1{$\color{#1}\m@th \setbox\z@\hbox{$\braceld$}% \braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru \bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd$}
\def\upbracefillColor#1{$\color{#1}\m@th \setbox\z@\hbox{$\braceld$}% \bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd \braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}
\makeatother

\begin{document}
\pagenumbering{gobble}
Some inline math $C_{0}$.
\begin{align*}
y = {\underbraceColor[black]{
\left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right]
}_\text{growth rate}} \times
{\underbraceColor{
\vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0
}_\text{initial}}
\end{align*}
\end{document}


If you prefer to set the color of the braces only once you can use this instead:

% To make the braces black (taken from fontmath.sty)
\def\downbracefill{$\color{black}\m@th \setbox\z@\hbox{$\braceld$}% \braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru \bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd$}
\def\upbracefill{$\color{black}\m@th \setbox\z@\hbox{$\braceld$}% \bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd \braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}

• I went for the shorter code. Coloring under/over braces in the same color as the text makes total sense. Thanks. Oct 26 '18 at 14:58
• @PatrickT Yes, if you want a document-wise change the second approach requires less effort. You're welcome :) Oct 26 '18 at 15:01

Redefining \m@th is not the best idea. Don't forget \everydisplay.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{xpatch}

\everymath{\color{blue}}
\everydisplay{\color{blue}}
\xpretocmd{\upbracefill}{\blacktext\everymath{\normalcolor}}{}{}
\xpatchcmd{\underbrace}{\limits}{\blacktext\limits}{}{}
\makeatletter
\newcommand{\blacktext}{%
\xpatchcmd{\text@}{\mathchoice}{\normalcolor\mathchoice}{}{}%
}
\makeatother

\begin{document}
Some inline math $C_{0}$.
$$y = {\underbrace{ \left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right] }_{\text{growth rate}}} \times {\underbrace{ \vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0 }_{\text{initial}}}$$
\end{document}


• thanks for your suggestion. I have hundreds of pages of align environments, not equation, is there a way to make this work with align? If I replace equation with align the compilation fails with ! Improper \halign inside 's. Oct 26 '18 at 14:39
• @PatrickT Yes, remove \everydisplay{\color{blue}}. Something else has to be patched if you happen to use equation. On the other hand, since you're doing bad typesetting anyway (the colored math), use align or gather instead of equation. Oct 26 '18 at 14:50
• thanks a lot for your reply. I like that you use xpatch. My clock shows your answer and Phelype's at the same time, and I noticed you have a few more points (!), so I selected his answer. But thank you very much. Oct 26 '18 at 14:52
• @PatrickT You're welcome! You did well. :-) Oct 26 '18 at 14:56

You can try to pach \text as follows (the braces will be in blue but the text in black). The following code needs to replace _\text{growth rate} by _{text{growth rate}} (nevertheless, for me, _\text{growth rate} seems strange).

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}

\everymath{\color{blue}}
\makeatletter
\let\oldtext@\text@
\def\text@{\color{black}\oldtext@}
\makeatother

\begin{document}
Some inline math $C_{0}$.
$$y = {\underbrace{ \left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right] }_{\text{growth rate}}} \times {\underbrace{ \vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0 }_{\text{initial}}}$$
\end{document}

• This doesn't work :/ Oct 26 '18 at 12:52
• @Phelype Oleinik : you're right. I have modified my answer. Oct 26 '18 at 13:05
• Thanks for answering and your comment about the curly braces around the text. I usually do add braces. In this case, I got the math code from Mico's answer linked at the end of this sentence, where the braces were absent. Braces don't seem to be needed in that particular application: tex.stackexchange.com/questions/154329/… Oct 26 '18 at 14:56
• Yes, but the syntax is not uniform. You can type $A_\text{yes}$ but you can't type $A_\sqrt{2}$. Oct 26 '18 at 18:23