Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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



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


If I change which version of the \highlight command is commented out, the compiler starts complaining about Missing $ inserted.

share|improve this question

3 Answers 3

up vote 16 down vote accepted

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:


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

\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{xcolor}% http://ctan.org/pkg/xcolor

% \highlight[<colour>]{<stuff>}

  & \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
share|improve this answer
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:


This has also the desired effect.

share|improve this answer
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:


% Highlighter code

\defbeamertemplate{background canvas}{highlighter}{%
  \ifbeamercolorempty[bg]{background canvas}{}{\color{bg}\vrule width\paperwidth height\paperheight}%
  \setbox\high@box=\hbox{\begin{tikzpicture}[remember picture,overlay]
    \node[minimum width=\textwidth, minimum height=\textheight, anchor=south east] (page) at (-\beamer@rightmargin,\footheight) {};


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



  \pgfkeysgetvalue{/tikz/highlighter width}{\hl@width}%
  \pgfmathsetlengthmacro{\hl@width}{\hl@width - 1pt}%
  \path (hlstart#1);
  \path (hlend#1);
  \draw[highlight] (hlstart#1) -- (hlend#1);
  \draw[highlight] (hlstart#1) -- (hlstart#1 -| page.east);
  \pgfmathsetlengthmacro{\hl@sy}{\hl@sy -\hl@width}%
  \pgfmathsetlengthmacro{\hl@ey}{\hl@ey +\hl@width}%
  \draw[highlight] (0,\hl@sy -| page.west) -- (0,\hl@sy -| page.east);
  \pgfmathsetlengthmacro{\hl@sy}{\hl@sy -\hl@width}%
  \draw[highlight] (hlend#1 -| page.west) -- (hlend#1);

    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}


\setbeamertemplate{background canvas}[highlighter]

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

With a sample:

highlighted text

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.