19

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

enter image description here

which, right now, I do with a succession of \texthl and \colorbox.

4
  • have a look at the empheq package
    – JPi
    Jul 11, 2016 at 21:47
  • @JPi I took a (admittedly quick) look but I did not see anything like the above. Did I miss it?
    – schremmer
    Jul 11, 2016 at 22:23
  • 1
    Does How to “highlight” text/formulas with tikz? answer your question? Jul 12, 2016 at 1:21
  • Seems to; that's way more elaborate than my more direct answer below.
    – JPi
    Jul 12, 2016 at 1:23

2 Answers 2

31

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}

enter image description here

[** 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}
8
  • 2
    If you want to change the color used in the \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/…
    – JPi
    Jul 12, 2016 at 1:13
  • 1
    This is what I intended. Well, almost. :-)) What I really would like is, instead of setting the color in the preamble which would force me to define one command per color, to define a command for \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.
    – schremmer
    Jul 12, 2016 at 18:16
  • ok, see second version.
    – JPi
    Jul 12, 2016 at 19:58
  • 2
    \hlfancy works like magic. I wish I could take the time to understand LaTeX instead of sponging. Well, thanks very much for your patience.
    – schremmer
    Jul 12, 2016 at 21:02
  • You may want to look at the todonotes package for other functionality you might like.
    – JPi
    Jul 12, 2016 at 23:03
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .