37

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.

2 Answers 2

38

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

3
  • Is it possible to have rigid corners instead of rounded ones?
    – smh
    Dec 18, 2012 at 17:32
  • 1
    @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 :) Dec 18, 2012 at 18:37
  • 3
    @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). Jan 19, 2013 at 7:41
27

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.

3
  • It works with split command. THX.
    – 71GA
    Nov 12, 2012 at 9:57
  • It doesn't work with multi-line equations
    – smh
    Dec 18, 2012 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 \begin{equation} x=\boxed{\begin{split}...\end{split}}\end{equation}
    – hpesoj626
    Jan 11, 2013 at 15:04

You must log in to answer this question.

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