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
.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThere 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:
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.
norndcorners
to have always rigid corners: just loading the package with \usepackage[customcolors,norndcorners]{hf-tikz}
the previous document becomes: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:
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
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}
You may tweak to your liking.
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}
Jan 11, 2013 at 15:04