As the title says, I would like to highlight all the content of $...$
, \(...\)
, \begin{math}...\end{math}
in my document.
My last trial, however not really well asked was:
Background color for inline math with linebreaks?
Unfortunately, after finally testing the proposed solution I found out, that:
- It fails when math gets taller – in example using
\frac
or\displaystyle
. - It fails when math extends over
\textwidth
– one of the purposes of highlighting would be to spot overfull lines. - The need to mark every equation with a separate pair of markers is cumbersome.
I tried to fix some of the issues, but I got completely lost in the code.
Update
@jfbu's answer looks pretty satisfying. The only test case I can think of at the moment, which is not covered is
Finally, while display equations look good for a page of samples,
the ability to mix math and text
\(\displaystyle \frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } }\)
The amssymb package might be omissible for documents whose math.
I guess constant height would give it a better feel.
Next update
As stated in the updated answer, mixing height of the content that is spread over a few lines gives unwanted results, as in
Finally, while display equations look good for a page of samples,
the ability to mix math and text \(\displaystyle ABC_{1234} +
ABC_{1234} - ABC_{1234} + ABC_{1234} - ABC_{1234} =
\frac{1}{(\sqrt{\phi \sqrt{5}}-\phi) e^{\frac25 \pi}} =
1+\frac{e^{-2\pi}} {1+\frac{e^{-4\pi}} {1+\frac{e^{-6\pi}}
{1+\frac{e^{-8\pi}} {1+\ldots} } } }\).
The amssymb package might be omissible for certain documents.
which treats each line as it was tall
Is it possible to make highlighting line-aware?
I hope I don't cross the line, as the great job have already been done here. Anyway this one step could make it flawless.
Best Answer
Hard work has led to something better. But this is not strictly
TeX
it uses the\pdfsavepos
via the linegoal package.Thus, the command
\Highlight
defined here can be used to give a colored background to portion from A to B in a paragraph, with linebreaks authorized. Although the OP's question was about inline math, in the end the final answer has no specific link to math. The macro, theoretically changes nothing to the layout of the paragraph, apart from adding the colored backgrounds.To avoid these color bars overwriting descenders elsewhere, the user may add
\strut's
, just before and just after both the starting point and the ending point of the argument to the macro; this is not done automatically (but it could at least be as an option). If the macro is to be used already at the start of a paragraph, it should be preceded by\indent
or\noindent
.The macro is not able to cope with page breaks occurring inside the highlighted material. And it is not for use on multi-paragraphs at once.
The material is typeset three times, the first two inside temporary
\vboxes
but this will impact\LaTeX
counters as they are global. Some more refined version of the macro would take care of such things.The whole thing could not work without pdftex
\pdfsavepos
, via package linegoal.Here is an evolution of my earlier proposal. Now the highlighting will try to compute first largest height and depth; of course, this means that if the material splits over lines and one piece is very high/deep this influences everything, anyhow in such cases, display math would be a better choice.
As to "highlighting environments" as has been asked in a comment, if this refers to display environments, some other method is necessary. Wrapping the environment in another one of the
framed
ormdframed
type, I guess. The method here is strictly for inline math. Actually it may also serve to display the possible hyphenation points in a word (indeed the method, inspired by Tex by Topic, 5.9.6 Dissecting paragraphs with \lastbox,texdoc topic
, is also used in more elaborate form in thesoul
package).Edit: the global
\sbox
now uses\box1
rather than\HighlightBox
, as I recall that local and global assignments should not be mixed although here I don't know if this would have any importance in this code in terms of save stack impact.This is the earlier proposal:
Output: