# Simpler way to add color to equations in math mode?

I was looking for a simpler way to add color to math mode. Specifically, I wanted different parts of an equation to have different colors in order to better correlate with colors in a figure. The only thing I could find that worked was using \begingroup and \group. While the code below worked, I was wondering if someone had a simpler way of doing it. I couldn't find anything else looking around...

\usepackage[usenames, dvipsnames]{color}
\definecolor{myred1}{RGB}{255, 0, 0}
\definecolor{myyellow1}{RGB}{255, 255, 219}
\definecolor{mygreen1}{RGB}{0, 255, 0}
\definecolor{mygreen2}{RGB}{0, 126, 0}
\definecolor{myblue1}{RGB}{0, 0, 255}

$$\frac{ \begingroup \textcolor{myblue1} {a} \endgroup } { \begingroup \textcolor{myred1} {b} \endgroup } = \frac{ \begingroup \textcolor{mygreen2} {a + b} \endgroup } { \begingroup \textcolor{myblue1} {a} \endgroup }$$

• Forgive me -- I'm new to posting on StackExchange. Didn't realize my cut-and-paste from TeXShop would not be formatted correctly. Nov 29, 2015 at 14:56
• You can use \color{myred1}{b}, for instance. Or even shorter, make a new command with \newcommand\myred[1]{{\color{myred1}{#1}}} and then use \myred{b} in the equation. Unfortunately you can't have commands with numbers, but I think you'll make do! Nov 29, 2015 at 15:04
• You need no \begingroup and \endgroup Nov 29, 2015 at 15:10

You need no \begingroup and \endgroup:

\documentclass{article}
\usepackage[usenames, dvipsnames]{color}

\definecolor{myred1}{RGB}{255, 0, 0}
\definecolor{myyellow1}{RGB}{255, 255, 219}
\definecolor{mygreen1}{RGB}{0, 255, 0}
\definecolor{mygreen2}{RGB}{0, 126, 0}
\definecolor{myblue1}{RGB}{0, 0, 255}

\begin{document}

$$\frac{\textcolor{myblue1}{a}}{\textcolor{myred1}{b}} = \frac{\textcolor{mygreen2}{a + b}}{\textcolor{myblue1}{a}}$$

\end{document}


A possibly better interface with xparse:

\documentclass{article}
\usepackage{xparse}
\usepackage[usenames, dvipsnames]{color}

\ExplSyntaxOn
\NewDocumentCommand{\colorfrac}{O{}mm}
{
\group_begin:
\keys_set:nn { khanna/colorfrac} { #1 }
\frac
{
\textcolor{\l_khanna_colorfrac_num_tl}{#2}
}
{
\textcolor{\l_khanna_colorfrac_den_tl}{#3}
}
\group_end:
}
\keys_define:nn { khanna/colorfrac }
{
num .tl_set:N  = \l_khanna_colorfrac_num_tl,
den .tl_set:N  = \l_khanna_colorfrac_den_tl,
num .initial:n = black,
den .initial:n = black,
}
\ExplSyntaxOff

\definecolor{myred1}{RGB}{255, 0, 0}
\definecolor{myyellow1}{RGB}{255, 255, 219}
\definecolor{mygreen1}{RGB}{0, 255, 0}
\definecolor{mygreen2}{RGB}{0, 126, 0}
\definecolor{myblue1}{RGB}{0, 0, 255}

\begin{document}

$$\colorfrac[num=myblue1,den=myred1]{a}{b} = \colorfrac[num=mygreen2,den=myblue1]{a + b}{a}$$

\end{document}


Despite the name, \textcolor can also be used in math mode without switching to text mode. But the implementation is not made with math in mind, because it uses curly braces as group for the color range:

\def\textcolor#1#{\@textcolor{#1}}
\def\@textcolor#1#2#3{\protect\leavevmode{\color#1{#2}#3}}


The curly braces have a side effect in math that the group is now a subformula with spacing of an ordinary math atom, compare:

\documentclass{article}
\usepackage{color}
\begin{document}
$a + b = c$
$\textcolor{green}{a}\textcolor{red}{+}\textcolor{blue}{b} \textcolor{magenta}{=}\textcolor{cyan}{c}$
\end{document}


The solution is to use \begingroup and \endgroup for the grouping:

\documentclass{article}
\usepackage{color}

\makeatletter
\def\mathcolor#1#{\@mathcolor{#1}}
\def\@mathcolor#1#2#3{%
\protect\leavevmode
\begingroup\color#1{#2}#3\endgroup
}
\makeatother

\begin{document}
$a + b = c$
$\mathcolor{green}{a}\mathcolor{red}{+}\mathcolor{blue}{b} \mathcolor{magenta}{=}\mathcolor{cyan}{c}$
\end{document}