A version of colorbox that works inside math environments

The problem here is that \colorbox reverts it's argument back to text mode. And, since you're using math-related macros (like \left, \right, \frac and \ell) in text mode, TeX complains about a missing $. So you need to explicitly state that you're in math mode using:

\newcommand{\highlight}[1]{\colorbox{yellow}{$\displaystyle #1$}}

I've added the \displaystyle to make sure your fractions and delimiters are expanded as usual. If this behaviour is unwanted, you can modify or remove it.

On that topic, amsmath provides \dfrac which is short for \displaystyle\frac. Such explicit use of display/text style fractions works well to force one's intent, and would eliminate the use of \displaystyle.

enter image description here


It is possible to improve the \highlight macro to detect the type of math mode being used. This is possible by using \mathchoice which provides typesetting choices for 4 different styles:

\mathchoice{<displaystyle>}{<textstyle>}{<scriptstyle>}{<scriptscriptstyle>}

In the updated version of \highlight the math mode is detected prior to using \colorbox, and switched accordingly inside it. Additionally, for generalization, I've added an optional argument to \highlight that allows you to switch the colour (default is yellow) as showcased in the MWE:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\abs}[1]{\lvert#1\rvert}
\newcommand{\Abs}[1]{\left\lvert#1\right\rvert}

% \highlight[<colour>]{<stuff>}
\newcommand{\highlight}[2][yellow]{\mathchoice%
  {\colorbox{#1}{$\displaystyle#2$}}%
  {\colorbox{#1}{$\textstyle#2$}}%
  {\colorbox{#1}{$\scriptstyle#2$}}%
  {\colorbox{#1}{$\scriptscriptstyle#2$}}}%

\begin{document}
\begin{alignat*}{2}
  & \lim_{\Delta x \to 0} \frac{\Delta y}{\Delta x} = \ell \\
  \iff & \forall \epsilon > 0, \, \exists \delta > 0 \text{ s.t. if }
    0 < \abs{\Delta x - 0} < \delta,
    \text{ then } \Abs{\frac{\Delta y}{\Delta x} - \ell} < \epsilon \\
\iff & \forall \epsilon > 0, \, \exists \delta > 0 \text{ s.t. if }
    0 < \highlight{\abs{x - x_0}} < \delta, \text{ then }
    \highlight[green]{\Abs{\frac{f(x) - f(x_0)}{x-x_0} - \ell}} < \epsilon
\end{alignat*}    
\end{document}

Another way is to use \ensuremath:

\newcommand{\highlight}[1]{\colorbox{yellow}{\ensuremath{#1}}}

This has also the desired effect.


I have some code for highlighting text that is specifically designed for use with the beamer class. It uses TikZ, but I load TikZ anyway in my presentations so that's no extra overhead. The main features of this solution are:

  1. The same code is for text as maths,
  2. The highlighting is drawn on the background canvas and so is behind the text,
  3. The style of the highlighter is considerably customisable via PGF keys,
  4. It does a reasonable job of line wrapping.

It is possible to adapt this to a non-beamer solution, either using Martin Scharer's code for drawing on the background of a page, or adapting beamer's (ab)use of headers to place code behind the text.

Here's the code:

\documentclass{beamer}
\usepackage{amsmath}
\usepackage{tikz}

\makeatletter
%
% Highlighter code
%

\newbox\high@box
\let\high@contents=\@empty
\defbeamertemplate{background canvas}{highlighter}{%
  \ifbeamercolorempty[bg]{background canvas}{}{\color{bg}\vrule width\paperwidth height\paperheight}%
  \begingroup
  \setbox\high@box=\hbox{\begin{tikzpicture}[remember picture,overlay]
    \node[minimum width=\textwidth, minimum height=\textheight, anchor=south east] (page) at (-\beamer@rightmargin,\footheight) {};
    \high@contents
  \end{tikzpicture}}%
  \wd\high@box=0pt\ht\high@box=0pt\dp\high@box=0pt%
    \vbox{\hbox{\box\high@box}}%
  \global\let\high@contents=\@empty
  \endgroup}

\newcommand{\addtobg}[1]{%
  \g@addto@macro\high@contents{#1}%
}

\newcounter{highlight}
\newcommand{\hlstart}{\tikz[remember picture,overlay,baseline=-0.7ex] \coordinate (hlstart\the\value{highlight});\hl@start}
\newcommand{\hlend}{\tikz[remember picture,overlay,baseline=-0.7ex] \coordinate (hlend\the\value{highlight});\hl@end\stepcounter{highlight}}

\newcommand{\hl@start}[1][]{%
  \edef\@temp{%
    \noexpand\addtobg{{\noexpand\tikzset{#1}\noexpand\hl@draw{\the\value{highlight}}}}%
  }%
  \@temp
}

\newcommand{\hl@end}{}

\newcommand{\hl@draw}[1]{%
  \pgfkeysgetvalue{/tikz/highlighter width}{\hl@width}%
  \pgfmathsetlengthmacro{\hl@width}{\hl@width - 1pt}%
  \path (hlstart#1);
  \pgfgetlastxy{\hl@sx}{\hl@sy}%
  \path (hlend#1);
  \pgfgetlastxy{\hl@ex}{\hl@ey}%
  \ifdim\hl@sy=\hl@ey\relax
  \draw[highlight] (hlstart#1) -- (hlend#1);
  \else
  \draw[highlight] (hlstart#1) -- (hlstart#1 -| page.east);
  \pgfmathsetlengthmacro{\hl@sy}{\hl@sy -\hl@width}%
  \pgfmathsetlengthmacro{\hl@ey}{\hl@ey +\hl@width}%
  \loop\ifdim\hl@sy>\hl@ey\relax
  \draw[highlight] (0,\hl@sy -| page.west) -- (0,\hl@sy -| page.east);
  \pgfmathsetlengthmacro{\hl@sy}{\hl@sy -\hl@width}%
  \repeat
  \draw[highlight] (hlend#1 -| page.west) -- (hlend#1);
  \fi
}

\tikzset{%
  highlight/.style={
    color=\pgfkeysvalueof{/tikz/highlighter colour},
    line width=\pgfkeysvalueof{/tikz/highlighter width},
    line cap=\pgfkeysvalueof{/tikz/highlighter cap},
  },
  highlighter colour/.initial=yellow,
  highlighter width/.initial=12pt,
  highlighter cap/.initial=butt,
}

\setbeamercolor{highlighted text}{bg=yellow}

\newcommand<>{\highlight}[2][]{%
  \only#3{\hlstart[#1]}#2\only#3{\hlend}}

\makeatother
\setbeamertemplate{background canvas}[highlighter]
\newcommand{\abs}[1]{\lvert#1\rvert}
\newcommand{\Abs}[1]{\left\lvert#1\right\rvert}

\begin{document}
\begin{frame}
\small
\begin{alignat*}{2}
&\lim_{\Delta x \to 0} \frac{\Delta y}{\Delta x} = \ell \\
\iff& \forall \epsilon > 0, \, \exists \delta > 0 \text{ s.t. if }
0 < \abs{\Delta x - 0} < \delta,
\text{ then } \Abs{\frac{\Delta y}{\Delta x} - \ell} < \epsilon \\
\iff& \forall \epsilon > 0, \, \exists \delta > 0 \text{ s.t. if }
0 < \highlight{\abs{x - x_0}} < \delta, \text{ then }
\highlight[highlighter colour=green,highlighter width=1cm]{\Abs{\frac{f(x) - f(x_0)}{x-x_0} - \ell}} < \epsilon
\end{alignat*}
\end{frame}
\end{document}

With a sample:

highlighted text