我想要文本和数学环境使用不同的颜色。我使用环境align
。下面的代码曾经可以工作,但最近(最多几周)就坏了。坦率地说,我记不清为什么我携带了位\def\m@th{\mathsurround\z@\normalcolor}
,这现在导致了中断。我可能已经读过它那里。因为我忘记了细节,所以我不确定是否最好删除这行代码。
客观的:将数学符号设为蓝色,将括号内的文字设为黑色(将括号本身设为黑色可能更好,但两种方式都可以)。
没有违规代码:
使用有问题的代码:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\everymath{\color{blue}}
\makeatletter
\def\m@th{\mathsurround\z@\normalcolor}% Can't remember the reason for this
\makeatother
\begin{document}
Some inline math $C_{0}$.
\begin{align*}
y = {\underbrace{
\left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right]
}_\text{growth rate}} \times
{\underbrace{
\vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0
}_\text{initial}}
\end{align*}
\end{document}
答案1
为了强制使用黑色文本,您可以添加到from\color{black}
的定义中。\textdef@
amsmath.sty
\upbracefill
要更改括号的颜色,您可以将其添加到(for \underbrace
) 和\downbracefill
(for )的定义中\overbrace
。但我会定义一个命令,比如说,\underbraceColor
使用一个可选参数来更改颜色。
我还将其更改\everymath{\color{blue}}
为\everymath\expandafter{\the\everymath\color{blue}}
保留可能以前的内容\everymath
:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\everymath\expandafter{\the\everymath\color{blue}}
\makeatletter
% To enforce black text (taken from amstext.sty)
\def\textdef@#1#2#3{\hbox{{\color{black}%
\everymath{#1}%
\let\f@size#2\selectfont
#3}}}
% To make the braces black (taken from fontmath.sty)
\newcommand{\overbraceColor}[2][.]{%
\mathop{\vbox{\m@th\ialign{##\crcr\noalign{\kern3\p@}%
\downbracefillColor{#1}\crcr\noalign{\kern3\p@\nointerlineskip}%
$\hfil\displaystyle{#2}\hfil$\crcr}}}\limits}
\newcommand{\underbraceColor}[2][.]{%
\mathop{\vtop{\m@th\ialign{##\crcr
$\hfil\displaystyle{#2}\hfil$\crcr
\noalign{\kern3\p@\nointerlineskip}%
\upbracefillColor{#1}\crcr\noalign{\kern3\p@}}}}\limits}
\def\downbracefillColor#1{$\color{#1}\m@th \setbox\z@\hbox{$\braceld$}%
\braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru
\bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd$}
\def\upbracefillColor#1{$\color{#1}\m@th \setbox\z@\hbox{$\braceld$}%
\bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd
\braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}
\makeatother
\begin{document}
\pagenumbering{gobble}
Some inline math $C_{0}$.
\begin{align*}
y = {\underbraceColor[black]{
\left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right]
}_\text{growth rate}} \times
{\underbraceColor{
\vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0
}_\text{initial}}
\end{align*}
\end{document}
如果您希望仅设置一次括号的颜色,则可以使用以下方法:
% To make the braces black (taken from fontmath.sty)
\def\downbracefill{$\color{black}\m@th \setbox\z@\hbox{$\braceld$}%
\braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru
\bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd$}
\def\upbracefill{$\color{black}\m@th \setbox\z@\hbox{$\braceld$}%
\bracelu\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\bracerd
\braceld\leaders\vrule \@height\ht\z@ \@depth\z@\hfill\braceru$}
答案2
重新定义\m@th
并不是最好的主意。别忘了\everydisplay
。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{xpatch}
\everymath{\color{blue}}
\everydisplay{\color{blue}}
\xpretocmd{\upbracefill}{\blacktext\everymath{\normalcolor}}{}{}
\xpatchcmd{\underbrace}{\limits}{\blacktext\limits}{}{}
\makeatletter
\newcommand{\blacktext}{%
\xpatchcmd{\text@}{\mathchoice}{\normalcolor\mathchoice}{}{}%
}
\makeatother
\begin{document}
Some inline math $C_{0}$.
\begin{equation}
y = {\underbrace{
\left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right]
}_{\text{growth rate}}} \times
{\underbrace{
\vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0
}_{\text{initial}}}
\end{equation}
\end{document}
答案3
您可以尝试\text
按如下方式进行修补(括号为蓝色,但文本为黑色)。以下代码需要替换_\text{growth rate}
为_{text{growth rate}}
(尽管如此,对我来说,_\text{growth rate}
这似乎很奇怪)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\everymath{\color{blue}}
\makeatletter
\let\oldtext@\text@
\def\text@{\color{black}\oldtext@}
\makeatother
\begin{document}
Some inline math $C_{0}$.
\begin{equation}
y = {\underbrace{
\left[ x^2 + r^2 \left(a^2+\frac{a^{0.3}}{b}\right) \right]
}_{\text{growth rate}}} \times
{\underbrace{
\vphantom{ \left(\frac{a^{0.3}}{b}\right) } C_0
}_{\text{initial}}}
\end{equation}
\end{document}