Streamline use of Highlight in text and equations

3,732

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

Share:
3,732

Related videos on Youtube

Clayton
Author by

Clayton

I'm a graduate student at UR in NY.

Updated on August 08, 2020

Comments

  • Clayton
    Clayton over 3 years

    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.

    • Admin
      Admin over 8 years
      There's also the pdfcomment package, which allows for annotations and hovering up tooltips.
    • Clayton
      Clayton over 8 years
      @ChristianHupfer: I'll look into it! Thanks for the suggestion.
    • Admin
      Admin over 8 years
      You're welcome. But Gonzalos answer is good!
  • Clayton
    Clayton over 8 years
    Gonzalo, this may answer the unasked question of how to handle multiple lines of equations (I hadn't even thought about that!), so thank you! More of what I am after, though, is something that won't be interrupted by math equations/environments (in the same way that I only need to declare the color of the font once and it can run that way through multiple pages, environments, etc.).
  • Gonzalo Medina
    Gonzalo Medina over 8 years
    @Clayton you can use a tcolorbox. In some minutes I will update.
  • Clayton
    Clayton over 8 years
    Thank you for taking the time to answer it differently; I appreciate it greatly!
  • Gonzalo Medina
    Gonzalo Medina over 8 years
    @Clayton Please see my updated answer.
  • Clayton
    Clayton over 8 years
    This is exactly what I was looking for! Thanks!