[Tex/LaTex] How to color math symbols


One can use the command such as \textcolor[rgb]{1.00,0.00,0.00}{boundary} to color the word "boundary" in a document.

Here is my question:

How to color the math symbols in $$? For example, can one turn $C^{2}$ into red in LaTeX?

Best Answer

\textcolor from the xcolor package also works in mathmode, even if the name says otherwise. You can also use $ \color{<color>} C^2 $. It sets the color for the rest of the current scope (group).





$ \color{red} C^2 $

$ \color{yellow} A = \textcolor{blue}{B} \mathbin{\textcolor{red}{-}} \textcolor{green}{C} $


enter image description here

Note that the \mathbin must be wrapped around the coloured, binary - here, as Leo Liu pointed out in his comment, to set the correct spacing again. The coloring changes the math type so they spacing would be different.