18

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}
9
  • Have you checked out fancybox?
    – A.Ellett
    Commented Dec 28, 2012 at 18:47
  • I've just tried, but it creates ugly frame with fake shadow, no background, and no rounded corners, and I don't know how to control its appearance to fit it to my needs. Is there any documentation for this package? Could these boxes be parametrized? (colors, rounded corners, backgrounds, turn off fake shadows etc.)
    – SasQ
    Commented Dec 28, 2012 at 18:54
  • 1
    I believe you should better post a picture of the desired output. Without that, your intentions are not entirely clear.
    – yo'
    Commented Dec 28, 2012 at 19:09
  • 1
    @tohecz: I added a picture of what I'd like to get in my question, and a sample of code using fancybox which doesn't work.
    – SasQ
    Commented Dec 28, 2012 at 19:22
  • 1
    First, you should use \[...\] instead of $$...$$. The later is deprecated in LaTeX. Also, if you used some indentation, your code would be a little bit more readable. I've edited your code. The error was that you didn't switch back into mathmode within the \colorbox command.
    – A.Ellett
    Commented Dec 28, 2012 at 19:32

3 Answers 3

16

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.

13
  • This looks closest to what I want to achieve. But I don't understand your code at all (what does all those numbers mean?). And it doesn't work with my texlive installation because of missing hf-tikz.sty file (I don't know how to install it correctly under Gentoo without breaking anything in my package manager when messing with the files by hand).
    – SasQ
    Commented Dec 28, 2012 at 20:42
  • 1
    I just wanted to add some boxy backgrounds, and now I have so many different options to choose, most of them not working at all, and some of them needing fiddling with my installation :-( The code to get it is a mess, I don't understand any single bit of it, and I cannot find any documentation on the Tikz's project page. All of this overwhelms me :-/ and seem to be lot more work than what I was doing so far with re-editing images of those formulas in GIMP, instead of simplifying things. So I'll probably be forced to revert to my old approach :-/ But thanks for all your help anyway.
    – SasQ
    Commented Dec 28, 2012 at 20:46
  • @SasQ: the package is quite recent (6 months), so it needs TeXLive 2012. The numbers simply represent the coordinates of the below right and above left corner of the box. If you visit ctan.org/tex-archive/graphics/pgf/contrib/hf-tikz you will find the documentation of the package in a pdf file. Unfortunately I don't know Gentoo, so I don't know how help you in updating the distribution or simply to install a single package. Commented Dec 28, 2012 at 20:58
  • 1
    As to the coordinates: This sounds very hacky anyway, because whenever I'll update something in the formula, I'll be required to update those coords I guess, which is not much an advantage over editting the image in GIMP anyway. Better solution would just mark which block of text needs to be surrounded by the box and update the size of the box automatically when I edit its content.
    – SasQ
    Commented Dec 28, 2012 at 21:07
  • 1
    Yes I do need PNG conversion, because this is supposed to be my target output format. I use TeX as a kind-of compiler of equation images from plain-text source codes. I export to DVI first, and then I use dvipng to quickly convert it to an image with a desired DPI resolution. And it worked quite well for simpler formulas. But I stucked on those highlight boxes with round corners. Your method with hf-tikz appears to work, but when I export to DVI, I see those boxes offset out of their places. In PDF they seem OK (strange). And when I convert DVI->PNG, they disappear at all. VERY strange!
    – SasQ
    Commented Dec 29, 2012 at 10:56
13

Use fancybox

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{fancybox}
\usepackage{xcolor}
\pagestyle{empty}
\begin{document}

\begin{align}
    x^2 + y^2 &= 4
\end{align}

\begin{align}
   \fbox{$ x^2 + y^2$} &= 4
\end{align}

\begin{align}
   \colorbox{blue!20}{$ x^2 + y^2$} &= 4
\end{align}

\begin{align}
   \ovalbox{$ x^2 + y^2$} &= 4
\end{align}


\begin{align}
   \ovalbox{$\displaystyle{} \frac{x+2}{x^2+y^2} $} &= 4
\end{align}

\end{document}

enter image description here

Notice how I have to re-enter math mode within the various framing boxes. Also for the fraction, I had to declare \displaystyle inside the box to get the fraction to be formatted appropriately for an align environment.

7
  • OK, the last one seems quite similar to what I need to achieve. It just lacks the last feature: rounded corners.
    – SasQ
    Commented Dec 28, 2012 at 18:56
  • But from some strange reasons, it doesn't work with my LaTeX framework (LiveTex on Gentoo). It says that it cannot recognize my defined color (! LaTeX Error: Undefined color `MySubst!20'). This color is defined and it works for other things.
    – SasQ
    Commented Dec 28, 2012 at 19:07
  • Please post a MWE above so we can see what you're doing.
    – A.Ellett
    Commented Dec 28, 2012 at 19:08
  • Another problem: It seems to forget that it's in a math mode, because fonts are big and not italic for variables. Also, I cannot use \frac inside of it, because it throws parse errors concerning unpaired braces. Strange...
    – SasQ
    Commented Dec 28, 2012 at 19:09
  • 1
    Please note the changes I've made above
    – A.Ellett
    Commented Dec 28, 2012 at 19:14
8

You can use mdframed package and define your own style in preamble

\usepackage[framemethod=TikZ]{mdframed}
\mdfdefinestyle{MyFrameYellow}{%
    linecolor=red,
    outerlinewidth=0.5pt,
    roundcorner=5pt,
    innertopmargin=\baselineskip,
    innerbottommargin=\baselineskip,
    innerrightmargin=20pt,
    innerleftmargin=20pt,
    backgroundcolor=yellow!50!white}

Then you can include your formula like that

\begin{mdframed}[style=MyFrameYellow]
\begin{align*}
N_1=&NP_1=N\dfrac{e^{-\beta\epsilon}}{e^{-\beta\epsilon} + 2e^{-2\beta\epsilon}     + 3e^{-3\beta\epsilon}+ 4e^{-4\beta\epsilon}}= N \dfrac{1}{1 + 2e^{-    \beta\epsilon} + 3e^{-2\beta\epsilon}+ 4e^{-3\beta\epsilon}}\\
N_2=&NP_2=N\dfrac{2e^{-2\beta\epsilon}}{e^{-\beta\epsilon} +     2e^{-2\beta\epsilon} + 3e^{-3\beta\epsilon}+ 4e^{-4\beta\epsilon}}= N \dfrac{1}    {1 + \frac12 e^{+\beta\epsilon} + \frac32 e^{-\beta\epsilon}+     2e^{-2\beta\epsilon}}
\end{align*}
\begin{align*}
N_3=&NP_3=N\dfrac{3e^{-3\beta\epsilon}}{e^{-\beta\epsilon} +     2e^{-2\beta\epsilon} + 3e^{-3\beta\epsilon}+ 4e^{-4\beta\epsilon}}=N \dfrac{1}{1     + \frac13e^{+2\beta\epsilon} + \frac23e^{+\beta\epsilon}+ \frac43e^{-    \beta\epsilon}}\\
N_4=&NP_4=N\dfrac{4e^{-4\beta\epsilon}}{e^{-\beta\epsilon} +     2e^{-2\beta\epsilon} + 3e^{-3\beta\epsilon}+ 4e^{-4\beta\epsilon}}= N \dfrac{1}    {1 + \frac14e^{+3\beta\epsilon} + \frac12e^{-2\beta\epsilon}+     \frac34e^{+\beta\epsilon}}
\end{align*}
\end{mdframed}

The result is something like that

The nice thing with mdframed is that it can split over pages!

8
  • This is a nice package, but it doesn't seem to work inside another math environment.
    – A.Ellett
    Commented Dec 28, 2012 at 19:25
  • @A.Ellett: You are right...I've just seen the edited question...
    – Thanos
    Commented Dec 28, 2012 at 19:30
  • It also doesn't seem to work for my installation (texlive on Gentoo). It says that the package couldn't be found (! LaTeX Error: File mdframed.sty' not found`), and for now I don't know how to install it. Although I'm not a noob in the LaTeX syntax, I'm a complete noob in the maintaining own installation, packages & use those extensions in my code. I couldn't find any good documentations for the packages you're describing here.
    – SasQ
    Commented Dec 28, 2012 at 19:32
  • @SasQ: You can download the package manually from ctan ctan.org/tex-archive/macros/latex/contrib/mdframed You can extract it, in your latex folder(where all packages' sty are) or just extract in the same folder as your .tex file. Note that you will also need to install the l3packages as well ctan.org/tex-archive/macros/latex/contrib/l3packages
    – Thanos
    Commented Dec 28, 2012 at 19:41
  • I added those files from CTAN to my installation in a subdir, but LaTeX still doesn't see them. Do I need also to edit some config files?
    – SasQ
    Commented Dec 28, 2012 at 21:02

You must log in to answer this question.

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