The issue of breakable highlighting was addressed in Cool Text Highlighting in LaTeX
But how, in latex
, do I highlight text involving math
, something like
which, right now, I do with a succession of \texthl
and \colorbox
.
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityThe issue of breakable highlighting was addressed in Cool Text Highlighting in LaTeX
But how, in latex
, do I highlight text involving math
, something like
which, right now, I do with a succession of \texthl
and \colorbox
.
Is this what you intended?
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\begin{document}
For inline math, one can simply do \hl{colored $a=b$ math}. For display math, the following works:
\begin{equation}
\mathcolorbox{red}{y=\frac{x^2}{q}}+z
\end{equation}
\end{document}
[** EDIT **]
Ok, see \hlfancy
below
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\newcommand{\hlfancy}[2]{\sethlcolor{#1}\hl{#2}}
\begin{document}
For inline math, one can simply do \hl{colored $a=b$ math}. For display math, the following works:
\begin{equation}
\mathcolorbox{red}{y=\frac{x^2}{q}}+z
\end{equation}
And for the fancy version: \hlfancy{orange}{colored $a=b$ math}. Now, \hlfancy{green}{colored $a=b$ math}.
\end{document}
\hl
command provided by the soul
package then you can use e.g. \sethlcolor{green}
but note the fragility issues mentioned in tex.stackexchange.com/questions/260566/…
\hl
in which the color is an option, namely something like inlinecolorbox{<color>}{<text with math>}
As for creating a robust command, that's even more beyond me . By the way, the reason \hl
had not worked for me is that instead of using $ $
to delimit math, I was using \( \)
which soul does not seem to recognize.
Jul 12, 2016 at 18:16
\hlfancy
works like magic. I wish I could take the time to understand LaTeX instead of sponging. Well, thanks very much for your patience.
Jul 12, 2016 at 21:02
You can also use \colorbox
instead of \mathcolorbox
. Here's an example:
\documentclass{article}
\usepackage{xcolor}
\usepackage{amsmath}
\begin{document}
Here is an equation:
\begin{equation}
\colorbox{yellow}{$E = mc^2$}
\end{equation}
\end{document}
In this example, the \colorbox
command is used to highlight the equation with a yellow background. The equation is enclosed in $
symbols to put it in math mode, which is required for equations. You can replace yellow with any other color that you prefer.
empheq
package