# Math mode does not recognize color

I change color globally in math mode, but if I try to change the color locally only to one symbol or letter, it does'n have effect.

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{amssymb,verbatim,amsthm,amsmath,color}
\usepackage[T1]{fontenc}

\definecolor{navy}{rgb}{0.0, 0.0,0.5}
\usepackage{fontspec}
\usepackage{unicode-math}
\setmathfont[slash-delimiter=frac,Color=navy]{Cambria Math}

\begin{document}
$$x^2-y^2=1$$
$$x^2-(\textcolor{red}{1})^2=1$$
\end{document}


• Off-topic: (a) Don't load the fontenc package when using XeLaTeX or LuaLaTeX. (b) Don't load both xcolor and color.
– Mico
Jul 3 '20 at 0:48
• Adding to the comment by @Mico: you do not need amssymb if you load unicode-math, which will just overwrite everything in it. You should not load fontenc together with Unicode fonts. You would not normally need to load amsmath with unicode-math, but you are also loading amsthm, so load amsmath before amsthm. Jul 3 '20 at 11:30
• Basically, unicode-math makes almost all legacy font packages redundant, although it’s written to be robust if you load many of them anyway. Jul 3 '20 at 11:31

(I updated this answer significantly after learning that modifying \everydisplay can cause serious problems with various display math style environments.)

Setting the Color=... option while loading a math font appears to override anything that \textcolor is supposed to do.

Instead of setting a Color option at the \setmathfont stage, you may achieve your objective by (a) modifying the \everymath token list for inline math material and (b) providing an \apptocmd directive to set the default color for $...$ material and, similarly, \AtBeginEnvironment directives for the equation and equation* environments. It turns out not be necessary to provide analogous commands for the display math environments of the amsmath package.

Observe that this approach does not work for $$...$$ material. This is deliberate. For an in-depth discussion of why one should not use $$ in a LaTeX document to initiate and terminate display math mode, please see the posting Why is $...$ preferable to $$ ... ?. \documentclass{article} \usepackage{amsmath} % provides various displaymath environments \usepackage{xcolor} % for '\definecolor', '\color', and '\textcolor' \definecolor{navy}{rgb}{0.0,0.0,0.5} \usepackage{unicode-math} \setmathfont[slash-delimiter=frac]{Cambria Math} % Auto-color inline math material: \everymath=\expandafter{\the\everymath\color{navy}} % Auto-color display math material: \usepackage{etoolbox} % <-- required for XeLaTeX; optional for LuaLaTeX \apptocmd{$}{\color{navy}}{}{} \AtBeginEnvironment{equation}{\color{navy}} \AtBeginEnvironment{equation*}{\color{navy}} \begin{document} % First, some inline math material. $$0+\textcolor{red}{1}=1$$, \begin{math}1+1=\textcolor{red}{2}\end{math}, \textcolor{red}{2}+1=3. %% Second, various forms of display math material. \[ x^2-(\textcolor{red}{1})^2=1$ \begin{equation*} x^2-(\textcolor{red}{1})^2=1 \end{equation*} \begin{gather} x^2-y^2=1\\ x^2-(\textcolor{red}{1})^2=1 \end{gather} \begin{align*} x^2-y^2&=1\\ x^2-(\textcolor{red}{1})^2&=1 \end{align*} \end{document}  • Thank's for you answer, I only have a problem, align environment give an error "improper \halign inside's" Jul 3 '20 at 5:41
• @OrEsc - Please see the update I've applied ot my answer.
– Mico
Jul 3 '20 at 13:35

One way to accomplish this is to insert the symbol as text:

\documentclass[11pt]{article}
\usepackage[svgnames]{xcolor}
\usepackage{amsmath,amsthm,verbatim}
\usepackage{unicode-math}

\pagestyle{empty} % Suppress page numbers in this MWE.

\setmainfont{STIX Two Text}
\setmathfont[slash-delimiter=frac,Color=Navy]{STIX Two Math}

\begin{document}
\begin{gather*}
x^2-y^2=1 \\
x^2-( \textnormal{\textcolor{Red}{1}} )^2=1
\end{gather*}
\end{document}


A more versatile approach, capable of supporting arbitrary math arguments, is to define a red math version, and write a \redsymbol command for it, similar to \boldsymbol from amsbsy.sty.

\documentclass[11pt]{article}
\usepackage[svgnames]{xcolor}
\usepackage{amsmath,amsthm,verbatim}
\usepackage{unicode-math}

\pagestyle{empty} % Suppress page numbers in this MWE.

\setmainfont{STIX Two Text}
\setmathfont[Color=Navy]{STIX Two Math}
\setmathfont[version=red,Color=Red]{STIX Two Math}

\makeatletter
\newcommand{\redmath}{\mathversion{red}}
\newcommand{\redsymbol}[1]{% Based on \boldsymbol from amsbsy.sty
\math@atom{#1}{%
\mathchoice%
{\hbox{\redmath$\displaystyle#1$}}%
{\hbox{\redmath$\textstyle#1$}}%
{\hbox{\redmath$\scriptstyle#1$}}%
{\hbox{\redmath$\scriptscriptstyle#1$}}}}
\makeatother

\begin{document}
\begin{gather*}
x^2-y^2=1 \\
x^2-(\redsymbol{\phi_i})^2=1
\end{gather*}
\end{document}


I'm afraid that the Color option to the font overrides any color specification, because it's applied later.

You can define a noncolored font for a different math version and specify it. I added an optional argument to help in case the symbol is not an ordinary one.

\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{fontspec}
\usepackage{unicode-math}

\definecolor{navy}{rgb}{0.0, 0.0,0.5}
\setmathfont[slash-delimiter=frac,Color=navy]{Cambria Math}
\setmathfont[version=black,slash-delimiter=frac]{Cambria Math}
\setmathfont[version=bold]{XITS Math}

\newcommand{\cm}[3][\mathord]{%
#1{\text{\mathversion{black}\textcolor{#2}{$#3$}}}%
}

\begin{document}

\begin{gather*}
x^2-(1)^2=1 \\
x^2-(\cm{red}{1})^2=1 \\
x^2\cm[\mathbin]{red}{-}(1)^2=1\\
\sum_{i=1}^n x_{\cm{red}{i}} \\
\cm[\mathop]{red}{\sum}_{i=1}^n x_{\cm{red}{i}}
\end{gather*}

\end{document}
`