[Tex/LaTex] Colored symbols

colormath-modesymbols

First of all, I apologize if this is a repetitive question. I searched, but couldn't find it. I was wondering if I can change the color of symbols in latex. For example \ast always give me a black symbol. Is there anyway to change it? (either changing it permanently throughout the document or changing it just once.

Best Answer

\textcolor of package color adds a level of curly braces that become a subformula in math mode, destroying the spacing. The following example defines \mathcolor that can be used in the same way as \textcolor, but without the subformula side effect. It uses \begingroup and \endgroup instead of the curly braces that do not cause the trouble in math. A grouping level is needed for LaTeX's color handling that restores the color after the group via \aftergroup.

\documentclass{article}
\usepackage{color}

\newcommand*{\mathcolor}{}
\def\mathcolor#1#{\mathcoloraux{#1}}
\newcommand*{\mathcoloraux}[3]{%
  \protect\leavevmode
  \begingroup
    \color#1{#2}#3%
  \endgroup
}

\begin{document}
$a\ast b$

$a\textcolor{red}{\ast}b$

$a\mathcolor{red}{\ast}b$
\end{document}

Result

The parameter text of \mathcolor is #1# with a final hash symbol without number. That means all tokens before the next opening curly brace are put into #1. In case of \mathcolor (or \textcolor) this is the optional argument. Without an optional argument #1 is empty, otherwise it contains the optional argument including the square brackets.