TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In user700902's answer to this thread, the following code is suggested to color alphabets A-Z green:

\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \toks@\expandafter{\csname orig:math:#1\endcsname}%

If an alphabet somewhere in the document is supposed to be red, is there a way to override the green command? The normal $\textcolor{red}{Q}$ does not work.

I mean, say I have a text $PQRST$, and I want just the letter $Q$ to be red. What can I do in that case?

share|improve this question
Locally (in a group) \colorlet{green}{red}? – Qrrbrbirlbel Mar 3 '13 at 2:31
I mean, say I have a text $PQRST$, and I want just the letter $Q$ to be red. What can I do in that case? – Mika H. Mar 3 '13 at 2:38
How about $P\text{\textcolor{red}{\emph{Q}}}RST$? Here, \text is a command provided by the amsmath package. (Of course, this presupposes that the text-italic "Q" and the math-italic "Q" letters are (very) similar. That's the case for Computer Modern, but needn't be true for other font families.) – Mico Mar 3 '13 at 2:48
The \colorlet macro needs the xcolor package: $P\begingroup\colorlet{green}{red}Q\endgroup RST$. – Qrrbrbirlbel Mar 3 '13 at 2:48
@Qrrbrbirlbel Works, thanks! TeX has so much in it. Without help from you and other people in this forum, searching for the right commands would have been much harder! – Mika H. Mar 3 '13 at 3:02
up vote 6 down vote accepted

My suggestion is to define named colors for math mode (so you only have to change them and not those in \colorizemath) and overwrite them manually in an \mathcolor macro.

Previously, I have used \colorlet but that doesn’t work with the [<model>]{<color>} syntax, so I simply used \definecolor.

The reason for the \begingroup and \endgroup rather than { } comes from Heiko Oberdiek’s answer to Colored symbols.



% Defining various math colors
\colorlet{math@latin@upper} {green}
\colorlet{math@latin@lower} {green}
\colorlet{math@digit}       {red}
\colorlet{math@everymath}   {blue}

% Setting math colors for whole content

% Setting colors for letters and digits
% Author: user700902
% Source: http://tex.stackexchange.com/a/100625/16595
\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \toks@\expandafter{\csname orig:math:#1\endcsname}%

% Using color in math-mode,
%   extended to overwrite the color invoked by active letters and digits
% Author: Heiko Oberdiek
% Source: http://tex.stackexchange.com/a/85035/16595

  P\begingroup\colorlet{math@latin@upper}{red}Q\endgroup RST
  \mathcolor[gray]{.5}{\sim} PQRST


enter image description here

share|improve this answer
Very clever! How would you extend this functionality to symbols (or any other objects that aren't letters or digits)? Right now, all math symbols are set in blue, and don't seem to be affected by either \overcolor or the \colorlet methods. – Mico Mar 3 '13 at 3:12
@Mico For that, the \mathcolor macro from the linked answer does still work because (as I see it) the \every… stuff is only invoked at the start of math-mode. The next step would be an eierlegende Wollmilchsau that works for both cases. In fact, I’d just add my \overcolor definition to Heiko’s \mathcolor macro. Though, I do not know exactly how to deal with a possible color model (#1 in \mathcolor) for \colorlet. – Qrrbrbirlbel Mar 3 '13 at 3:25
Thanks. You've provided some helpful pointers. – Mico Mar 3 '13 at 4:04
@Mico I have updated my answer so that now any object can be re-colored and dropped \colorlet completely as \definecolor does, apparently, the same job. – Qrrbrbirlbel Mar 3 '13 at 4:22
Thanks! It's great now. – Mico Mar 3 '13 at 13:09

Your Answer


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.