为什么数学环境中的“\textcolor”会意外地将周围的文本(不在“\textcolor”内)更改为黑色?

为什么数学环境中的“\textcolor”会意外地将周围的文本(不在“\textcolor”内)更改为黑色?

前:

\documentclass{standalone}
\usepackage[alignedleftspaceno]{amsmath}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \path node[fill=black,text=white]
            {%
                $%
                    \begin{gathered}
a+b=\text{cvar}\\ % THIS LINE WILL BE CHANGED LATER TO INCLUDE \textcolor
a=\text{cvar}-b   % THIS LINE WILL BE CHANGED LATER TO INCLUDE \textcolor
                    \end{gathered}
                $
            };
    \end{tikzpicture}
\end{document}

后:

a+b=\textcolor{red}{\text{cvar}}\\
a=\textcolor{red}{\text{cvar}}-b

答案1

我想这基本上就是大卫在他最新的评论中所提到的:不要text=white使用 ,而要使用font=\color{white}

output of code

\documentclass{standalone}
\usepackage[alignedleftspaceno]{amsmath}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \path node[fill=black,font=\color{white}]
            {%
                $%
                    \begin{gathered}
a+b=\textcolor{red}{\text{cvar}}\\
a=\textcolor{red}{\text{cvar}}-b
                    \end{gathered}
                $
            };
    \end{tikzpicture}
\end{document}

答案2

你实际上并不需要tikz这个,除非你真的需要这个standalone类:(empheq加载mathtools)可以很容易地获得相同的结果:

\documentclass{article}
\usepackage[alignedleftspaceno]{empheq}%
\usepackage[svgnames]{xcolor}

\newcommand{\myblackbox}[1]{\colorbox{black}{\enspace#1\enspace}}

\newenvironment{myempheq}[2][]{%
\setkeys{EmphEqEnv}{#2}%
\setkeys{EmphEqOpt}{box=\myblackbox,#1}%
\fboxsep=8pt\color{white}\EmphEqMainEnv}%
{\endEmphEqMainEnv}

\begin{document}%

\begin{myempheq}{gather}
  a+b=\text{\color{red}cvar}\\ % THIS LINE WILL BE CHANGED LATER TO INCLUDE \textcolor
  a=\text{\color{red}cvar}-b % THIS LINE WILL BE CHANGED LATER TO INCLUDE \textcolor
\end{myempheq}

\end{document}

enter image description here

相关内容