# Why \textcolor inside math environment unexpectedly changes surrounding text (which is not inside \textcolor) to black?

## Before:

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


## After:

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


• unfortunately it's a general feature of tikz colour that it doesn't use the driver supported colour stack commands so does not mix well with latex colour. In particular here latex restores the last set colour on the colour stack, but that is black, it does not know of the white colour set directly by tikz – David Carlisle Oct 29 '17 at 20:11
• @DavidCarlisle, can something be done to make it look as expected without dropping tikzpicture? – bp2017 Oct 29 '17 at 20:19
• \begin{tikzpicture}\color{white} works in your mwe. I don't know in general. – user4686 Oct 29 '17 at 20:27
• @jfbu yes that's the best workaround currently (or within a specific node, but setting the colour using the colour stack rather than the tikz attribute – David Carlisle Oct 29 '17 at 20:46

I guess this is basically what David refers to in his latest comment: instead of text=white, use font=\color{white}.

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


You don't really need tikz for this, unless you really need the standalone class: empheq (which loads mathtools) makes it easy to obtain the same result:

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