填充和覆盖不能很好地协同工作

填充和覆盖不能很好地协同工作

在尝试回答这个问题时这里和 一样tikzmark,似乎[overlay]\fill不能同时使用。我有一个问题——请参见下面的代码。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{calc,tikzmark,backgrounds}
\begin{document}
I want to decorate some math formulae with \verb|tikzmark| of TikZ. For the \verb|\draw| command, it is fine!   
\begin{align*}
\tikzmarknode{F1}{F(p)}&=\tikzmarknode{F2}{\int_{0}^{+\infty} e^{-pt} e^{at}\,dt}\\[2mm]
&=\dfrac{1}{p-a}.
\end{align*}
\begin{tikzpicture}[overlay,remember picture]
\draw[magenta] (F1.north west)+(-.2,.4) rectangle ($(F2.south east)+(.2,-.15)$);
\end{tikzpicture}
For the \verb|\fill| command, there are some tricks like this
\begin{align*}
\tikzmarknode{G1}{G(p)}&\tikzmarknode{G2}{=\int_{0}^{+\infty} e^{-pt} \cos(at)\,dt}\\[2mm]
&=\dfrac{p}{p^2+a^2}.
\end{align*}
\begin{tikzpicture}[overlay,remember picture]
\begin{scope}[on background layer]
\fill[cyan!50] 
(G1.north west)+(-.2,.4) rectangle ($(G2.south east)+(.2,-.2)$);
\path
(G1) node{$G(p)$}
(G2) node{$=\int_{0}^{+\infty} e^{-pt}\cos(at)\,dt$};
\end{scope} 
\end{tikzpicture}
or like this
\begin{align*}
\tikzmarknode{H1}{H(p)}&\tikzmarknode{H2}{=\int_{0}^{+\infty} e^{-pt} \sin(at)\,dt}\\[2mm]
&=\dfrac{a}{p^2+a^2}.
\end{align*}
\begin{tikzpicture}[remember picture,overlay]
\draw[fill=yellow,opacity=.5] 
(H1.north west)+(-.2,.4) rectangle ($(H2.south east)+(.2,-.2)$);
\end{tikzpicture}
However, both of these tricks are not good.

I expect the \verb|\fill| command works well as the \verb|\draw| command in the above situations. Is any way to do that?
\end{document}

更新:根据 Andrew Stacey 之前的回答,我使用tikzmark而不是tikzmarknode,并将 放在tikzpicture\fill填充的数学公式之前。

在此处输入图片描述

% based on a previous answer by Andrew Stacey 
\documentclass{article}
\usepackage{tikz,amsmath,lipsum}
\usetikzlibrary{calc,tikzmark}
\begin{document}
\lipsum[1]
\begin{align*}
\tikzmark{F1}F(p)&=\int_{0}^{+\infty} e^{-pt} e^{at}\,dt \tikzmark{F2}\\[2mm]
&=\dfrac{1}{p-a}.
\end{align*}
\begin{tikzpicture}[overlay,remember picture]
\draw[magenta] (pic cs:F1)+(-.2,-.5) rectangle ($(pic cs:F2)+(.2,.65)$);
\end{tikzpicture}
\lipsum[2]
% This tikzpicture (with \fill) must go before the math formula
\begin{tikzpicture}[overlay,remember picture]
\draw[fill=yellow,rounded corners] (pic cs:G1)+(-.2,-.5) rectangle ($(pic cs:G2)+(.2,.65)$);
\end{tikzpicture}
\begin{align*}
\tikzmark{G1}{G(p)}&={\int_{0}^{+\infty} e^{-pt} \cos(at)\,dt}\tikzmark{G2}\\[2mm] 
&=\dfrac{p}{p^2+a^2}.
\end{align*}
\lipsum[3]
\end{document}

答案1

为了\fill正确地得到矩形,tikzpicture定义矩形的方程式已排版。这意味着在定义坐标之前需要先提供坐标。幸运的是,这相对简单:需要将它们写入文件并在下次编译时读回。

Tikzmarks 已经具备此功能,因此使用 tikzmarks 作为坐标即可。稍微有点烦人的是,定义矩形所需的坐标不是 tikzmarknode 创建的 tikzmarks,因此我们在方程后使用另一个 tikzpicture 来确定要保存的坐标。

tikzmark 库定义了另一种方法,可用于将有关节点的信息保存到外部文件并在适当的时刻将其读回。这是关键save node。但是,这有一个问题,即当\tikzmarknode在数学模式下定义时,实际上会定义许多节点以确保节点文本设置为正确的数学样式。保存的节点名称不是传入的 tikzmarknode 名称。

这感觉像是应该修复的问题,所以我会研究一下。与此同时,由于 tikzmarknodes 是以显示样式定义的,因此保存的名称将是<name>-d.a

\documentclass{article}
%\url{https://tex.stackexchange.com/q/629644/86}
\usepackage{tikz,amsmath}

\usetikzlibrary{calc,tikzmark,backgrounds}

% Load in the saved node information from previous runs.
\tikzset{
  restore nodes from file,
}

\begin{document}

In this version, we use the fact that \verb|tikzmark|s are always available prior to their declaration in the document.
So we put tikzmarks at the corners of the desired rectangle.
%
\begin{tikzpicture}[overlay,remember picture]
\fill[cyan!50] 
(pic cs:Gnw)+(-.2,.4) rectangle ($(pic cs:Gse)+(.2,-.2)$);
\end{tikzpicture}

\begin{align*}
\tikzmarknode{G1}{G(p)}&\tikzmarknode{G2}{=\int_{0}^{+\infty} e^{-pt} \cos(at)\,dt}\\[2mm]
&=\dfrac{p}{p^2+a^2}.
\end{align*}
%
This is the tikz picture that puts the tikzmarks at the rectangle's corners.
%
\begin{tikzpicture}[overlay,remember picture]
\tikzmark{Gnw}{(G1.north west)}
\tikzmark{Gse}{(G2.south east)}
\end{tikzpicture}

In this version, we use the \verb+save node+ facility of the tikzmark library.
There's a slight hitch in that when a \verb|\tikzmarknode| is defined in maths mode then actually quite a lot of nodes are created and they are all saved.
Those nodes have names derived from the given name and normally the one that is actually used is aliased to the given name.
However, that aliasing happens when the node is defined so if we want to use it beforehand then we need to refer to the actual name.
This might get fixed in later versions.
%
\begin{tikzpicture}[remember picture,overlay]
\draw[fill=yellow] 
(H1-d.north west)+(-.2,.4) rectangle ($(H2-d.south east)+(.2,-.2)$);
\end{tikzpicture}
\begin{align*}
\tikzmarknode[save node]{H1}{H(p)}&\tikzmarknode[save node]{H2}{=\int_{0}^{+\infty} e^{-pt} \sin(at)\,dt}\\[2mm]
&=\dfrac{a}{p^2+a^2}.
\end{align*}
However, both of these tricks are not good.

I expect the \verb|\fill| command works well as the \verb|\draw| command in the above situations. Is any way to do that?
\end{document}

相关内容