4

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

\makeatletter
\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \mathcode`#1="8000
    \toks@\expandafter{\csname orig:math:#1\endcsname}%
    \begingroup
       \lccode`~=`#1
       \lowercase{%
    \endgroup
       \edef~{{\noexpand\color{#2}\the\toks@}}}%
   }
\@for\@tempa:=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\do{%
    \expandafter\colorizemath\@tempa{green}}
\makeatother

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?

Mika H.
  • 3,451
  • Locally (in a group) \colorlet{green}{red}? – Qrrbrbirlbel Mar 03 '13 at 02: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 03 '13 at 02: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 03 '13 at 02:48
  • 2
    The \colorlet macro needs the xcolor package: $P\begingroup\colorlet{green}{red}Q\endgroup RST$. – Qrrbrbirlbel Mar 03 '13 at 02: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 03 '13 at 03:02

1 Answers1

6

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.

Code

\documentclass{article}
\usepackage{xcolor}

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

% Setting math colors for whole content
\everymath{\color{math@everymath}}
\everydisplay{\color{math@everydisplay}}

% Setting colors for letters and digits
% Author: user700902
% Source: https://tex.stackexchange.com/a/100625/16595
\makeatletter
\def\colorizemath #1#2{%
    \expandafter\mathchardef\csname orig:math:#1\endcsname\mathcode`#1
    \mathcode`#1="8000
    \toks@\expandafter{\csname orig:math:#1\endcsname}%
    \begingroup
       \lccode`~=`#1
       \lowercase{%
    \endgroup
       \edef~{{\noexpand\color{#2}\the\toks@}}}%
   }
\@for\@tempa:=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z\do{%
    \expandafter\colorizemath\@tempa{math@latin@lower}}
\@for\@tempa:=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z\do{%
    \expandafter\colorizemath\@tempa{math@latin@upper}}
\@for\@tempa:=0,1,2,3,4,5,6,7,8,9\do{%
    \expandafter\colorizemath\@tempa{math@digit}}
\makeatother

% Using color in math-mode,
%   extended to overwrite the color invoked by active letters and digits
% Author: Heiko Oberdiek
% Source: https://tex.stackexchange.com/a/85035/16595
\newcommand*{\mathcolor}[3][named]{%
  \protect\leavevmode
  \begingroup
    \definecolor{math@latin@upper}{#1}{#2}%
    \definecolor{math@latin@lower}{#1}{#2}%
    \definecolor{math@digit}{#1}{#2}%
    \color[#1]{#2}%
    #3%
  \endgroup
}

\begin{document}\thispagestyle{empty}
\[
  P\begingroup\colorlet{math@latin@upper}{red}Q\endgroup RST
  \mathcolor{yellow}{=}
  P\mathcolor{red}{Q}RST
  \mathcolor[gray]{.5}{\sim} PQRST
\]
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • 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 03 '13 at 03: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 03 '13 at 03:25
  • @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 03 '13 at 04:22