\boxed{}, TikZ and colored equation background

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

  • empheq
  • hf-tikz

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

  • Highlight an equation within an align environment
  • Highlight an equation within an align environment with color option
  • Black and white emphasis for \boxed?

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


I think the best solution was already provided by Claudio. I am only putting this here to do something about that \boxed command. But I think this solution will not work with split environment so this does not really answer your question. As pointed out already, you may better use the solution by Claudio or use empheq package instead.

Here is what I came up with.

\documentclass[10pt]{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}

% put color to \boxed math command
\newcommand*{\boxcolor}{orange}
\makeatletter
\renewcommand{\boxed}[1]{\textcolor{\boxcolor}{%
\tikz[baseline={([yshift=-1ex]current bounding box.center)}] \node [rectangle, minimum width=1ex,rounded corners,draw] {\normalcolor\m@th$\displaystyle#1$};}}
 \makeatother

\begin{document}

\begin{equation}
f(x)=\boxed{ax^2+bx+c}
\end{equation}

\begin{equation}
x_{1,2}=\boxed{\frac{-b\pm \sqrt{b^2-4ac}}{2a}}
\end{equation}

\end{document}

enter image description here

You may tweak to your liking.