2

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?

3 Answers 3

2

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

4
  • Compare with the definition of \boxed, I think your solution is missing a \m@th before $\displaystyle.... Else a nice solution.
    – daleif
    Jan 1, 2022 at 11:36
  • You mean the $\displaystyle inside the \fcolorbox? Jan 9, 2022 at 17:22
  • Compare with the definition of \boxed in amsmath.sty
    – daleif
    Jan 9, 2022 at 17:24
  • I see. Thanks for the hint! Jan 9, 2022 at 17:46
3

The next release of mathtools will contain a \Aboxed macro generator to generate \Aboxed like macros but with different box commands.

0

TeX programming is hard and not convenient to customize, like @, \edef, \edef\@tempa {\kern \wd\z@ &\kern -\the\wd\z@ \fboxsep ^^ I like TikZ more! TikZ has very natural syntax.

By a suggestion from this answer of Andrew Stacey, the below is a way of decorating some math formulae with the tikzmark library of TikZ. The advantage is that we can utilize all handy features of nodes of TikZ, for example, connecting different nodes.

enter image description here

% suggested from Andrew Stacy
% https://tex.stackexchange.com/questions/629644/fill-and-overlay-do-not-work-well-together/629657#629657
\documentclass{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{calc,tikzmark}
\begin{document}
To decorate some math formulae with the \verb|tikzmark| library of TikZ. The advantage is that we can utilise all handy features of \verb|node|s of TikZ, for example, connecting different \verb|node|s.

The Laplace transform of $f(t)=e^{at}$ can be easily calculated as
\begin{align}
\tikzmarknode{F1}{F(p)}&=\tikzmarknode{F2}{\int_{0}^{+\infty} e^{-pt} e^{at}\,dt}\\[2mm]
&=\dfrac{1}{p-a}.\notag
\end{align}
\begin{tikzpicture}[overlay,remember picture]
\draw[magenta] (F1.north west)+(-.2,.4) 
rectangle ($(F2.south east)+(.2,-.15)$)
(F1.north west)+(-.2,0) coordinate (F)
;
\end{tikzpicture}
It follows that the Laplace transform of $z(t)=e^{\mathbf{i}at}$ is
\begin{align*}
\tikzmarknode{Z1}{Z(p)}&=\tikzmarknode{Z2}{\int_{0}^{+\infty} e^{-pt} e^{\mathbf{i}at}\,dt}\\[2mm]
&=\int_{0}^{+\infty} e^{-pt} \cos(at)\,dt+\mathbf{i}\int_{0}^{+\infty} e^{-pt} \sin(at)\,dt\\[2mm]
&=\dfrac{1}{p-\mathbf{i}a}=\dfrac{p}{p^2+a^2}+\mathbf{i}\dfrac{a}{p^2+a^2}.
\end{align*}
\begin{tikzpicture}[overlay,remember picture]
\draw[cyan] (Z1.north west)+(-.2,.4) coordinate (Z) 
rectangle ($(Z2.south east)+(.2,-.15)$)
(Z1.north west)+(-.2,0) coordinate (Z)
(Z1.south west)+(-.2,-.1) coordinate (ZH)
(Z2.south east)+(.2,.4) coordinate (ZG);
\draw[thick,gray,->] (F) .. controls +(180:5) and +(180:3) .. (Z);
\end{tikzpicture}

Consequently, we have
\begin{tikzpicture}[overlay,remember picture]
\draw[fill=yellow!50,rounded corners] (pic cs:Gse)+(.2,-.2) rectangle ($(pic cs:Gnw)+(-.2,.2)$)
(pic cs:Gse)+(.2,.4) coordinate (G)
;
\draw[thick,gray,->] (ZG) .. controls +(30:6) and +(20:3) .. (G);
\end{tikzpicture}
\begin{equation} 
\tikzmarknode{G1}{G(p)}=\tikzmarknode{G2}{\int_{0}^{+\infty} e^{-pt} \cos(at)\,dt=\dfrac{p}{p^2+a^2}.} 
\end{equation}
\begin{tikzpicture}[overlay,remember picture]
\tikzmark{Gse}{(G2.south east)}
\tikzmark{Gnw}{(G1.west |- G2.north)}
\end{tikzpicture}
and
\begin{tikzpicture}[overlay,remember picture]
\draw[fill=yellow!50,rounded corners] (pic cs:Hse)+(.2,-.2) rectangle ($(pic cs:Hnw)+(-.2,.2)$)
(pic cs:Hnw)+(-.2,0) coordinate (H);
\draw[thick,gray,->] (ZH) .. controls +(180:5) and +(180:3) .. (H);
\end{tikzpicture}
\begin{equation} 
\tikzmarknode{H1}{H(p)}=\tikzmarknode{H2}{\int_{0}^{+\infty} e^{-pt} \sin(at)\,dt=\dfrac{a}{p^2+a^2}.} 
\end{equation}
\begin{tikzpicture}[overlay,remember picture]
\tikzmark{Hse}{(H2.south east)}
\tikzmark{Hnw}{(H1.west |- H2.north)}
\end{tikzpicture}

\end{document}

You must log in to answer this question.

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