TikZ Colored Equations – Using \boxed{}, TikZ, and Colored Equation Backgrounds

amsmathhighlightingmath-modetikz-pgf

I wonder if someone could provide me with a simple MWE for using TikZ together with \boxed to produce a colored equation background. If possible with rounded corners.

I am using \boxed{} inside the split environment from amsmath.

Best Answer

There are some packages that can help you, for example:

and along this site some answers could be a good starting point:

Here, I provide you a MWE using the hf-tikz package. Actually \boxed{} is not used at all, thus I don't know if this will meet your requirements. Notice that, in the following example, it is shown how to highlight the whole equation or just a part of it, that is the major potentiality of the package. You should compile twice to get the right result.

\documentclass{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}

\begin{document}
\begin{equation}\label{e:barwq}\begin{split}\tikzmarkin{a}(0.2,-0.5)(-0.2,0.65)
H_c&=\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].\tikzmarkend{a}
\end{split}\end{equation}

\hfsetfillcolor{blue!10}
\hfsetbordercolor{blue}
\begin{equation}\label{e:barwq2}\begin{split}
H_c&=\tikzmarkin{b}(0,-0.6)(0,0.65)\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\tikzmarkend{b}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].
\end{split}\end{equation}

\hfsetfillcolor{green!10}
\hfsetbordercolor{green!50!black}
\begin{equation}\label{e:barwq3}\begin{split}
H_c&=\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\tikzmarkin{c}(0.05,-0.6)(-0.05,0.65)\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].\tikzmarkend{c}
\end{split}\end{equation}

\end{document}

Result:

enter image description here


According to the request in the comments, here are the two possibilities to get rigid corners. It is needed the version 0.2 of the package.

  • The option norndcorners to have always rigid corners: just loading the package with \usepackage[customcolors,norndcorners]{hf-tikz} the previous document becomes:

enter image description here

  • The second possibility allows to have rigid corners singularly; load the package with the shade option and then use the key disable rounded corners=true in the tikzmarkin command.

In this example, the second equation is highlighted with rigid corners while the other two with rounded corners:

\documentclass{article}
\usepackage{amsmath}
\usepackage[customcolors,shade]{hf-tikz}

\begin{document}
\begin{equation}\label{e:barwq}\begin{split}\tikzmarkin{a}(0.2,-0.5)(-0.2,0.65)
H_c&=\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].\tikzmarkend{a}
\end{split}\end{equation}

\hfsetfillcolor{blue!10}
\hfsetbordercolor{blue}
\begin{equation}\label{e:barwq2}\begin{split}
H_c&=\tikzmarkin[disable rounded corners=true]{b}(0,-0.6)(0,0.65)\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\tikzmarkend{b}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].
\end{split}\end{equation}

\hfsetfillcolor{green!10}
\hfsetbordercolor{green!50!black}
\begin{equation}\label{e:barwq3}\begin{split}
H_c&=\frac{1}{2n} \sum^n_{l=0}(-1)^{l}(n-{l})^{p-2}
\sum_{l _1+\dots+ l _p=l}\prod^p_{i=1} \binom{n_i}{l _i}\\
&\quad\cdot[(n-l )-(n_i-l _i)]^{n_i-l _i}\cdot
\tikzmarkin{c}(0.05,-0.6)(-0.05,0.65)\Bigl[(n-l )^2-\sum^p_{j=1}(n_i-l _i)^2\Bigr].\tikzmarkend{c}
\end{split}\end{equation}

\end{document}

The result:

enter image description here

Related Question