[Tex/LaTex] highlighting math

colorboxsoul

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.

Best Answer

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