Define Another Command Like Aboxed but with a Different Colorbox

colormathtools

The following commands define Aboxed but with a yellow coloured box.

\def\@Aboxed#1&#2&#3\ENDDNE{%
  \ifnum0=`{}\fi \setbox \z@
    \hbox{$\displaystyle#1{}\m@th$\kern\fboxsep \kern\fboxrule }%
    \edef\@tempa {\kern  \wd\z@ &\kern -\the\wd\z@ \fboxsep
        \the\fboxsep \fboxrule \the\fboxrule }\@tempa 
        \fcolorbox{black}{yellow}{$\displaystyle #1#2$}% changed
}

Now, how can I define another command, say AboxedG, with a green coloured box?

Best Answer

Instead of redefining the existing macro for \Aboxed I would copy the macro defintion and create a new macro that can take a color name as argument:

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

\makeatletter
\newcommand\ColorAboxed[2]{\gdef\@AboxedColor{#1}\let\bgroup{\romannumeral-`}\@ColorAboxed#2&&\ENDDNE}
\def\@ColorAboxed#1&#2&#3\ENDDNE{%
  \ifnum0=`{}\fi \setbox \z@
    \hbox{$\displaystyle#1{}\m@th$\kern\fboxsep \kern\fboxrule }%
    \edef\@tempa {\kern  \wd\z@ &\kern -\the\wd\z@ \fboxsep
        \the\fboxsep \fboxrule \the\fboxrule }\@tempa%
        \fcolorbox{black}{\@AboxedColor}{\m@th$\displaystyle #1#2$}%
} 
\makeatother

\begin{document}

\begin{align*}
\Aboxed{ f(x) & = \int h(x)\, dx} \\
& = g(x)
\end{align*}

\begin{align*}
\ColorAboxed{green}{ f(x) & = \int h(x)\, dx} \\
& = g(x)
\end{align*}

\begin{align*}
\ColorAboxed{yellow}{ f(x) & = \int h(x)\, dx} \\
& = g(x)
\end{align*}

\end{document}

enter image description here

Related Question