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:
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:
The \sethlcolor
macro does not understand the colour specifiction of x!y!z
etc, it can work with named colors being defined with \definecolor
only.
However, using xcolor
there is a trick to support the colour definition:
\colorlet{foo}{x!y!z!}
defines and transforms the specification into a colour named foo
(this will overwrite an existing definition of the colour named foo
, however.
Basically, it is similar to the macro \let\foo\foobar
statement.
Since all happens in a group (by definition of \hlc
), the new colour foo
is not known outside.
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\newcommand{\hlc}[2][yellow]{{%
\colorlet{foo}{#1}%
\sethlcolor{foo}\hl{#2}}%
}
\begin{document}
\hlc[pink]{hello}
\hlc[cyan!50]{hello}
\end{document}
Best Answer
Don't, please.
;-)
One might think to make a command that works both in text and in math:
The example shows that the gray background doesn't fully cover the exponent also in the standard version, so it's a
soul
problem.