0

I can use TikZ to highlight one or more terms/factors/exponents/etc. in a mathematical expression. But when I have two highlighted entities in a row extra space is introduced that I would like to eliminate, even if that means the highlighting overlaps surrounding entities a bit.

Here is a MWE that illustrates this.

% !TEX TS-program = lualatexmk
\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes}

\NewDocumentCommand{\hilite}{ O{brown!60} m O{rectangle} }{% % See https://texample.net/tikz/examples/beamer-arrows/ \tikz[baseline]{\node[fill=#1,#3,anchor=base]{\ensuremath{#2}};}% previous working version }%

\begin{document} \begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align} \begin{align} \hilite[orange]{\Delta\mathbf{p}}[circle] &= \mathbf{F}{\textnormal{net}},\Delta t \ \Delta\mathbf{p} &= \hilite[yellow!50]{\mathbf{F}{\textnormal{net}}}[rounded rectangle],\Delta t \ \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\hilite[olive!50]{\Delta t}[rectangle] \ \Delta\mathbf{p} &= \hilite[cyan!50]{\mathbf{F}{\textnormal{net}},\Delta t}[ellipse] \end{align} \end{document}

Here is the result. Note the extra space introduced. I would like to eliminate that.

Example with extra space

If I change my hilite command to

\NewDocumentCommand{\hilite}{ O{brown!60} m O{rectangle} }{%
  % See https://tex.stackexchange.com/a/406084/218142
  % See also https://tex.stackexchange.com/a/570789/218142
  % See also https://tex.stackexchange.com/a/79659/218142
  % See also https://tex.stackexchange.com/q/375032/218142
  \tikz[overlay,baseline=(n1.base)]{\node[fill=#1,draw,#3] (n1){\ensuremath{#2}};}%
}%

the result looks like this,

enter image description here

which introduces less space but obviously doesn't work. I've tried to understand the overlay option, and I understand why it causes the collapsed view here. I think the solution may lie with using \tikzmark but I can't seem to understand that concept. Ideally, I would like to be able to hilight any quantity, symbol, operator, etc. while in math mode. The only way I can think of to verbalize how I think the highlighting should look is that is should look like a "spotlight" centered on whatever I pass to the command, and overlapping onto other entities is okay. The shape and color of the "spotlight" can also be specified.

1 Answers1

1

It is not too difficult to literally answer the question. I switch to tikzmark as this library is made for such things. One just need to add a node that has the desired shape underneath using the behind path option.

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit,shapes,tikzmark}

\NewDocumentCommand{\hilite}{ O{brown!60} m O{rectangle} }{% % See https://texample.net/tikz/examples/beamer-arrows/ \tikzmarknode[alias=n,append after command={% node[behind path,fill=#1,#3,overlay,fit=(n)]{}% }]{n}{#2}% }%

\begin{document} \begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align} \begin{align} \hilite[orange]{\Delta\mathbf{p}}[circle] &= \mathbf{F}{\textnormal{net}},\Delta t \ \Delta\mathbf{p} &= \hilite[yellow!50]{\mathbf{F}{\textnormal{net}}}[rounded rectangle],\Delta t \ \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\hilite[olive!50]{\Delta t}[rectangle] \ \Delta\mathbf{p} &= \hilite[cyan!50]{\mathbf{F}{\textnormal{net}},\Delta t}[ellipse] \end{align} \end{document}

enter image description here

Almost certainly this is not what you want. It does fulfill the requirements but since the colored areas overlap with earlier elements of the equations, they erase them. This can be cured with some atbegshi trickery. However, I am wondering if this is what you really want. After all, fulfilling your requirements seem to imply that you highlight stuff that is outside of the node that you want to highlight.

On a different note, I'd also like to draw your attention to \tcbhighmath from tcolorbox.

ADDENDUM: This is the version with ship out trickery.

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{eso-pic}
\usetikzlibrary{fit,shapes,tikzmark}
\newcounter{tikzhighlightnode}
\NewDocumentCommand{\hilite}{ O{brown!60} m O{rectangle} }{%
  % See https://texample.net/tikz/examples/beamer-arrows/
  \stepcounter{tikzhighlightnode}%
  \tikzmarknode{highlighted-node-\number\value{tikzhighlightnode}}{#2}%
  \edef\temp{\noexpand\AddToShipoutPictureBG*{\noexpand\begin{tikzpicture}[overlay,remember picture]
  \noexpand\node[fill=#1,#3,fit=(highlighted-node-\number\value{tikzhighlightnode})]{};%
  \noexpand\end{tikzpicture}}}%
  \temp%
}%

\begin{document} \begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align} \begin{align} \hilite[orange]{\Delta\mathbf{p}}[circle] &= \mathbf{F}{\textnormal{net}},\Delta t \ \Delta\mathbf{p} &= \hilite[yellow!50]{\mathbf{F}{\textnormal{net}}}[rounded rectangle],\Delta t \ \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\hilite[olive!50]{\Delta t}[rectangle] \ \Delta\mathbf{p} &= \hilite[cyan!50]{\mathbf{F}{\textnormal{net}},\Delta t}[ellipse] \end{align} \end{document}

enter image description here

P.S. I am not a fan of the optional arguments using \NewDocumentCommand, at least not if I have pgf at my disposal. You could just have one argument, which you feed with an arbitrary number of keys.

  • Let me study this solution. As you say, it literally meets the specs but now I see why the earlier elements weren't showing up, whereas before that was a mystery. I didn't think about overlapping, so I've learned something. atbegshi seems complicated upon looking it up (I'm not even sure what shipout means). I know about \tcbhighmath so if there's a better way I'm open to it. – LaTeXereXeTaL Nov 14 '20 at 19:37
  • 1
    @LaTeXereXeTaL Basically there are two options: either the fill exceeds the box that get allocated to the element you want to highlight, and you have to do something to the ship out, or you only change things within the box which is allocated to the box. You can still have ellipses and so on, but they won't cover everything. –  Nov 14 '20 at 19:43
  • Does that apply to \tcbhighmath too? – LaTeXereXeTaL Nov 14 '20 at 20:25
  • @LaTeXereXeTaL Yes. –  Nov 14 '20 at 20:26
  • Setting opacity=0.5 seems to give something pleasing. I've read about \shipout but I can't find an example of its application to something like this. – LaTeXereXeTaL Nov 15 '20 at 02:45
  • 1
    @LaTeXereXeTaL I added a version using some ship out trickery. –  Nov 15 '20 at 03:17
  • Could you briefly explain how the trickery works? – LaTeXereXeTaL Nov 15 '20 at 11:28
  • 1
    @LaTeXereXeTaL It makes sure that the fit nodes gets shipped out before the rest of the page. This works as long as there are not too many other macros that also use the ship out hooks. –  Nov 15 '20 at 14:14
  • I just discovered if I'm using \hilite inside an environment (e.g. align) that is forced to be on the next page, the highlighting doesn't happen. I think that's because the shipout only applies to the current page, correct? – LaTeXereXeTaL Nov 21 '20 at 02:15
  • @LaTeXereXeTaL This is part of the reason, probably. I think that there is a chance that another part of the reason is the "measuring" thingy that let's the stuff inside some amsmath run through twice. (See e.g. here for a way to switch this one off.) I would think that in both cases the problem is fixable. One can check if the tikzmark exists on the current page, and then use \AddToShipoutPictureBG without a star. You might want to ask a new question with an explicit example in which this happens.... –  Nov 21 '20 at 02:29
  • Thanks. Just found this: https://tex.stackexchange.com/q/484285/218142. Is this applicable? – LaTeXereXeTaL Nov 21 '20 at 02:45
  • @LaTeXereXeTaL I think that in principle yes, but the good news is that the updated tikzmark library has (improved versions of) all these checks built in. –  Nov 21 '20 at 02:47
  • I'm looking at the tikzmark docs now. – LaTeXereXeTaL Nov 21 '20 at 02:52