第一種方法

第一種方法

我可以將圖層用作:

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(不是 pdf 頁面)。首先設定兩個邏輯頁面:第一個用於突出顯示,第二個用於實際內容。每當您想要突出顯示某些內容時,請將突出顯示程式碼新增至第一個邏輯頁面。 (您可以一次突出顯示多個單字。)該套件將完成其餘的工作。

如果實際內容超過一頁,則此方法不起作用。如果您使用投影儀,它就可以工作。

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

第三種方法

瘋了吧。基本上,它將您想知道的所有內容寫入 aux 檔案。當包含 aux 檔案時(在第二次編譯中),它將在目標 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}

相關內容