数学模式无法识别颜色

数学模式无法识别颜色

我在数学模式下全局更改颜色,但如果我尝试将颜色局部更改为一个符号或字母,则不会产生效果。

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

在此处输入图片描述

答案1

(在了解到修改\everydisplay可能会导致各种显示数学样式环境出现严重问题后,我对这个答案进行了重大更新。)

Color=...在加载数学字体时设置选项似乎会覆盖所有\textcolor应该做的事情。

您可以通过 (a) 修改内联数学材料的标记列表和 (b) 提供指令来设置材料的默认颜色,以及类似地,提供和环境的指令来实现目标,而不是Color在阶段设置选项。事实证明,没有必要为包的显示数学环境提供类似的命令。\setmathfont\everymath\apptocmd\[ ... \]\AtBeginEnvironmentequationequation*amsmath

请注意,这种方法确实不是为材料而工作$$ ... $$。这是故意的。关于为什么应该不是在 LaTeX 文档中使用$$来启动和终止显示数学模式,请参阅帖子为什么\[ ... \]优于$$ ... $$

在此处输入图片描述

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

答案2

实现此目的的一种方法是将符号插入为文本:

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

数学颜色样本

\redsymbol一种更通用的方法是定义一个红色数学版本,并为其编写一个命令,类似于\boldsymbolfrom ,它能够支持任意数学参数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}

颜色测试样张

答案3

我担心字体的颜色选项会覆盖任何颜色规范,因为它是稍后应用的。

您可以为不同的数学版本定义非彩色字体并指定它。我添加了一个可选参数,以防符号不是普通符号。

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

在此处输入图片描述

相关内容