2

I use this code to change the appearance of the equation, but it is full of errors even if at the end it gives a result, please help me to correct it. Thank you .

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

\newcommand{\coloredeq}[1]{
  \begin{empheq}[box=\colorbox{red}]{align*}
    #1
  \end{empheq}
}

\let\oldeqnarray\eqnarray*
\let\endoldeqnarray\endeqnarray*

\renewenvironment{eqnarray*}{
  \begin{empheq}[box=\colorbox{red}]{align*}
}{
  \end{empheq}
}

\begin{document}
\begin{eqnarray*}
  a &= b \\
  c &= d
\end{eqnarray*}

\end{document}
2
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Could you edit your post so that the code is formatted the way you want it to be? What change are you wanting? Are you wanting everything in those environments to be red? Also, be aware that eqnarray is bad and should be replaced by align.
    – Teepeemm
    Commented Aug 7 at 20:59
  • 1
    You cannot let macros with *, and you should never use eqnarray.
    – daleif
    Commented Aug 8 at 7:59

2 Answers 2

3

The old way to copy a starred environment would've been

\expandafter\let\expandafter\oldeqnarray\csname eqnarray*\endcsname

but nowadays we have \NewEnvironmentCopy:

\NewEnvironmentCopy{oldeqnarray}{eqnarray*}

However, given that there is no real reason to use {eqnarray} you can safely lose the copy. A better way to define wrappers around {empheq} shown in Sec. 6 of the documentation.

\documentclass[twocolumn]{article}

\usepackage{empheq}
\usepackage{xcolor}

% if you must
\NewEnvironmentCopy{oldeqnarray}{eqnarray*}

% one possibility
\renewenvironment{eqnarray*}{%
   \empheq[box=\colorbox{red}]{align*}%
  }{%
   \endempheq
}

% a better one
\newenvironment{foo}[1][red]{%
   \setkeys{EmphEqEnv}{align*}%
   \setkeys{EmphEqOpt}{box=\colorbox{#1}}%
   \EmphEqMainEnv
  }{%
   \endEmphEqMainEnv
}

\begin{document}
See
\begin{oldeqnarray}
please & don't & use\ me
\end{oldeqnarray}
or
\begin{eqnarray*}
  a &= b \\
  c &= d
\end{eqnarray*}
but I'd prefer
\begin{foo}
a&=b\\
c&=d
\end{foo}
and
\begin{foo}[blue!10]
a&=b\\
c&=d
\end{foo}

\end{document}

enter image description here

3

I'm not sure why using eqnarray* as the environment's name. If you really want to save the meaning of eqnarray*, do

\NewEnvironmentCopy{oldeqnarray}{eqnarray*}

but I see no reason to, because eqnarray is buggy under many respects.

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

\NewDocumentEnvironment{boxedalign*}{b}{%
  \setlength{\fboxsep}{2\arraycolsep}%
  \begin{empheq}[box=\colorbox{red!40}]{align*}
    #1
  \end{empheq}%
}{\ignorespacesafterend}

\begin{document}

Some text before the display
some text before the display
some text before the display
\begin{boxedalign*}
  a &= b \\
  c &= d+k+m+n
\end{boxedalign*}
Some text after the display
some text after the display
some text after the display

\end{document}

output

With the b argument type, you tell LaTeX to absorb the environment's content and pass it as #1.

I also suggest a bit more separation between the edges of the box and the contents.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .