Math & Scienceempheq | Coloring all Equations in an aligned System

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
hbaromega
Posts: 48
Joined: Mon Mar 07, 2011 8:21 pm

empheq | Coloring all Equations in an aligned System

Post by hbaromega »

Suppose I'm using the empheq package with the corresponding environment and have the following code.

Code: Select all

\begin{empheq}[box=\widefbox]{align}
{\color{red}
A}
&=B+C\nonumber\\
&=D \nonumber\\
&=E+F
\end{empheq}
This paints A in red. I want to color all of the equations, e.g. like this.

Code: Select all

\begin{empheq}[box=\widefbox]{align}
{\color{red}
A
&=B+C\nonumber\\
&=D \nonumber\\
&=E+F
}
\end{empheq}
But I get an error.

How can sort this out? I don't want to specify color command for each expression of equation line (e.g. I may have 20 equations).

Thanks

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

empheq | Coloring all Equations in an aligned System

Post by cgnieder »

This is not working because every cell in align is a group in itself (just like in a {tabular}). The error you're getting is because you open a new group with { in the first cell but don't close it there again. Similarly the color setting is forgotten in the next cell because it was limited to the scope of the first cell. An own environment might be of help:

Code: Select all

\documentclass{article}
\usepackage{amsmath,empheq,xcolor}

\newcommand*\widefbox[1]{\fbox{\hspace{1em}#1\hspace{1em}}}

% see the empheq documentation for examples like this one:
\newenvironment{important}[2][]{%
  \setkeys{EmphEqEnv}{#2}%
  \setkeys{EmphEqOpt}{box=\normalcolor\widefbox,#1}%
  \color{red}%
  \EmphEqMainEnv}%
  {\endEmphEqMainEnv}

\begin{document}

\begin{important}{align}
 A &= B+C\nonumber\\
   &= D \nonumber\\
   &= E+F
\end{important}

\end{document}
But this will also color the equation numbers.

Regards
site moderator & package author
hbaromega
Posts: 48
Joined: Mon Mar 07, 2011 8:21 pm

Re: empheq | Coloring all Equations in an aligned System

Post by hbaromega »

Thanks, Clemens.

But for every coloring (suppose, next time I want to color in blue) I have to define a new environment. Isn't it? Could be there an alternative/simpler way?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

empheq | Coloring all Equations in an aligned System

Post by cgnieder »

No, you can define an optional argument for that:

Code: Select all

\documentclass{article}
\usepackage{amsmath,empheq,xcolor}

\newcommand*\widefbox[1]{\fbox{\hspace{1em}#1\hspace{1em}}}

\usepackage{xparse}% flexible defining of document commands

% see the empheq documentation for examples like this one:
\NewDocumentEnvironment{important}{O{red}O{}m}{%
  \setkeys{EmphEqEnv}{#3}%
  \setkeys{EmphEqOpt}{box=\normalcolor\widefbox,#2}%
  \color{#1}%
  \EmphEqMainEnv}%
  {\endEmphEqMainEnv}

\begin{document}

\begin{important}{align}
 A &= B+C\nonumber\\
   &= D \nonumber\\
   &= E+F
\end{important}

\begin{important}[blue]{align}
 A &= B+C\nonumber\\
   &= D \nonumber\\
   &= E+F
\end{important}

\end{document}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: empheq | Coloring all Equations in an aligned System

Post by svend_tveskaeg »

Interesting! This will go into me "treasure box" of LaTeX tricks.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply