Is there any way to display math expressions with a background color (highlight) using AMS-Tex or AMS-LaTeX . For example, using AMS-TeX/LaTeX, we can display a math expression in font color using \color[colorName]{mathExpression}.

UPDATE Using the answer from @werner, I tried $\frac{\colorbox{yellow}{$a-b$}}{c-d}$ to highlight only the numerator but the size of a-b is much larger than the demominator c-d even with the setting of \setlength{\fboxsep}{1pt} -in the preamble - as shown below. How can I get the desired output shown below:

enter image description here

  • 4
    If you load xcolor, you can use \colorbox{<color>}{$<math content>$}. This uses a regular "framed box", so you can set the values of \fboxsep to decrease the border.
    – Werner
    Dec 2, 2012 at 6:27
  • ams-tex is plain-based, not latex, so although it's feasible to concoct a method to include color, it's not trivial, and i don't know of any package that might do it. ams-latex, on the other hand, is based (as its name implies) on latex, so @Werner's suggestion is one good possibility. Dec 2, 2012 at 13:33
  • @Werner, thank you for providing this information. I thought \colorbox command is only for highlighting plain text in color. For example, \colorbox{yellow}{x^3} would highlight the plain text x^3 in yellow and not in math-mode as ‘x cubed’. Whereas, the font color command, e.g., \color{yellow}{x^3} would display ‘x cubed’ in math-mode with yellow font. Now, I know that, for \colorbox, we need to enclose the math expression with delimiter such as \colorbox{yellow}{$x^3$}.
    – nam
    Dec 8, 2012 at 4:19
  • @nam: I've converted my comment into an answer, since it seemed to have satisfied your requirements.
    – Werner
    Dec 9, 2012 at 8:22
  • @Werner, sorry I did not know at that time about marking a response as an answer. I have marked it as an answer now.
    – nam
    Jun 1, 2014 at 22:20

1 Answer 1


If you load xcolor, you can use \colorbox. This uses a regular "framed box", so you can set the values of \fboxsep to decrease the border.

In most cases, text-related formatting of this sort also transfers to math-related content. However, a verbatim switch is sometimes required since boxing reverts back to text mode. As such, use

\colorbox{<color>}{$<math content>$}

and not

$\colorbox{<color>}{<math content>}$

Here's a small example in LaTeX illustrating the use of \colorbox together with a modification of \fboxsep:

enter image description here

\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\setlength{\parindent}{0pt}% Just for this example
Here is \colorbox{blue!50}{coloured text} with a coloured \colorbox{green!30}{$f(x)$} function. \par \bigskip
\setlength{\fboxsep}{2pt}% No "border" around coloured box
Here is \colorbox{blue!50}{coloured text} with a coloured \colorbox{green!30}{$f(x)$} function. \par
  • 3
    herbert voss wrote a good summary -- which may be read from CTAN [MathCol](mathCol) which is pretty clear (even to non-readers of german) sinc mirror.ctan.org/info/math/voss/mathCol/mathCol.pdf since all the examples come with source+typeset output. Dec 9, 2012 at 10:54
  • i find writing in this sort of medium very difficult. have to find all my typos within 5 minutes; failed with my previous own afaict). anyway, the link should be mathCol Dec 9, 2012 at 11:04

You must log in to answer this question.

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