[Tex/LaTex] A version of colorbox that works inside math environments

amsmathboxescolor

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.

Best Answer

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}
Related Question