19

The issue of breakable highlighting was addressed in Cool Text Highlighting in LaTeX

But how, in latex, do I highlight text involving math, something like

enter image description here

which, right now, I do with a succession of \texthl and \colorbox.

schremmer
  • 2,107

2 Answers2

31

Is this what you intended?

\documentclass{article}


\usepackage{xcolor}
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}

\begin{document}
For inline math, one can simply do \hl{colored $a=b$ math}.  For display math, the following works:
\begin{equation}
\mathcolorbox{red}{y=\frac{x^2}{q}}+z
\end{equation}  
\end{document}

enter image description here

[** EDIT **]

Ok, see \hlfancy below

\documentclass{article}


\usepackage{xcolor}
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\newcommand{\hlfancy}[2]{\sethlcolor{#1}\hl{#2}}

\begin{document}
For inline math, one can simply do \hl{colored $a=b$ math}.  For display math, the following works:
\begin{equation}
\mathcolorbox{red}{y=\frac{x^2}{q}}+z
\end{equation}  
And for the fancy version: \hlfancy{orange}{colored $a=b$ math}.  Now, \hlfancy{green}{colored $a=b$ math}.
\end{document}
JPi
  • 13,595
  • 2
    If you want to change the color used in the \hl command provided by the soul package then you can use e.g. \sethlcolor{green} but note the fragility issues mentioned in http://tex.stackexchange.com/questions/260566/how-to-reliably-switch-the-highlighting-color-with-soul – JPi Jul 12 '16 at 01:13
  • 1
    This is what I intended. Well, almost. :-)) What I really would like is, instead of setting the color in the preamble which would force me to define one command per color, to define a command for \hl in which the color is an option, namely something like inlinecolorbox{<color>}{<text with math>} As for creating a robust command, that's even more beyond me . By the way, the reason \hl had not worked for me is that instead of using $ $ to delimit math, I was using \( \) which soul does not seem to recognize. – schremmer Jul 12 '16 at 18:16
  • ok, see second version. – JPi Jul 12 '16 at 19:58
  • 2
    \hlfancy works like magic. I wish I could take the time to understand LaTeX instead of sponging. Well, thanks very much for your patience. – schremmer Jul 12 '16 at 21:02
  • You may want to look at the todonotes package for other functionality you might like. – JPi Jul 12 '16 at 23:03
  • I have and I regret having done so because I cannot afford to spend anymore time on what it offers, as good as it is: I really have to get back to writing the actual text. But, when I am done, I will study it for the fourth edition! So, thanks for the suggestion. – schremmer Jul 13 '16 at 01:30
  • An oddity: even though $\mathcolorbox{red}{x^{2}}$ works, \colorbox cannot be replaced by \mathcolorbox in `$f(x)=\underset{\text{\textbf{code}}}{\underbrace{\colorbox{red}{$x^{2}$}}}$' – schremmer Jul 15 '16 at 17:30
  • remove the $ signs around x^{2}. – JPi Jul 15 '16 at 19:49
0

You can also use \colorbox instead of \mathcolorbox. Here's an example:

\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}

\begin{document}

Here is an equation: \begin{equation} \colorbox{yellow}{$E = mc^2$} \end{equation}

\end{document}

In this example, the \colorbox command is used to highlight the equation with a yellow background. The equation is enclosed in $ symbols to put it in math mode, which is required for equations. You can replace yellow with any other color that you prefer.

Pankaj Singh
  • 107
  • 3