[Tex/LaTex] How to color math symbols

colormacrosmath-mode

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).

Examples:

\documentclass{article}

\usepackage{xcolor}

\begin{document}

$ \color{red} C^2 $

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

\end{document}

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.