# highlighting math

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

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

• have a look at the empheq package – JPi Jul 11 '16 at 21:47
• @JPi I took a (admittedly quick) look but I did not see anything like the above. Did I miss it? – schremmer Jul 11 '16 at 22:23
• Does How to “highlight” text/formulas with tikz? answer your question? – Peter Grill Jul 12 '16 at 1:21
• Seems to; that's way more elaborate than my more direct answer below. – JPi Jul 12 '16 at 1:23

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:
$$\mathcolorbox{red}{y=\frac{x^2}{q}}+z$$
\end{document}


[** 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:
$$\mathcolorbox{red}{y=\frac{x^2}{q}}+z$$
And for the fancy version: \hlfancy{orange}{colored $a=b$ math}.  Now, \hlfancy{green}{colored $a=b$ math}.
\end{document}

• 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 tex.stackexchange.com/questions/260566/… – JPi Jul 12 '16 at 1:13
• 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
• \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