# Color symbols individually in math mode (unicode-math clash)

The solution in the post https://tex.stackexchange.com/a/100625 is highly efficient. Unfortunately it can not be used with unicode-math, which erases the individualized symbol colors. While unicode-math does allow individual symbol coloring via \setmathfont, if too many symbols are independently colored in this way, the source will not compile.

Is there any way to make the \colorizemath code (linked above) compatible with unicode-math ?

• If you have an example that fails in unicode-math, please submit a bug in the issue tracker: github.com/wspr/unicode-math/issues . I'm not surprised you run into problems, but I might be able to develop an alternative interface if I can see what you're trying to do. Aug 30, 2018 at 1:52
• Honestly I cannot tell whether this is a bug, or an abuse of the package by yours truly. I have been using \setmathfont one glyph at a time to individualize colour etc. If \setmathfont is called more than a handful of times, the symbol font limit is exceeded; hence the source does not compile. If unicode-math wants to allow dozens of characters to be customized, one \setmathfont by one, I will happily submit a bug. Unicode-math continues to be my favourite package, with its prints all over documents like dropbox.com/s/m95ta9o6ac0l8wg/Project1Report_BenBarlow.pdf?dl=0 Sep 6, 2018 at 5:39
• Nice example! Probably one glyph at a time is excessive, but I see you are using lots of difference colours :) Sep 6, 2018 at 14:13

The following snippet causes the variable x to display blue in math mode without affecting its appearance in text mode. Further, it seems to get along well with unicode-math. (This snippet can be extended to independently colour many symbols, and it compiles rapidly.)

\begingroup
\lccode~=x
\lowercase{\endgroup
\def~{{\color{blue}\symnormal{x}}}%
}%
\mathcodex="8000


(Code modified from https://texfaq.org/FAQ-activechars)

The lcc lower-case-code method is a lookup.

An expl3 lookup method could be used.

Here, a token-list mapping function as proof-of-concept:

A key-value property-list could also be used in expl3, as an alternative.

Much patience needed, to set up every symbol (and operator).

MWE

\documentclass{article}
\usepackage{xcolor}
\usepackage{unicode-math}
\setmathfont{TexGyrePagella-Math}
\usepackage{xparse}

\ExplSyntaxOn

\cs_new:Npn \sym_to_col:n #1
{
\tl_set:Nn \l_tmpb_tl { #1 }
\str_case:VnTF
\l_tmpb_tl
{
{ 𝒚 } { {\color{red} 𝒚 } }
{ + } { {\color{green} + } }
{ m } { { {\colorbox{blue}{\color{yellow} $m$} } } }
{ n } { {\color{blue} n } }
{ ( } { {\color{brown} ( } }
{ ) } { {\color{brown} ) } }
{ 𝒯 } { {\color{violet} 𝒯 } }
}
{ }  % true
{ #1 } % false
}

\NewDocumentCommand \colourmaths { m } {
\tl_set:Nn \l_tmpa_tl { #1 }
\if_mode_math:
\tl_map_function:NN
\l_tmpa_tl
\sym_to_col:n
\else:
\tl_use:N \l_tmpa_tl
\fi:

}

\prop_gset_from_keyval:Nn \g_tmpa_prop
{
𝒚=blue,
}

\ExplSyntaxOff

\begin{document}

Text mode:

abc \colourmaths{mno}

\bigskip
Math mode:

$xyz = \colourmaths{𝒚mn+qrs(0)𝒯}$

\end{document}

`