# A version of colorbox that works inside math environments

I'd like to highlight particular parts of an amsmath environment. Here's a minimal working example:

\documentclass{article}
\usepackage{amsmath,color}
\newcommand{\abs}[1]{\lvert#1\rvert}
\newcommand{\Abs}[1]{\left\lvert#1\right\rvert}

%\newcommand{\highlight}[1]{\colorbox{yellow}{#1}}
\newcommand{\highlight}[1]{\boxed{#1}}

\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{\Abs{\frac{f(x) - f(x_0)}{x-x_0} - \ell}} < \epsilon
\end{alignat*}

\end{document}


If I change which version of the \highlight command is commented out, the compiler starts complaining about Missing $inserted. - ## 3 Answers 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.

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:

\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}

-
Thanks! I'm not accepting this immediately, but only because of the possibility that someone might supply code that make sure the style inside the colorbox matches the style outside the colorbox. (For instance, I might want to use the same \highlight command elsewhere for an inline fraction, or conceivably even for text.) But this solution certainly works. – Charles Staats Nov 2 '11 at 2:51
@CharlesStaats: I've updated my answer to accommodate a variable choice in math style using \mathchoice... and spruced it up a little with an optional argument for colour changes. – Werner Nov 2 '11 at 3:27
This is great! Answer accepted. (I did not know about \mathchoice, and I'm very pleased to learn about it.) – Charles Staats Nov 2 '11 at 13:27
By the way, I have to ask: Is there a good reason here to use xcolor instead of color? – Charles Staats Nov 2 '11 at 13:29
@CharlesStaats: My reason for loading xcolor is three-fold: (1) It loads color by default and (2) it provides an easy user interface which extends that of color, allowing one to use colour shades and mixes without formally defining them first. For example, you can use \color{black!15} (15% black) or \color{yellow!30!green} (30% yellow, 70% green), without figuring our what the RGB components of it is and using \color[rgb]{?,?,?} or \definecolor{<col>}{...}. (3) Finally, it has a large set of predefined colour names. – Werner Nov 2 '11 at 14:21

Another way is to use \ensuremath:

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


This has also the desired effect.

-
The issue is that this doesn't re-enables the correct form of mathmode, like \displaymode. – Martin Scharrer Nov 2 '11 at 9:05

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}

}

\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{%
}%
\@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:

-
Is there a lot of changes to do so as to use your code for "no-beamer" documents ? – projetmbc Nov 2 '11 at 15:19
@projetmbc: No, not a lot. But it does hack into the headers so if you had special headers you'd have to be careful with that (beamer already has this "feature") and for some reason I get an overfull hbox that I don't understand! – Loop Space Nov 2 '11 at 18:50