第一种方法

第一种方法

我可以使用图层作为:

Some
\begin{tikzpicture}[baseline]
\node [anchor=base,inner sep=0pt] (X) at (0,0) {Highlighted};
\begin{pgfonlayer}{background layer}
    \draw [fill=orange, fill opacity=0.9, baseline] (X.south west) rectangle (X.north east); 
\end{pgfonlayer}
\end{tikzpicture}  Text v1.

产生所需的输出,即下图中的第一行:

在此处输入图片描述

\MyTikzMark但是,这相当麻烦。在需要时创建节点要容易得多

Some \MyTikzMark{MyNode}{Highlighted} Text v2.

然后稍后通过访问进行绘图MyNode。但是,使用此模式,我无法获得突出显示on background layer,最终得到上图中的第二条线。

更令人惊讶的是,使用试图模仿“繁琐”解决方案的强力方法却失败了:

\newcommand{\MyTikzMarkA}[2]{% Same behavior as \MyTikzMark.
    \begin{tikzpicture}[baseline,remember picture] 
        \begin{pgfonlayer}{foreground layer}
            \node[anchor=base,inner sep=0pt] (#1) {#2};%
        \end{pgfonlayer}
    \end{tikzpicture}%
}%

代码:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{backgrounds}
\pgfdeclarelayer{background layer} 
\pgfdeclarelayer{foreground layer} 
\pgfsetlayers{background layer,main,foreground layer}

\newcommand{\MyTikzMark}[2]{%
    \tikz[baseline,remember picture] \node[anchor=base,inner sep=0pt] (#1) {#2};%
}%
%\newcommand{\MyTikzMarkA}[2]{% Same behavior as above.
%    \begin{tikzpicture}[baseline,remember picture] 
%        \begin{pgfonlayer}{foreground layer}
%            \node[anchor=base,inner sep=0pt] (#1) {#2};%
%        \end{pgfonlayer}
%    \end{tikzpicture}%
%}%


\begin{document}
% This works!
Some
\begin{tikzpicture}[baseline]
\node [anchor=base,inner sep=0pt] (X) at (0,0) {Highlighted};
\begin{pgfonlayer}{background layer}
    \draw [fill=orange, fill opacity=0.9, baseline] (X.south west) rectangle (X.north east); 
\end{pgfonlayer}
\end{tikzpicture}  Text v1.

%% But how to get this to work?
Some \MyTikzMark{MyNode}{Highlighted} Text v2.
\begin{tikzpicture}[remember picture, overlay]
\begin{pgfonlayer}{background layer}
    \draw [fill=orange, fill opacity=0.9, baseline] (MyNode.south west) rectangle (MyNode.north east); 
\end{pgfonlayer}
\end{tikzpicture}%

\end{document}

答案1

您可以先使用 tikzmark 绘制背景。您需要对分页符稍微小心一点(但无论如何您都需要这样做)。它需要两次编译。

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{tikzmark}

\newsavebox\mynode{}
\newcommand{\MyTikzMark}[2]{%
    \savebox\mynode{#1}\raisebox{-\dp\mynode}{\tikzmark{#1begin}}\usebox\mynode\raisebox{\ht\mynode}{\tikzmark{#1end}}}


\begin{document}

\iftikzmark{MyNodebegin}
 {\tikz[remember picture,overlay]\draw [fill=orange, fill opacity=0.9, baseline] (pic cs:MyNodebegin) rectangle (pic cs:MyNodeend);}
 {}%
Some \MyTikzMark{MyNode}{Highlighted} Text v2.

\end{document}

在此处输入图片描述

答案2

第一种方法

或许你可以用一个盒子来记住里面的内容,然后再放回去。

这里我比较懒,习惯spy设置记忆框。因为只有一个记忆框,所以你不能设置\tikzmark两个单词然后突出显示它们。但如果你愿意自己管理这些框,这是可能的。例如,替换

      \global\setbox\tikz@lib@spybox=\hbox{\copy\tikz@lib@spybox}%

经过

      \global\setbox\your@remembering@box=\hbox{\copy\tikz@lib@spybox}%

代码

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{spy}

\newcommand{\MyTikzMark}[2]{%
    \tikz[baseline,remember picture,spy scope]\node[anchor=base,inner sep=0pt](#1){#2};%
}%

\begin{document}

\makeatletter
\tikzset{
  spy scope/.append style={
    execute at end scope={%
      \global\setbox\tikz@lib@spybox=\hbox{\copy\tikz@lib@spybox}%
    }
  }
}

%% But how to get this to work?
Some \MyTikzMark{MyNode}{Highlighted} Text v2.
\begin{tikzpicture}[remember picture, overlay]
    \draw [fill=orange, fill opacity=0.9] (MyNode.south west) rectangle (MyNode.north east);
    \node at(MyNode.base){\copy\tikz@lib@spybox};
\end{tikzpicture}%

\end{document}

第二种方法

该方法使用 pGfpages(不是 pdfpages)。首先设置两个逻辑页面:第一个用于突出显示,第二个用于实际内容。每当您想要突出显示某些内容时,请将突出显示代码添加到第一个逻辑页面。(您可以一次突出显示多个单词。)该包将完成剩下的工作。

如果实际内容超过一页,则此方法无效。如果您使用 beamer,则此方法有效。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfpages}
\pgfpagesdeclarelayout{one page but with background}{}{%
  \pgfpagesphysicalpageoptions{         logical pages=2,%
    physical width=\paperwidth, first logical shipout=2,%
    physical height=\paperheight,last logical shipout=2,%
  }
  \pgfpageslogicalpageoptions1{center=\pgfqpoint{.5\paperwidth}{.5\paperheight}}
  \pgfpageslogicalpageoptions2{center=\pgfqpoint{.5\paperwidth}{.5\paperheight}}
}
\pgfpagesuselayout{one page but with background}%

\newcommand{\MyTikzMark}[2]{%
    \tikz[baseline,remember picture]\node[anchor=base,inner sep=0pt](#1){#2};%
}

\begin{document}
%% But how to get this to work?
Some \MyTikzMark{MyNode}{Highlighted} Text v2.

\pgfpagesshipoutlogicalpage1\vbox{
\tikz[remember picture, overlay]
    \draw[fill=orange, fill opacity=0.9](MyNode.south west)rectangle(MyNode.north east); 
}

\end{document}

第三种方法

这太疯狂了。基本上,它会将您想知道的所有内容写入辅助文件。当包含辅助文件时(在第二次编译中),它会在目标 tikzpicture 之前添加一个钩子。因此,在绘制目标 tikzpicture 之前(在第三次编译中),钩子会为您绘制背景。

再说一遍,这太疯狂了。它实际上要求目标节点是矩形。虽然这些并不难克服,但我还是会在这里留下不完美的答案。

好消息是:它不受分页的影响。

\documentclass{article}
\usepackage{tikz,lipsum}

\newcommand{\MyTikzMark}[2]{%
    \dopicturebackground%
    \tikz[baseline,remember picture]\node[anchor=base,inner sep=0pt](#1){#2};%
}

\makeatletter
\newcount\pgf@picture@serial@count@temp
% #1: associated node name
% #2: the background code you want to add
\def\addbackgroundto(#1)#2{%
    \edef\pgf@tempnodename{#1}%
    \edef\pgf@tempgfid{{\csname pgf@sh@pi@\pgf@tempnodename\endcsname}}%
    \def\southwest{\string\southwest}%
    \def\northeast{\string\northeast}%
    \edef\pgf@tempnodeinfor{{%
        \string\pgfutil@namedef{pgf@sh@ns@\pgf@tempnodename}{\csname pgf@sh@ns@\pgf@tempnodename\endcsname}%
        \string\pgfutil@namedef{pgf@sh@np@\pgf@tempnodename}{\csname pgf@sh@np@\pgf@tempnodename\endcsname}%
        \string\pgfutil@namedef{pgf@sh@nt@\pgf@tempnodename}{\csname pgf@sh@nt@\pgf@tempnodename\endcsname}%
        \string\pgfutil@namedef{pgf@sh@pi@\pgf@tempnodename}{\csname pgf@sh@pi@\pgf@tempnodename\endcsname}%
        \string\pgfutil@namedef{pgf@sh@ma@\pgf@tempnodename}{\csname pgf@sh@ma@\pgf@tempnodename\endcsname}%
    }}%
    \expandafter\pgfsys@addnodebackground\expandafter\pgf@tempgfid\pgf@tempnodeinfor{\tikz[remember picture,overlay]{#2}}%
}
% #1: pgfid of the desination
% #2: all node information
% #3: the background code you want to add
\def\pgfsys@addnodebackground#1#2#3{%
  \pgfutil@writetoaux{\string\pgfsysaddpicturebackground#1{#2\detokenize{#3}}}%
}
% #1: pgfid of the destination
% #2: all node information
%     and the background code you want to add
\def\pgfsysaddpicturebackground#1#2{%
    \expandafter\gdef\csname picturebackground#1\endcsname{#2}%
}
\def\dopicturebackground{%
    \global\pgf@picture@serial@count@temp=\pgf@picture@serial@count\relax%
    \pgf@picture@serial@count=10000\relax%
    \csname picturebackgroundpgfid\the\numexpr\pgf@picture@serial@count@temp+1\endcsname%
    \global\pgf@picture@serial@count=\pgf@picture@serial@count@temp\relax%
}

\begin{document}

%% But how to get this to work?
Some \MyTikzMark{My  Node}{Highlighted} Text v2.

\clearpage

second page

\addbackgroundto(My  Node){
    \draw[fill=orange, fill opacity=0.9](My  Node.south west)rectangle(My  Node.north east);
}
\end{document}

相关内容