Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Sort of related question: tex.stackexchange.com/q/335/86 –  Loop Space Jun 25 '11 at 19:50
    
In \eqnarray environment, I find that curly bracketing can control the range of coloring. For example, {\color{red}{some symbols}} will ensure that math symbols after '}' are not colored. –  EKG Jun 13 '12 at 19:56
    
Don't use eqnarray. –  kahen Oct 16 '12 at 0:28

2 Answers 2

up vote 54 down vote accepted

\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}

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.

share|improve this answer
13  
There's a little problem: \textcolor{red}{-} makes it a \mathord, but it should be a \mathbin. Use \mathbin{\textcolor{red}{-}} to get proper spacing. –  Leo Liu Jun 26 '11 at 2:23
    
@Leo: Thanks, fixed. I wasn't sure about that myself. –  Martin Scharrer Jun 26 '11 at 8:47

An addition to Martin Scharrer's answer.

\textcolor has a side effect in math, that the contents is put in curly braces (see definition of \@textcolor). The color implementation of package color is based on groups (the color is restored at the end of the group automatically), but curly braces also make a subformula in math. This affects the horizontal spacing, since the \textcolor now acts as ordinary math atom. This can be avoided by using \begingroup and \endgroup instead of the curly braces:

\documentclass{article}

\usepackage{xcolor}% or package color

\begin{document}

\[
  \color{yellow} A
  \begingroup\color{magenta}=\endgroup
  \textcolor{blue}{B}
  \mathbin{\color{red}-}% shorter than \mathbin{\textcolor{red}{-}}
  \textcolor{green}{C}
  \begingroup\color{cyan}+\endgroup
  D
\]
\end{document}

Result

A macro \mathcolor can be defined, which can be used instead of \textcolor in math:

\documentclass{article}

\usepackage{xcolor}

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

\begin{document}

\[
  \color{yellow} A
  \mathcolor{magenta}{=}
  \mathcolor{blue}{B}
  \mathcolor{red}{-}
  \mathcolor{green}{C}
  \mathcolor{cyan}{+}
  D
\]
\end{document}

Or the definition of \textcolor can be fixed:

\makeatletter
\renewcommand*{\@textcolor}[3]{%
  \protect\leavevmode
  \begingroup
    \color#1{#2}#3%
  \endgroup
}
\makeatother
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.