1

This is an extension of my previous question How can I highlight part of an equation with TikZ but without extra space around the highlighted part?. The new question is, how do I make the hilite command work over a page break? It was suggested that the \iftikzmarkoncurrentpage mechanism might work, but I'm having difficulty with the required logic. I also found Tikz picture with tikzmark across page boundary won't print if inside environment, Leaving Whitespace for Hidden Text, Math, and Figures, and some other posts that may be relevant.

I understand the difference between \AddToShipoutPictureBG and \AddToShipoutPictureBG* and with the former, every highlight background is shipped out to every page rather than just the current page. I am obviously am not doing something correctly.

This MWE is the closest I've come to getting a working solution.

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

% This original works, except over pagebreaks! %\newcounter{tikzhighlightnode} %\NewDocumentCommand{\hilite}{ O{magenta!60} m O{rectangle} }{% % % Code by anonymous user abcdefg % % See https://texample.net/tikz/examples/beamer-arrows/ % % See also https://tex.stackexchange.com/a/406084/218142 % % See also https://tex.stackexchange.com/a/570858/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 % \stepcounter{tikzhighlightnode}% % \tikzmarknode{highlighted-node-\number\value{tikzhighlightnode}}{#2}% % \edef\temp{\noexpand\AddToShipoutPictureBG*{\noexpand\begin{tikzpicture}[overlay,remember picture] % \noexpand\node[inner sep=1.0pt,fill=#1,#3,fit=(highlighted-node-\number\value{tikzhighlightnode})]{};% % \noexpand\end{tikzpicture}}}% % \temp% %}%

\newcounter{tikzhighlightnode} \NewDocumentCommand{\hilite}{ O{magenta!60} m O{rectangle} }{% % Modified by me. % See https://texample.net/tikz/examples/beamer-arrows/ % See also https://tex.stackexchange.com/a/406084/218142 % See also https://tex.stackexchange.com/a/570858/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 \stepcounter{tikzhighlightnode}% \tikzmarknode{highlighted-node-\number\value{tikzhighlightnode}}{#2}% \iftikzmarkoncurrentpage{\thetikzhighlightnode}% \else \edef\temp{% \noexpand\AddToShipoutPictureBG{% \noexpand\begin{tikzpicture}[overlay,remember picture]% \noexpand\node[inner sep=1.0pt,fill=#1,#3,fit=(highlighted-node-\number\value{tikzhighlightnode})]{};% \noexpand\end{tikzpicture}% }% }% \temp% \fi }%

\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} + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[brown]{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[cyan!50]{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \end{align}

\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 \ (\Delta s)^{\hilite{2}[circle]} &= \hilite[green]{-}[circle](\Delta t)^{\hilite[cyan]{2}[circle]}+ (\Delta x)^{\hilite[orange]{2}[circle]} + (\Delta y)^{\hilite[blue!50]{2}[circle]} + (\Delta z)^{\hilite[violet!45]{2}[circle]} \end{align}

\begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[blue!50]{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[green!50]{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[magenta!70]{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align}

\begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[violet!50]{-(\Delta t)^2+(\Delta x)^2}[rounded rectangle]+(\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[darkgray!50]{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[purple!50]{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align}

\begin{align} \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\Delta t \ \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] \ \hilite{\Delta\mathbf{p}}[rectangle] &= \mathbf{F}{\textnormal{net}},\Delta t \end{align}

\begin{align} \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\Delta t \ \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] \ \hilite{\Delta\mathbf{p}}[rectangle] &= \mathbf{F}{\textnormal{net}},\Delta t \end{align} \end{document}

1 Answers1

2

I think your strategy works. The only issue seems to be that \iftikzmarkoncurrentpage{...} requires the full name of the node. And you want to draw the node if the tikzmark is on the current page.

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

% This original works, except over pagebreaks! %\newcounter{tikzhighlightnode} %\NewDocumentCommand{\hilite}{ O{magenta!60} m O{rectangle} }{% % % Code by anonymous user abcdefg % % See https://texample.net/tikz/examples/beamer-arrows/ % % See also https://tex.stackexchange.com/a/406084/218142 % % See also https://tex.stackexchange.com/a/570858/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 % \stepcounter{tikzhighlightnode}% % \tikzmarknode{highlighted-node-\number\value{tikzhighlightnode}}{#2}% % \edef\temp{\noexpand\AddToShipoutPictureBG*{\noexpand\begin{tikzpicture}[overlay,remember picture] % \noexpand\node[inner sep=1.0pt,fill=#1,#3,fit=(highlighted-node-\number\value{tikzhighlightnode})]{};% % \noexpand\end{tikzpicture}}}% % \temp% %}%

\newcounter{tikzhighlightnode} \NewDocumentCommand{\hilite}{ O{magenta!60} m O{rectangle} }{% % Modified by me. % See https://texample.net/tikz/examples/beamer-arrows/ % See also https://tex.stackexchange.com/a/406084/218142 % See also https://tex.stackexchange.com/a/570858/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 \stepcounter{tikzhighlightnode}% \tikzmarknode{highlighted-node-\number\value{tikzhighlightnode}}{#2}% \edef\temp{% \noexpand\AddToShipoutPictureBG{% \noexpand\begin{tikzpicture}[overlay,remember picture]% \noexpand\iftikzmarkoncurrentpage{highlighted-node-\number\value{tikzhighlightnode}}% \noexpand\node[inner sep=1.0pt,fill=#1,#3,fit=(highlighted-node-\number\value{tikzhighlightnode})]{};% \noexpand\fi \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} + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[brown]{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[cyan!50]{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \end{align}

\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 \ (\Delta s)^{\hilite{2}[circle]} &= \hilite[green]{-}[circle](\Delta t)^{\hilite[cyan]{2}[circle]}+ (\Delta x)^{\hilite[orange]{2}[circle]} + (\Delta y)^{\hilite[blue!50]{2}[circle]} + (\Delta z)^{\hilite[violet!45]{2}[circle]} \end{align}

\begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[blue!50]{-(\Delta t)^2 + (\Delta x)^2}[rounded rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[green!50]{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[magenta!70]{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align}

\begin{align} (\Delta s)^2 &= -(\Delta t)^2 + (\Delta x)^2 + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[violet!50]{-(\Delta t)^2+(\Delta x)^2}[rounded rectangle]+(\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[darkgray!50]{-(\Delta t)^2 + (\Delta x)^2}[rectangle] + (\Delta y)^2 + (\Delta z)^2 \ (\Delta s)^2 &= \hilite[purple!50]{-(\Delta t)^2 + (\Delta x)^2}[ellipse] + (\Delta y)^2 + (\Delta z)^2 \end{align}

\begin{align} \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\Delta t \ \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] \ \hilite{\Delta\mathbf{p}}[rectangle] &= \mathbf{F}{\textnormal{net}},\Delta t \end{align}

\begin{align} \Delta\mathbf{p} &= \mathbf{F}{\textnormal{net}},\Delta t \ \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] \ \hilite{\Delta\mathbf{p}}[rectangle] &= \mathbf{F}{\textnormal{net}},\Delta t \end{align} \end{document}

enter image description here