[Tex/LaTex] Streamline use of Highlight in text and equations

highlightingsoul

I have been highlighting certain parts of text in order to make it easier for me to search through a long document to find parts that need expanded on or some sort of work is left to do; however, I've been having issues with highlighting equations. I have a bit of a workaround (courtesy of this answer). Inserting this every time seems a bit cumbersome, so I am looking for something that will perform the intended operation below seamlessly.

MWE

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{soul}
\newcommand{\hll}[1]{\colorbox{yellow}{$\displaystyle #1$}}

\begin{document}
    {\color{red}\hl{Here is some text, and now we make the observation} 
    \[ \hll{\lim_{n\to\infty}\frac{1}{n}=0.} \]}
\end{document}

I have tried using \colorbox, but only once because it didn't wrap the text in the way I expected (in fact, in that instance, no wrapping occurred whatsoever).

Update: Gonzalo Medina has provided a marvelous answer, though I'm not sure I was entirely clear above. The ideal answer to this question will have something that needs to be declared only once, just as the color of the font can be declared only once and pass through math mode, other environments such as lemmas, theorems, remarks, etc. without needing to be ended and declared again.

Best Answer

I'd like to suggest you the \tcbhighmath command from the tcolorbox package; its interaction with empheq will give you the possibility to easily highlight expressions inside the amsmath environments; a little example:

enter image description here

The code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{soul}
\usepackage{empheq}
\usepackage[many]{tcolorbox}

\newcommand{\hll}[1]{\colorbox{yellow}{$\displaystyle #1$}}

\tcbset{
  highlight math style={
    colback=yellow,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    top=2pt,
    bottom=2pt,
    left=2pt,
    right=2pt,
  }
}

\begin{document}

\hl{Here is some text, and now we make the observation} 
\[ 
\tcbhighmath{\lim_{n\to\infty}\frac{1}{n}=0.} 
\]

\hl{Here is some text, and now we make the observation} 
\begin{empheq}[box=\tcbhighmath]{align}
a&=\sin(z)\\
E&=mc^2 + \int_a^b x\, dx
\end{empheq}

\end{document}

If you want to highlight longer elments such as long paragraphs, with possible page breaks, and expressions, then you can use a breakable tcolorbox. A little example adding to the previous solution a breakable tcolorbox so now you can highlight individual texts or formulas as well as longer material:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{soul}
\usepackage{empheq}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}% just to generate filler text

\newcommand{\hll}[1]{\colorbox{yellow}{$\displaystyle #1$}}

\tcbset{
  myhlight/.style={
    colback=yellow,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt,
    top=2pt,
    bottom=2pt,
    left=2pt,
    right=2pt,
  },
  highlight math style={myhlight}
}

\newtcolorbox{myhl}{
  breakable,
  myhlight
}

\begin{document}

\hl{Here is some text, and now we make the observation} 
\[ 
\tcbhighmath{\lim_{n\to\infty}\frac{1}{n}=0.} 
\]

\hl{Here is some text, and now we make the observation} 
\begin{empheq}[box=\tcbhighmath]{align}
a&=\sin(z)\\
E&=mc^2 + \int_a^b x\, dx
\end{empheq}

\begin{myhl}
\lipsum[1-4]
\begin{align}
a&=\sin(z)\\
E&=mc^2 + \int_a^b x\, dx
\end{align}
\lipsum[3]
\end{myhl}

\end{document}

The result:

enter image description here