0

I would like to be able to highlight mathematical formulas by placing them in a nice box (with rounded corners and a background color).

So far, I have found this in my macros, but it is not working for centered formulas.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newcommand\myBox[2][]{\tikz[overlay]\node[fill=blue!20,inner xsep=4pt, inner ysep=4pt, anchor=text, rectangle, rounded corners=3pt,#1] {#2};\phantom{#2}}

\begin{document}

\myBox{Test}

\[
\myBox{\left |\int_0^1 f(x)g(x)dx \right |} % This does not compile
\]

\end{document}

enter image description here

Optional arguments

As in this answer, if the command could take arguments like \myBox[lr=3pt,tb=9pt]{...} to change the inner sep or inner ysep it would be great.

2
  • \usepackage[most]{tcolorbox} and \tcbhighmath{Test}, for example ... The highlightmath style is configurable, or you could use tcboxmath instead, so changing the margins, colour etc. is no real problem then
    – user31729
    Jan 14, 2017 at 17:32
  • I think this can help: tex.stackexchange.com/a/237855/1952
    – Ignasi
    Jan 16, 2017 at 9:20

1 Answer 1

4

It compiles like a breeze (do breezes compile, really?) if you load the empheq package. Don't load amsmath in this case: empheq loads mathtools, which loads amsmath. I took the opportunity to replace your pair of \left| … \right| with an \abs command, which inserts a pair of \left … \right if you use the starred version, or accepts an optional argument (\big, \Big`, &c.) for fine-tuning of the size. I also added some horizontal padding to your box.

\documentclass{article}
\usepackage{empheq}
\usepackage{tikz}

\newcommand\myBox[2][]{\tikz[overlay]\node[fill=blue!20,inner xsep=4pt, inner ysep=4pt, anchor=text, rectangle, rounded corners=3pt,#1] {\quad#2\quad\mbox{}};\phantom{#2}}
\DeclarePairedDelimiter\abs\lvert\rvert

\begin{document}

\myBox{Test}

\begin{empheq}[box=\myBox]{equation}
  \abs*{\int_0^1 f(x)g(x)\,dx } % This does not compile
\end{empheq}

\end{document}

enter image description here

You must log in to answer this question.

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