4

I use the empheq package along with the xcolor package to display equations in coloured boxes. I also wish to change the text colour of the equation. In single-line environments, such as equation, this works perfectly. With multi-line AMS environments, however, the text colour is only applied to the first line, then the equation goes on with the default colour from the second line.

The MWE goes as follows:

\documentclass{article}
\usepackage[svgnames, x11names]{xcolor}
\usepackage{amsmath}
\usepackage{empheq}
\newcommand{\HEG}[1]{\begin{empheq}[box = \colorbox{Thistle4}]{gather}{\color{Snow1}#1}\end{empheq}}
\begin{document}
        \HEG{\mathcal{T}\left(a_1\left\{x_{1k}\right\} + a_2\left\{x_{2k}\right\}\right) =\\\nonumber
        = a_1 \mathcal{T}\left(\left\{x_{1k}\right\}\right) + a_2 \mathcal{T}\left(\left\{x_{2k}\right\}\right)}
\end{document}

This will produce the following output: enter image description here

Manually inserting colour definition at the beginning of the new line will work, ie:

\documentclass{article}
\usepackage[svgnames, x11names]{xcolor}
\usepackage{amsmath}
\usepackage{empheq}
\newcommand{\HEG}[1]{\begin{empheq}[box = \colorbox{Thistle4}]{gather}{\color{Snow1}#1}\end{empheq}}
\begin{document}
        \HEG{\mathcal{T}\left(a_1\left\{x_{1k}\right\} + a_2\left\{x_{2k}\right\}\right) =\\\nonumber
        \color{Snow1}= a_1 \mathcal{T}\left(\left\{x_{1k}\right\}\right) + a_2 \mathcal{T}\left(\left\{x_{2k}\right\}\right)}
\end{document}

will produce

enter image description here

Is there a way to configure empheq to keep the text colour across multiple lines without manual insertions of the colour definition? Your thoughts are much appreciated.

5
  • 2
    Change color before the environment (observe the braces to group it): \newcommand{\HEG}[1]{{\color{Snow1}\begin{empheq}[box = \colorbox{Thistle4}]{gather}{#1}\end{empheq}}}
    – Sigur
    Feb 18 '20 at 14:00
  • @Sigur: thank you, that works perfectly. Feb 18 '20 at 14:05
  • 2
    @Sigur, please extend your comment to an answer.
    – Zarko
    Feb 18 '20 at 14:07
  • 2
    Unrelated: no need to load amsmath before empheq.
    – campa
    Feb 18 '20 at 14:09
  • @Sigur, you are welcome! (+1)
    – Zarko
    Feb 18 '20 at 14:17
6

(works for non numbered only)

You can apply the color to the whole environment simply using \color{} before it. But, make sure that the color changes apply only to the environment, so embrace everything with braces, that is,

{\color{<color>}...}

enter image description here

Off topic: I would not use those \left \right.

MWE

\documentclass{article}
\usepackage[svgnames,x11names]{xcolor}
\usepackage{empheq}
\newcommand{\HEG}[1]{%
  {\color{Snow1}%
    \begin{empheq}[box=\colorbox{Thistle4}]{gather}{#1}\end{empheq}%
  }%
}
\begin{document}
Text in normal color.
\HEG{
  \mathcal{T} (a_1 \{x_{1k}\} + a_2 \{x_{2k}\}) = \\ 
  a_1 \mathcal{T}(\{x_{1k}\}) + a_2 \mathcal{T}(\{x_{2k}\})
}
Text in normal color.
\end{document}
4
  • Somehow this is killing the equation numbering...
    – campa
    Feb 18 '20 at 14:22
  • 1
    @campa, it is not killing, but changing its color also.... lol
    – Sigur
    Feb 18 '20 at 14:23
  • Ah, of course... silly me :-P
    – campa
    Feb 18 '20 at 14:24
  • For me, numbering colour is all right as I use the suggestion at tex.stackexchange.com/questions/134300/… \makeatletter \def\tagform@#1{\maketag@@@{\footnotesize\fontspec{CMU Serif}\color{Grey}(\ignorespaces#1\unskip\@@italiccorr)}} \makeatother Feb 18 '20 at 14:56
6

A small variation of Sigur's answer: I define an environment instead of a macro (my idiosyncrasy: I find it better to read) and declare a new tag form which guarantees that the equation number is still printed in black.

\documentclass{article}

\usepackage[svgnames, x11names]{xcolor}
\usepackage{empheq}

% provided by mathtools, which is loaded by empheq
\newtagform{black}{\textcolor{black}\bgroup(}{)\egroup}

\newenvironment{important}[1][gather] % optional parameter defaults to gather
{%
 \usetagform{black}%
 \color{Snow1}%
 \setkeys{EmphEqEnv}{#1}%
 \setkeys{EmphEqOpt}{box=\colorbox{Thistle4}}%
 \EmphEqMainEnv
}{\endEmphEqMainEnv}


\begin{document}

See
\begin{important}
\mathcal{T}(a_1\{x_{1k}\} + a_2\{x_{2k}\}) = \nonumber \\
 = a_1 \mathcal{T} (\{x_{1k}\}) + a_2 \mathcal{T}(\{x_{2k}\})
\end{important}
or similarly
\begin{important}[align]
\mathcal{T}(a_1\{x_{1k}\} + a_2\{x_{2k}\}) &= \\ \nonumber
 &= a_1 \mathcal{T} (\{x_{1k}\}) + a_2 \mathcal{T}(\{x_{2k}\})
\end{important}

\end{document}

enter image description here

1
  • 2
    Very very nice!!
    – Sigur
    Feb 18 '20 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.