First approach
Perhaps you can use a box to remember the content and put it back.
Here I am lazy and use spy
to set up the remembering box. Since there is only one remembering box, you cannot \tikzmark
two words and then highlight them. But this is possible if you are willing to manage the boxes by yourself. For example replace
\global\setbox\tikz@lib@spybox=\hbox{\copy\tikz@lib@spybox}%
by
\global\setbox\your@remembering@box=\hbox{\copy\tikz@lib@spybox}%
code
\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}
Second Approach
The approach uses pgfpages (not pdfpages). Firstly set up two logical pages: the first for highlight and the second for the actual content. Whenever you want to highlight something, add the highlighting code to the first logical page. (You can highlight multiple words at once.) The package will do the rest.
This approach does not work if the the actual content is more than one pages. It works if you are using 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}
Third approach
This is insane. Basically it writes everything you want to know to the aux file. When the aux file is included (in the second compilation), it will add a hook before the targeted tikzpicture. Therefore before the targeted tikzpicture is drawn (in the third compilation), the hook draws the background for you.
Again, this is insane. It actually requires the targeted node being rectangle. Although these are not difficult to overcome, I will leave the imperfect answer here.
The good news is: it is not affected by page-breaking.
\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}