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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this community\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.
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}
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
\mathcolor
definition should no longer be used, as its \def
will overwrite the current one, which now exists in modern xcolor
and color
packages.
Aug 16, 2022 at 6:38
In sufficiently new version of LaTeX, simply load xcolor
package and use \mathcolor
. Refer to texdoc mathcolor
for documentation.
\documentclass{article}
\usepackage{xcolor}
\begin{document}
$\mathcolor{red}{1}\mathcolor{blue}{+}\mathcolor{green}{2}$
$1+2$
\end{document}
This has certain advantages compared to other approaches:
Spacing is usually more correct:
\[\textcolor{blue}{f}^2_3 \ne \mathcolor{blue}{f}^2_3\]
\[5\textcolor{blue}{+}6 \ne 5\mathcolor{blue}{+}6\]
Other surprises with \color
:
\[ f^{\color{blue} 2}_3 \]
(Of course, if you try hard enough to break it it won't work: Why do optional arguments of commands affect spacing of \mathcolor but usually it would)