如何使环境内的“tikzmark”在分页符上起作用?

如何使环境内的“tikzmark”在分页符上起作用?

这是我上一个问题的延伸如何使用 TikZ 突出显示方程的一部分,但不在突出显示的部分周围留出多余的空间?。新的问题是,如何让hilite命令在分页符上工作?有人建议这种\iftikzmarkoncurrentpage机制可能有效,但我对所需的逻辑感到困难。我还发现如果在环境中,带有跨页面边界的 tikzmark 的 Tikz 图片将不会打印为隐藏文本、数学和数字留出空白,以及其他一些可能相关的帖子。

\AddToShipoutPictureBG我理解和之间的区别\AddToShipoutPictureBG*,前者的每个高亮背景都会发送到每个页面,而不仅仅是当前页面。我显然做错了什么。

这个 MWE 是我最接近获得可行的解决方案的。

% !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

我认为你的策略是有效的。唯一的问题似乎是\iftikzmarkoncurrentpage{...}需要节点的全名。如果 tikzmark 在当前页面上,你希望绘制该节点。

\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}

在此处输入图片描述

相关内容