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.
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,
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.




atbegshiseems complicated upon looking it up (I'm not even sure what shipout means). I know about\tcbhighmathso if there's a better way I'm open to it. – LaTeXereXeTaL Nov 14 '20 at 19:37\tcbhighmathtoo? – LaTeXereXeTaL Nov 14 '20 at 20:25opacity=0.5seems to give something pleasing. I've read about\shipoutbut I can't find an example of its application to something like this. – LaTeXereXeTaL Nov 15 '20 at 02:45\hiliteinside 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:15amsmathrun 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 thetikzmarkexists on the current page, and then use\AddToShipoutPictureBGwithout a star. You might want to ask a new question with an explicit example in which this happens.... – Nov 21 '20 at 02:29tikzmarklibrary has (improved versions of) all these checks built in. – Nov 21 '20 at 02:47tikzmarkdocs now. – LaTeXereXeTaL Nov 21 '20 at 02:52