[Tex/LaTex] Color box with rounded corners around a fragment of a formula

backgroundsboxeshighlightingmath-moderounded-corners

Is there any way in TeX to highlight some part of a formula with a box having colored background and rounded corners?

At present, I need to export such formulas into an image, add those highlight boxes in GIMP, and import back into the document as an image. But this is a mess, and I need to redo all of that every time I change anything in these formulas :/ So it'd be better if I could mark up those fragments of my formulas in LaTeX natively. Is it possible?

I saw one example of boxes made with TikZ package, but I couldn't figure out how does it work and how to assimilate it into my own document. I don't want to make floating boxes inside a document, but just around a fragment of a formula to highlight it. (No, I cannot use text colors for that, because this text already has some colors which means something else, so I need to use background color instead.)

Edit 1
Here's the effect I'd like to achieve:

http://sasq.comyr.com/Stuff/WaveFunc1D_pE_my.png

Edit 2
OK, after suggestions from A.Ellet below, I came out with the following code:

\documentclass{article}
\pagestyle{empty}
\usepackage{color}
\usepackage{amsmath}
\usepackage{fancybox}
\usepackage[usenames,dvipsnames]{xcolor}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}}     % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}}                     % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}}                % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}}           % substitution

\begin{document}
$$\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(
    \colorbox{MySubst!50}{
        \frac{p}{\const{\hbar}}
    }   % line 24
    \var{x} - \frac{E}{\const{\hbar}}\var{t} \right) }
$$
\end{document}

which doesn't work. It throws errors like:

! Missing $ inserted.
<inserted text> 
                $
l.24    }

(I marked the line 24 with a comment.)
When I remove the \frac from inside the box and replace it by just x, it starts to work, but apparently from the formatting it seems to treat is as plain text instead of math.

Edit 3 Here's your code editted to work:

\documentclass{article}
\pagestyle{empty}
\usepackage{color}
\usepackage{amsmath}
\usepackage{fancybox}
\usepackage[usenames,dvipsnames]{xcolor}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}}     % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}}                     % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}}                % independent variable
\newcommand{\subst}[1]{\colorbox{MySubst}{#1}}           % substitution

\begin{document}
\[
\color{My}{\color{MyFunc}}\Psi =
\const{\Psi_{_0}}\;
\uconst{e}^{
        \uconst{i} 
        \left(
            \colorbox{MySubst!50}{$\frac{p}{\const{\hbar}}$}
            \var{x} - \frac{E}{\const{\hbar}}
            \var{t} 
        \right) 
    }
\]
\end{document}

Best Answer

A solution with hf-tikz (requires two compilation runs):

\documentclass[dvipsnames]{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}}     % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}}                     % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}}                % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}}           % substitution

\hfsetfillcolor{Lavender!50}
\hfsetbordercolor{white}

\begin{document}
\[
\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(   
    \tikzmarkin{a}(0.01,-0.175)(-0.015,0.3)\frac{p}{\const{\hbar}}\tikzmarkend{a}
    \var{x} - \tikzmarkin{b}(0.01,-0.175)(-0.015,0.3)\frac{E}{\const{\hbar}}\tikzmarkend{b}\var{t} \right) }
\]
\end{document}

The result:

enter image description here

Remarks:

  1. it is better to use \[ \] rather than $$ $$;
  2. the package allows you to not set in math mode the text inside a \colorbox: one just have to declare where the delimiters of the box should be positioned.

From version 0.3 of the package, things are a bit more user-friendly as shown in hf-tikz doesn't box around equation. The above code can be written thanks to styles as:

\documentclass[dvipsnames]{article}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}}     % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}}                     % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}}                % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}}           % substitution

% available from version 0.3
\tikzset{offset def/.style={
    above left offset={-0.015,0.3},
    below right offset={0.01,-0.175},
  },
  hl/.style={
    offset def,
    set fill color=Lavender!50,
    set border color=white,
  }
}

\begin{document}
\[
\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(   
    \tikzmarkin[hl]{a}\frac{p}{\const{\hbar}}\tikzmarkend{a}
    \var{x} - \tikzmarkin[hl]{b}\frac{E}{\const{\hbar}}\tikzmarkend{b}\var{t} \right) }
\]
\end{document}

Procedure to get .png output

After reading the comments below, here is a way to get a .png. The standalone class here really helps since it has a png class option.

Save as test.tex:

\documentclass[dvipsnames,png, border=2pt]{standalone}
\usepackage{amsmath}
\usepackage[customcolors]{hf-tikz}
\definecolor{My}{RGB}{0,31,63}
\definecolor{MyConst}{RGB}{128,128,128}
\definecolor{MyFunc}{RGB}{0,75,107}
\definecolor{MyIndep}{RGB}{127,55,0}
\definecolor{MySubst}{RGB}{250,230,230}
\newcommand{\const}[1]{{\color{MyConst}\mathrm{#1}}}     % normal constant
\newcommand{\uconst}[1]{\mathrm{#1}}                     % universal mathematical constant
\newcommand{\var}[1]{{\color{MyIndep}#1}}                % independent variable
\newcommand{\subst}[1]{{\colorbox{MySubst}#1}}           % substitution

\hfsetfillcolor{Lavender!50}
\hfsetbordercolor{white}

\begin{document}
$
\color{My}
{\color{MyFunc}\Psi} =
\const{\Psi_{_0}}\;
\uconst{e}^{\uconst{i} \left(   
    \tikzmarkin{a}(0.01,-0.175)(-0.015,0.3)\frac{p}{\const{\hbar}}\tikzmarkend{a}
    \var{x} - \tikzmarkin{b}(0.01,-0.175)(-0.015,0.3)\frac{E}{\const{\hbar}}\tikzmarkend{b}\var{t} \right) }
$
\end{document}

and compile with:

pdflatex -shell-escape test.tex

This will create

  1. test.pdf
  2. test.png

perfectly cropped.

Related Question