I would like to color the background of characters (word, sentences or math variables) with the same command in text paragraphs and in equations.
The command \colorbox{color}{some text}
doesn't do the job because it is not able to break lines, and it is not usable with math content.
Then I tried to use the soul
package as given here. This is very close to what I want except it doesn't work within math environment.
Here is a MWE (the problematic part is commented and marked by a red dot on image):
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\sethlcolor{gray}
\begin{document}
This is a long text without highlighting. \hl{This is a long text with highlighting over the line break.} This is a long text without highlighting.
This is an equation with highlighted variable:
$
1+x^2+ %\hl{x^3}
$
\end{document}
Thank you for your help.