# Colored symbols

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.

-
\textcolor{red}{\ast}. \textcolor works in math mode as well. To redefine it globally \let\OldAst\ast \renewcommand{\ast}{\textcolor{red}{\OldAst}}. – Peter Grill Nov 30 '12 at 23:09
@PeterGrill: It works; great! Thank you. I wish you had written it as an answer; then I get to select it as correct answer; just for the future reference. Would you please do that? – N Nik Nov 30 '12 at 23:22
Well, looks like Heiko's answer is a much better solution. Good to have real experts on this site. – Peter Grill Nov 30 '12 at 23:40

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


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.

-
Would you add an explanation why you need #1 in the definition of \mathcolor? – egreg Nov 30 '12 at 23:52
@egreg I have updated the answer to add an explanation. – Heiko Oberdiek Nov 30 '12 at 23:56
Thanks. I had in mind something with \binrel@ of amsmath but it's not as general as your code. – egreg Nov 30 '12 at 23:58