4

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.

  • There's also the pdfcomment package, which allows for annotations and hovering up tooltips. – user31729 Aug 7 '15 at 16:27
  • @ChristianHupfer: I'll look into it! Thanks for the suggestion. – Clayton Aug 7 '15 at 16:30
  • You're welcome. But Gonzalos answer is good! – user31729 Aug 7 '15 at 16:31
7

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

  • 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.). – Clayton Aug 7 '15 at 16:29
  • @Clayton you can use a tcolorbox. In some minutes I will update. – Gonzalo Medina Aug 7 '15 at 16:54
  • Thank you for taking the time to answer it differently; I appreciate it greatly! – Clayton Aug 7 '15 at 17:18
  • @Clayton Please see my updated answer. – Gonzalo Medina Aug 7 '15 at 17:33
  • This is exactly what I was looking for! Thanks! – Clayton Aug 7 '15 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.