# \boxed{}, TikZ and colored equation background

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.

-

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}
$$\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}$$

\hfsetfillcolor{blue!10}
\hfsetbordercolor{blue}
$$\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}$$

\hfsetfillcolor{green!10}
\hfsetbordercolor{green!50!black}
$$\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{document}

Result:

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:

• 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}

\begin{document}
$$\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}$$

\hfsetfillcolor{blue!10}
\hfsetbordercolor{blue}
$$\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}$$

\hfsetfillcolor{green!10}
\hfsetbordercolor{green!50!black}
$$\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{document}

The result:

-
THX this example is great! –  71GA Oct 4 '12 at 20:01
Is it possible to have rigid corners instead of rounded ones? –  smh Dec 18 '12 at 17:32
@smh: sorry, unfortunately at the moment the package does not provide that functionality. I will try see if there is a simple way to insert it :) –  Claudio Fiandrino Dec 18 '12 at 18:37
@smh: I've updated the package (version 0.2) and now there's the possibility to disable rounded corners in two manners: globally with the option norndcorners or just for one box, and in that case there's a specific procedure (details in section 3.5). –  Claudio Fiandrino Jan 19 '13 at 7:41

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}

$$f(x)=\boxed{ax^2+bx+c}$$

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

\end{document}

You may tweak to your liking.

-
It works with split command. THX. –  71GA Nov 12 '12 at 9:57
It doesn't work with multi-line equations –  smh Dec 18 '12 at 17:31
@smh How do you use it? It is not meant to be used to frame multi-line equations at all but it works with the split environment (just as well as the unmodified \boxed command) although that was not my intention when I wrote this. I was just toying with the command to learn how to write and redefine LaTeX macros. Try $$x=\boxed{\begin{split}...\end{split}}$$ –  hpesoj626 Jan 11 '13 at 15:04