Debian Codegraph @

texmf-dist/source/latex/tikzmark/tikzmark.dtx

raw
1 % \iffalse meta-comment
2 %<*internal>
3 \iffalse
4 %</internal>
5 %<*readme>
6 ----------------------------------------------------------------
7 tikzmark --- remembering absolute positioning with TikZ
8 E-mail: [email protected]
9 Released under the LaTeX Project Public License v1.3c or later
10 See http://www.latex-project.org/lppl.txt
11 ----------------------------------------------------------------
12
13 The tikzmark package defines a command to "remember" a position
14 on a page for later (or earlier) use, primarily (but not
15 exclusively) with TikZ.
16 %</readme>
17 %<*internal>
18 \fi
19 \def\nameofplainTeX{plain}
20 \ifx\fmtname\nameofplainTeX\else
21 \expandafter\begingroup
22 \fi
23 %</internal>
24 %<*install>
25 \input docstrip.tex
26 \keepsilent
27 \askforoverwritefalse
28 \preamble
29 ----------------------------------------------------------------
30 tikzmark --- remembering absolute positioning with TikZ.
31 E-mail: [email protected]
32 Released under the LaTeX Project Public License v1.3c or later
33 See http://www.latex-project.org/lppl.txt
34 ----------------------------------------------------------------
35
36 \endpreamble
37 \postamble
38
39 Copyright (C) 2011-2021 by Andrew Stacey <[email protected]>
40
41 This work may be distributed and/or modified under the
42 conditions of the LaTeX Project Public License (LPPL), either
43 version 1.3c of this license or (at your option) any later
44 version. The latest version of this license is in the file:
45
46 http://www.latex-project.org/lppl.txt
47
48 This work is "maintained" (as per LPPL maintenance status) by
49 Andrew Stacey.
50
51 This work consists of the file tikzmark.dtx
52 and the derived files tikzmark.ins,
53 tikzmark.pdf,
54 tikzlibrarytikzmark.code.tex, and
55 tikzmarklibrarylistings.code.tex
56 tikzmarklibraryhighlighting.code.tex
57 tikzmarklibraryams.code.tex
58
59 \endpostamble
60 \usedir{tex/latex/tikzmark}
61 \generate{
62 \file{tikzlibrarytikzmark.code.tex}{\from{\jobname.dtx}{tikzlibrary}}
63 }
64 \generate{
65 \file{tikzmarklibrarylistings.code.tex}{\from{\jobname.dtx}{listings}}
66 }
67 \generate{
68 \file{tikzmarklibraryhighlighting.code.tex}{\from{\jobname.dtx}{highlighting}}
69 }
70 \generate{
71 \file{tikzmarklibraryams.code.tex}{\from{\jobname.dtx}{ams}}
72 }
73 %</install>
74 %<install>\endbatchfile
75 %<*internal>
76 \usedir{source/latex/tikzmark}
77 \generate{
78 \file{\jobname.ins}{\from{\jobname.dtx}{install}}
79 }
80 \nopreamble\nopostamble
81 \usedir{doc/latex/demopkg}
82 \generate{
83 \file{README.txt}{\from{\jobname.dtx}{readme}}
84 }
85 \ifx\fmtname\nameofplainTeX
86 \expandafter\endbatchfile
87 \else
88 \expandafter\endgroup
89 \fi
90 %</internal>
91 %<*driver>
92 \documentclass{ltxdoc}
93 \usepackage[T1]{fontenc}
94 \usepackage{lmodern}
95 \usepackage{amsmath}
96 %\usepackage{morefloats}
97 \usepackage{listings}
98 \usepackage{tikz}
99 \usetikzlibrary{fit,arrows.meta,tikzmark,shadows,decorations.pathreplacing,calc}
100 \usetikzmarklibrary{ams,listings,highlighting}
101
102 %\usepackage[numbered]{hypdoc}
103 \definecolor{lstbgcolor}{rgb}{0.9,0.9,0.9}
104
105 \usepackage{listings}
106 \lstloadlanguages{[LaTeX]TeX}
107 \lstset{
108 gobble=2,
109 breakatwhitespace=true,
110 breaklines=true,
111 language=[LaTeX]TeX,
112 basicstyle=\small\ttfamily,
113 keepspaces=true,
114 columns=fullflexible
115 }
116 \usepackage{fancyvrb}
117
118 \usepackage[hyperindex=false]{hyperref}
119
120 \newenvironment{example}
121 {\VerbatimEnvironment
122 \begin{VerbatimOut}[gobble=2]{example.out}}
123 {\end{VerbatimOut}
124 \begin{center}
125 % \setlength{\parindent}{0pt}
126 \fbox{\begin{minipage}{.9\linewidth}
127 \lstinputlisting{example.out}
128 \end{minipage}}
129
130 \fbox{\begin{minipage}{.9\linewidth}
131 \input{example.out}
132 \end{minipage}}
133 \end{center}
134 }
135
136 \newenvironment{justexample}
137 {\VerbatimEnvironment
138 \begin{VerbatimOut}[gobble=2]{example.out}}
139 {\end{VerbatimOut}
140 \begin{center}
141 % \setlength{\parindent}{0pt}
142 \fbox{\begin{minipage}{.9\linewidth}
143 \lstinputlisting{example.out}
144 \end{minipage}}
145 \end{center}
146 }
147
148 \newcommand\balloon[4]{%
149 \pgfmathtruncatemacro\firstline{%
150 #3-1
151 }%
152 \iftikzmark{line-#2-\firstline-start}{%
153 \iftikzmark{line-#2-#3-first}{%
154 \xdef\blines{({pic cs:line-#2-\firstline-start} -| {pic cs:line-#2-#3-first})}%
155 }{%
156 \iftikzmark{line-#2-#3-start}{%
157 \xdef\blines{({pic cs:line-#2-\firstline-start} -| {pic cs:line-#2-#3-start})}%
158 }{%
159 \xdef\blines{(pic cs:line-#2-\firstline-start)}%
160 }%
161 }%
162 }{%
163 \xdef\blines{}%
164 }%
165 \foreach \k in {#3,...,#4} {%
166 \iftikzmark{line-#2-\k-first}{%
167 \xdef\blines{\blines (pic cs:line-#2-\k-first) }
168 }{}
169 \iftikzmark{line-#2-\k-end}{%
170 \xdef\blines{\blines (pic cs:line-#2-\k-end) }
171 }{}
172 }%
173 \ifx\blines\empty
174 \else
175 \edef\temp{\noexpand\tikz[remember picture,overlay]{\noexpand\node[fit={\blines},balloon] (#1) {}}}%
176 \temp
177 \fi
178 }
179 \tikzset{
180 line/.style={
181 draw,
182 rounded corners=3pt,
183 -latex
184 },
185 balloon/.style={
186 draw,
187 fill=blue!20,
188 opacity=0.4,
189 inner sep=4pt,
190 rounded corners=2pt
191 },
192 comment/.style={
193 draw,
194 fill=blue!70,
195 text=white,
196 text width=3cm,
197 minimum height=1cm,
198 rounded corners,
199 drop shadow,
200 align=left,
201 font=\scriptsize
202 },
203 }
204
205 \DisableCrossrefs
206 % \CodelineIndex
207 \RecordChanges
208 \begin{document}
209 \DocInput{\jobname.dtx}
210 \end{document}
211 %</driver>
212 % \fi
213 %
214 % \CheckSum{1891}
215 %
216 % \CharacterTable
217 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
218 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
219 % Digits \0\1\2\3\4\5\6\7\8\9
220 % Exclamation \! Double quote \" Hash (number) \#
221 % Dollar \$ Percent \% Ampersand \&
222 % Acute accent \' Left paren \( Right paren \)
223 % Asterisk \* Plus \+ Comma \,
224 % Minus \- Point \. Solidus \/
225 % Colon \: Semicolon \; Less than \<
226 % Equals \= Greater than \> Question mark \?
227 % Commercial at \@ Left bracket \[ Backslash \\
228 % Right bracket \] Circumflex \^ Underscore \_
229 % Grave accent \` Left brace \{ Vertical bar \|
230 % Right brace \} Tilde \~}
231 %
232 %
233 % \changes{1.0}{2012/11/08}{Converted to DTX file}
234 % \changes{1.1}{2013/04/22}{Fix bug relating to active semi-colon}
235 % \changes{1.2}{2016/04/07}{Tikzmark works inside tikzpicture, added easy suffix and prefix for tikzmarks}
236 % \changes{1.3}{2017/06/01}{Tikzmark no longer uses semi-colon (too many issues with catcodes) and fix for beamer-awareness}
237 % \changes{1.4}{2017/10/29}{Bug fixes: spurious characters in aux file, tikzmark prefix/suffix ignored in iftikzmark test}
238 % \changes{1.5}{2018/09/09}{Reintroduced the original command (now as tikzmarknode) with a mathchoice hack for different math modes}
239 % \changes{1.6}{2018/10/18}{Added the ability to save node information between runs and between TeX documents}
240 % \changes{1.7}{2019/05/07}{Added conditions to test if a tikzmark is on a particular page}
241 % \changes{1.8}{2019/10/04}{Fixed some bugs with subnode and tikzmarknode inside maths}
242 % \changes{1.10}{2021/02/16}{Tikzmarknode is now prefix and suffix aware, and added a test to see if a picture id has been saved to the aux file for times when pictures are thrown away, eg in AMS's text command}
243 % \changes{1.11}{2021/08/16}{Tikzmark inside a tikzpicture was not applying any surrounding transformation before saving the coordinates. Code for adjusting a pic's location based on an internal coordinate.}
244 % \changes{1.12}{2021/08/24}{Experimental library for creating pseudo-nodes around AMSMath equation alignment boxes.}
245 % \changes{1.14}{2022/01/28}{Reimplementing the highlighting code using LaTeX3 hooks.}
246 % \changes{1.15}{2022/08/24}{Improvements to highlighting code}
247 %
248 % \DoNotIndex{\newcommand,\newenvironment}
249 % \pdfstringdefDisableCommands{%
250 % \def\\{}%
251 % \def\url#1{<#1>}%
252 % }
253 %
254 % \GetFileInfo{tikzlibrarytikzmark.code.tex}
255 % \providecommand*{\url}{\texttt}
256 % \title{The \textsf{tikzmark} package}
257 % \author{Andrew Stacey \\ \url{[email protected]}}
258 % \date{\fileversion~from \filedate}
259 %
260 %
261 % \maketitle
262 %
263 % \section{Introduction}
264 %
265 % The \Verb+\tikzmark+ macro burst onto the scene in a blaze of glory on \href{http://tex.stackexchange.com}{TeX-SX}.
266 % Since then, it has proved embarrassingly (to its original author) popular.
267 % The idea behind it is extremely simple: that the machinery underneath TikZ provides a way to ``mark'' a point on a page for further use.
268 % This functionality is already provided by several other packages.
269 % The point of this one is that as TikZ can provide this feature, if already loading TikZ then it makes sense to use the TikZ version than another version.
270 % Moreover, if the goal is to use these marks with some TikZ code then this version is already set up for that purpose (not that it would be exactly difficult to add this to any of the other implementations).
271 %
272 % \section{Use}
273 %
274 % Using the \Verb+\tikzmark+ is extremely simple.
275 % You need to load the \Verb+tikz+ package and then load \Verb+tikzmark+ as a \Verb+tikzlibrary+.
276 % Thus in your preamble you should have something like:
277 %
278 % \begin{lstlisting}
279 % \usepackage{tikz}
280 % \usetikzlibrary{tikzmark}
281 % \end{lstlisting}
282 %
283 % In your document, you can now type \Verb+\tikzmark{<name>}+ at a point that you want to remember.
284 % This will save a mark with name \Verb+<name>+ for use later (or earlier).
285 % To use it in a \Verb+\tikz+ or \Verb+tikzpicture+, simply use the \Verb+pic+ coordinate system:
286 %
287 % \begin{lstlisting}
288 % \tikz[remember picture] \draw[overlay] (0,0) -- (pic cs:<name>);
289 % \end{lstlisting}
290 %
291 % There are two important points to note:
292 %
293 % \begin{enumerate}
294 % \item The enveloping \Verb+\tikz+ or \Verb+tikzpicture+ must have the key \Verb+remember picture+ set.
295 %
296 % This is because of how TikZ coordinates work.
297 % The coordinates inside a TikZ picture are relative to its origin, so that origin can move around on the page and not affect the internals of the picture.
298 % To use a point outside the picture, therefore, the current picture not only has to know where that point is on the page it also has to know where it itself is on the page.
299 % Hence the \Verb+remember picture+ key must be set.
300 % \item The drawing command must have the \Verb+overlay+ key set (or be in a scope or picture where it is set).
301 %
302 % This is to keep the bounding box of the current picture under control.
303 % Otherwise, it would grow to encompass the remembered point as well as the current picture.
304 % (This isn't necessary if the remembered point is inside the current picture.)
305 % \end{enumerate}
306 %
307 %
308 %
309 % \section{History}
310 %
311 % I wrote the original \Verb+\tikzmark+ macro in 2009 for use in lecture slides prepared with the \Verb+beamer+ package.
312 % Its original definition was:
313 %
314 % \begin{lstlisting}
315 % \newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
316 % \end{lstlisting}
317 %
318 % Its first use was in the (inelegant) code:
319 %
320 % \begin{lstlisting}
321 % \begin{frame}
322 % \frametitle{Structure of Continuous Functions}
323 %
324 % \begin{tikzpicture}[overlay, remember picture]
325 % \useasboundingbox (0,0);
326 % \draw<2-|trans: 0|handout: 0>[red,->] (bsp) .. controls +(-1,-1) and ($(cnvs.north)+(1,1)$) .. ($(cnvs.north)+(0,1)$) .. controls ($(cnvs.north)+(-1,1)$) and +(-1,0) .. (cnvs.north);
327 % \draw<3-|trans: 0|handout: 0>[green!50!black,->] (cplt) .. controls +(-1,-1) and +(-1,0) .. (mcplt.north);
328 % \draw<4-|trans: 0|handout: 0>[blue,->] (norm) .. controls +(-1,-.5) and ($(nvs.north)+(0,1.5)$) .. ($(nvs.north)+(0,1.5)$) .. controls ($(nvs.north)+(-1.5,1.5)$) and +(-1.5,0) .. (nvs.north);
329 % \draw<5-|trans: 0|handout: 0>[purple,->] (vector) .. controls +(-1,-1) and ($(vsp.north)+(2,2)$) .. ($(vsp.north)+(0,2)$) .. controls ($(vsp.north)+(-2,2)$) and +(-2,0) .. (vsp.north);
330 % \end{tikzpicture}
331 %
332 % \begin{theorem}
333 % \centering
334 % \(\big(C([0,1],\R),d_\infty\big)\) \\
335 % is a \\
336 % \alert{Banach\tikzmark{bsp} space}
337 % \end{theorem}
338 %
339 % \pause
340 % \bigskip
341 %
342 % \begin{itemize}
343 % \item[\tikzmark{cnvs}] {\color<.(2)->{green!50!black}Comp\tikzmark{cplt}lete} {\color<.(3)->{blue}nor\tikzmark{norm}med} {\color<.(4)->{purple}vector\tikzmark{vector} space}.
344 %
345 % \bigskip
346 % \bigskip
347 % \pause
348 %
349 % \begin{itemize}[<+->]
350 % \item[\tikzmark{mcplt}] {\color{green!50!black}Cauchy sequences converge.}
351 % \medskip
352 % \item[\tikzmark{nvs}] {\color{blue}Metric from a norm.}
353 % \medskip
354 % \item[\tikzmark{vsp}] {\color{purple}Functions behave like vectors.}
355 % \end{itemize}
356 % \end{itemize}
357 %
358 % \end{frame}
359 % \end{lstlisting}
360 %
361 % This produced, on the final slide, Figure~\ref{fig:tikzmarkex}.
362 %
363 % \begin{figure}
364 % \centering
365 % \IfFileExists{tikzmarkExample.pdf}{\includegraphics{tikzmarkExample}}{}
366 % \caption{First use of tikzmark}
367 % \label{fig:tikzmarkex}
368 % \end{figure}
369 %
370 % Its first appearance on \href{http://tex.stackexchange.com}{TeX-SX} was in an \href{http://tex.stackexchange.com/a/316/86}{answer} to a question about how to put overlapping braces on a mathematical text.
371 % This was in July 2010.
372 % The opening statement of the answer was not overly encouraging: ``This may not be the best solution\dots''.
373 % And for a macro that would go on to become quite ubiquitous, its initial appearance only garnered it 2 votes.
374 %
375 % However, it started out in life as a useful macro for me and as such I found more uses for it in my own code and thus more opportunity for using it to answer questions on TeX-SX.
376 % The one that seems to have been where it got noticed came in \href{http://tex.stackexchange.com/a/1570/86}{August 2010}, again about putting braces in text but in a more complicated fashion.
377 % From this answer, it got picked up, picked over, and picked apart.
378 % A common use was in highlighting or adding marks to text.
379 %
380 % Gradually, as it got used, it developed.
381 % A major revision dates from an answer given in \href{http://tex.stackexchange.com/a/50054/86}{March 2012} where the question was actually about \Verb+\tikzmark+.
382 % This version added two important features: a TikZ coordinate system for referencing saved marks directly and the ability to refer to marks earlier in the document than they are defined (the mechanism for remembering points uses the \Verb+aux+ file anyway so this was more about exposing the information earlier than anything complicated).
383 % Then in October 2012 there was a \href{http://tex.stackexchange.com/q/79121/86}{question} where it would have been useful to remember which page the mark was on and a \href{http://tex.stackexchange.com/q/79762/86}{question} where for some reason using the \Verb+\tikz+ macro didn't work so the \Verb+\pgfmark+ macro was introduced.
384 %
385 % By this point, the \Verb+\tikzmark+ command had morphed considerably from its original definition.
386 % Experience has shown that on the TeX-SX site it has continued to be used in its original form as well as its current form.
387 % I've therefore taken the decision to reintroduce a form of the original command, now called \Verb+\tikzmarknode+.
388 % It goes beyond the original version in that it uses some \Verb+\mathchoice+ trickery (inspired by \href{https://tex.stackexchange.com/a/122419/86}{this answer} from Heiko Oberdiek) to hopefully correctly choose the correct math style.
389 %
390 % The original reason for not using nodes inside \Verb+\tikzmark+ was to be able to use the information from a \Verb+\tikzmark+ before the point where it was defined (via information saved into the \Verb+aux+ file).
391 % Thanks to a \href{https://tex.stackexchange.com/a/415862/86}{question on TeX-SX} about saving node information, I've developed code that solves that issue with nodes.
392 % As it fits in the general concept of this package, I've added that code to the \Verb+\tikzmark+ package.
393 %
394 % \section{Usage}
395 %
396 % This package defines the following commands and usable stuff.
397 %
398 % \subsection{Core Commands}
399 %
400 % \begin{enumerate}
401 % \item \Verb+\tikzmark+\oarg{drawing command}\marg{name}
402 %
403 % The mandatory argument is the name of the mark to be used to refer back to this point later.
404 %
405 % The \Verb+\tikzmark+ command can take an optional parameter which is some drawing command that can be put in a \Verb+\tikz ... ;+ command.
406 % This drawing command can be used to place a node or something similar at the marked point, or to set some \Verb+\tikzset+ keys.
407 % Sometimes this can be useful.
408 % Note, though, that if this is used to define an offset coordinate then this will only be available in the document \emph{after} the \Verb+\tikzmark+ command, even on later runs.
409 %
410 % If the \Verb+beamer+ class is loaded then this command is made overlay-aware.
411 %
412 % \item \Verb+\tikzmark+\marg{name}\marg{coordinate}
413 %
414 % v1.2 of the \Verb+tikzmark+ package introduced a new variant of \Verb+\tikzmark+ which works inside a \Verb+tikzpicture+.
415 % One feature of \Verb+\tikzmark+ which isn't part of TikZ's normal coordinate remembering system is the ability to use a \Verb+\tikzmark+ coordinate before it is defined (due to the use of the \Verb+aux+ file).
416 % This is potentially useful to have inside a \Verb+tikzpicture+ and so it is now possible to use \Verb+\tikzmark+ inside a \Verb+tikzpicture+.
417 % The syntax is slightly different as we need to specify the coordinates of a point to remember.
418 %
419 % This was inspired by the question \href{http://tex.stackexchange.com/q/295903/86}{Refer to a node in tikz that will be defined ``in the future'' (two passes)?} on TeX-SX.
420 %
421 % \item \Verb+\pgfmark+\marg{name}
422 %
423 % This is a more basic form of the \Verb+\tikzmark+ which doesn't use any of the \Verb+\tikz+ overhead.
424 % One advantage of this command is that it doesn't create an \Verb+hbox+.
425 % It does, however, insert a \Verb+whatsit+ into the stream so it will, for example, stop two vertical spaces either side of it being merged.
426 % This can't be avoided.
427 %
428 % If the \Verb+beamer+ class is loaded then this command is made overlay-aware.
429 %
430 % \item \Verb+\iftikzmark+\marg{name}\marg{true code}\marg{false code}
431 %
432 % This is a conditional to test if a particular mark is available.
433 % It executes \Verb+true code+ if it is and \Verb+false code+ if not.
434 %
435 % \item \Verb+\iftikzmarkexists+\marg{name}
436 %
437 % This is a conditional to test if a particular mark is available which works with the lower level \TeX\ \Verb+\else+ and \Verb+\fi+.
438 %
439 % \item \Verb+\iftikzmarkoncurrentpage+\marg{name}
440 %
441 % This is a conditional to test if a particular mark is on the current page; it works with the lower level \TeX\ \Verb+\else+ and \Verb+\fi+.
442 %
443 % \item \Verb+\iftikzmarkonpage+\marg{name}\marg{page}
444 %
445 % This is a conditional to test if a particular mark is on a given page; it works with the lower level \TeX\ \Verb+\else+ and \Verb+\fi+.
446 %
447 % \item \Verb+\tikzmarknode+\oarg{options}\marg{name}\marg{contents}
448 %
449 % This is a reincarnation of the original \Verb+\tikzmark+ command which places its contents inside a \Verb+\tikz+ node. It also defines a \Verb+tikzmark+ with the same name. Using a sneaky trick with \Verb+\mathchoice+, it works inside a math environment. The spacing either side might not be quite right as although it detects the math style it doesn't got beyond that. The \Verb+options+ are passed to the node.
450 %
451 % Two styles are attempted, one on the surrounding picture and one on the node, which are:
452 %
453 % \begin{itemize}
454 % \item \Verb+every tikzmarknode picture+
455 % \item \Verb+every tikzmarknode+
456 % \end{itemize}
457 %
458 % To refer to the \emph{node}, use usual TikZ coordinates.
459 % To refer to the underlying \emph{tikzmark}, use the special tikzmark coordinates (see below).
460 %
461 % \item \Verb+(pic cs:<name>)+ or \Verb+(pic cs:<name>,<coordinate>)+
462 %
463 % This is the method for referring to a position remembered by \Verb+\tikzmark+ (or \Verb+\pgfmark+) as a coordinate in a \Verb+tikzpicture+ environment (or \Verb+\tikz+ command).
464 % If the extra \Verb+coordinate+ is specified then this is used in case the mark \Verb+name+ has not yet been defined (this can be useful for defining code that does something sensible on the first run).
465 %
466 % \item \Verb+/tikz/save picture id=<name>+
467 %
468 % This is the TikZ key that is used by \Verb+\tikzmark+ to actually save the connection between the name and the picture coordinate.
469 % It can be used on an arbitrary picture to save its origin as a tikzmark.
470 %
471 % \item \Verb+/tikz/check picture id+
472 %
473 % There are circumstances where, behind the scenes, a tikzpicture is actually placed in a box and processed several times (often this involves \Verb+\mathchoice+).
474 % In such a situation, when defining nodes then the last one ``wins'' in that each node remembers the id of the last processed picture.
475 % However, only the one that is actually used has its location remembered on the page (since the others don't have a position).
476 % This can lead to the situation whereby a node becomes disassociated from its picture and so using it for later reference fails.
477 % This key tries to get around that situation by checking the \Verb+aux+ file to see if the current picture was actually typeset last time (by checking for the presence of the remembered location) and if it finds that it wasn't, it quietly appends the string \Verb+discard-+ to each node name.
478 % The idea being that the version of the picture that is actually typeset will not have this happen and so its nodes ``survive''.
479 %
480 % \item \Verb+/tikz/maybe define node=#1+
481 %
482 % The previous key can lead to undefined nodes on the first time that the picture is processed.
483 % Using this key will ensure that the specified node is aliased to its \Verb+discard-+ version providing it doesn't already exist.
484 % This is purely to get rid of pointless error messages, and also should only be used in conjunction with \Verb+check picture id+.
485 %
486 % Note that due to the order in which code gets executed, \Verb+check picture id+ should be before any \Verb+maybe define node+ keys.
487 %
488 % \item \Verb+/tikz/if picture id=#1#2#3+
489 %
490 % This is a key equivalent of the \Verb+\iftikzmark+ command.
491 %
492 % \item \Verb+/tikz/if tikzmark on current page=#1#2#3+
493 %
494 % This is a key equivalent of the \Verb+\iftikzmarkoncurrentpage+ command.
495 % If true, the keys in \Verb+#2+ are executed, otherwise the keys in \Verb+#3+.
496 %
497 % \item \Verb+/tikz/if tikzmark on page=#1#2#3#4+
498 %
499 % This is a key equivalent of the \Verb+\iftikzmarkonpage+ command.
500 %
501 % \item \Verb+/tikz/next page+, \Verb+/tikz/next page vector+
502 %
503 % It is possible to refer to a mark on a different page to the current page.
504 % When this is done, the mark is offset by a vector stored in the key \Verb+/tikz/next page vector+.
505 % The key \Verb+/tikz/next page+ can be used to set this to certain standard vectors by specifying where the ``next page'' is considered as lying corresponding to the current page.
506 % Possible values are (by default) \Verb+above+, \Verb+below+, \Verb+left+, \Verb+right+, and \Verb+ignore+.
507 % (The last one sets the vector to the zero vector.)
508 %
509 % Previous versions of \Verb+tikzmark+ tried to make this work correctly with the mark being on, say, \(5\) pages further on but this got too fiddly so this version just pretends that the mark is on the next or previous page and points to it as appropriate.
510 %
511 % \item \Verb+/tikz/tikzmark prefix=<prefix>+ and \Verb+/tikz/tikzmark suffix=<suffix>+
512 %
513 % These keys allow for the automatic addition of a prefix and/or suffix to each \Verb+\tikzmark+ name.
514 % The prefix and suffix are added both at time of definition and of use, so providing one is in the same scope there is no difference in at the user level when using prefixes and suffixes.
515 % What it can be useful for is to make the \Verb+\tikzmark+ names unique.
516 % In particular, if the \Verb+beamer+ class is loaded then an automatic suffix is added corresponding to the overlay.
517 % This means that if a slide consists of several overlays with \Verb+\tikzmark+s on them, and the positions of the \Verb+\tikzmark+s move then the resulting pictures should look right.
518 % Without the automatic suffix, only the final positions of the marks would be used throughout.
519 %
520 % This was inspired by the question \href{http://tex.stackexchange.com/q/302517/86}{using tikzmark subnode with overlays beamer} on TeX-SX.
521 %
522 % \end{enumerate}
523 %
524 % \subsection{Pic and Scope Positioning}
525 %
526 % \Verb+scope anchor+, \Verb+pic anchor+, and \Verb+surround pic+.
527 %
528 % These keys can be used to enable advanced positioning of \Verb+scope+s and \Verb+pic+s.
529 % The standard positioning of a \Verb+pic+ places its internal origin at the location specified on the \Verb!\pic! command.
530 % This is more limited than what is available to a \Verb+node+ whereby any of the defined anchors can be placed at the given position.
531 % The key \Verb+pic anchor+ allows a little more flexibility to \Verb+pic+ positioning by allowing a \Verb+pic anchor+ to be defined and used as the point to place at the given position.
532 %
533 % When invoking the \Verb!pic! the key \Verb!pic anchor={coordinate}! can be used to specify a point inside the \Verb!pic! to use as the anchor.
534 % This point is evaluated inside the \Verb!pic! so if using a node then the node name should be specified as if inside the \Verb!pic!.
535 %
536 % The node positioning syntax, things like \Verb!below! and \Verb!below=5pt of!, sets the anchor of the following node.
537 % Using \Verb!pic anchor! without a coordinate uses this anchor on the bounding box of the pic when positioning the pic.
538 %
539 % Internally, this works by adjusting the location of the \Verb!pic!'s surrounding scope.
540 % So the code can equally be used on \Verb!scope!s.
541 % For a \Verb!scope!, use the \Verb!scope anchor! version on the scope directly.
542 % The keys \Verb!name! and \Verb!anchor! can be used on the scope as if on a \Verb!node! with the same effect on the positioning.
543 %
544 % The key \Verb!surround pic! saves the bounding box of the pic as if it were the boundary of a rectangular node, using the name of the pic as the name of the node.
545 %
546 % This was inspired by the questions \href{https://tex.stackexchange.com/q/185279/86}{Anchoring TiKZ pics} and \href{https://tex.stackexchange.com/q/567245/86}{Reposition Tikz Scope After Size Known}.
547 %
548 % \subsection{Subnodes}
549 %
550 % \Verb+\subnode[options]{name}{content}+
551 %
552 % This produces a pseudo-node named \Verb+name+ around the \Verb+content+.
553 % The design purpose of this is to create a ``subnode'' inside a TikZ node.
554 % As far as TikZ is concerned, the contents of a node is just a box.
555 % It therefore does not know anything about it beyond its external size and so cannot easily determine the coordinates of pieces inside.
556 % The \Verb+\subnode+ command boxes its contents and saves the position of that box and its dimensions.
557 % This information is stored in the same way that PGF stores the necessary information about a node.
558 % It is therefore possible to use ordinary node syntax (within a \Verb+tikzpicture+) to access this information.
559 % Thus after \Verb+\node {a \subnode{a}{sub} node};+ it is possible to use \Verb+a+ as a node.
560 % The \Verb+options+ are passed to the node construction mechanism, but note that the only sensible options are those that affect the size and shape of the node: drawing options are ignored (except in so far as they affect the size -- as an example, \Verb+line width+ affects the node size).
561 %
562 % There are two important points to make about this.
563 % The first is that, as with all the \Verb+tikzmark+ macros, the information is always one compilation old.
564 % The second is that the pseudo-node is purely about coordinates: the path information is not used and the contents are not moved.
565 % This is partly for reasons of implementation: the pseudo-node is constructed when TikZ is not in ``picture mode''.
566 % But also interleaving the background path of the pseudo-node and any containing node would be problematic and so is best left to the user.
567 %
568 % The simplest way to turn a pseudo-node into a more normal node is to use the \Verb+fit+ library.
569 % Using the above example, \Verb+\node[fit=(a),draw,inner sep=0pt] {};+ would draw a rectangle around the word \Verb+sub+ of exactly the same size as would appear had a normal node been created.
570 %
571 % Using a sneaky trick with \Verb+\mathchoice+, \Verb+subnode+ works inside a math environment.
572 % The spacing either side might not be quite right as although it detects the math style it doesn't got beyond that.
573 %
574 % Note that because of the way that this works, the outer \Verb!tikzpicture! must have the \Verb!remember picture! option set.
575 %
576 % \subsection{Node saving}
577 %
578 % The node saving system takes the information stored about a node and saves it for later use.
579 % That later use can be in the same document, in which case it should be saved just to the memory of the current TeX process, or it can be used earlier in the same document or another document altogether (in particular, if the nodes are defined in a \Verb+tikzpicture+ that has been externalised, this can be used to import the node information into the main file) in which cases the node data is saved to a file.
580 %
581 % When working with files, nodes are saved and restored in bulk.
582 % When working in memory, nodes are saved and restored in named lists.
583 % Nodes are not actually saved until the end of the tikzpicture in which they are defined, meaning that if saving to memory then all the nodes in a tikzpicture will belong to the same list.
584 %
585 % The keys for working with saving and restoring nodes are as follows.
586 %
587 % \begin{itemize}
588 % \item \Verb+save node+, \Verb+save node=<name>+
589 %
590 % This is the key that indicates a node to be saved.
591 % The version with no argument is to be used directly in the keys for a node and it saves that node.
592 % With an argument then it saves a node that has been declared somewhere in the current tikz picture (it may not always be convenient to issue the \Verb+save node+ key directly on the node itself).
593 % Since the list is saved up to the end of the picture, this can be invoked before the node is defined.
594 %
595 % \item \Verb+\SaveNode[group name]{name}+
596 %
597 % This command is for outside a tikzpicture and saves the named node directly.
598 % The optional argument is a group name for saving to a group.
599 % If this is not specified then the node is saved to a file.
600 %
601 % \item \Verb+set node group=<name>+
602 %
603 % Nodes are grouped together into a list that can be saved either to a file or for use later on in the document.
604 % This sets the name for the current group.
605 %
606 % \item \Verb+restore nodes from list=<name>+
607 %
608 % This restores the node information from the named list to the current \Verb+tikzpicture+.
609 % This is required both for when the node information comes from a file or from earlier in the same document.
610 %
611 % \item \Verb+save nodes to file+
612 %
613 % This is a \Verb+true/false+ key which determines whether to save the node information to a file.
614 %
615 % \item \Verb+set saved nodes file name=<name>+
616 %
617 % This sets the file name for the saved nodes (the extension will be \Verb+.nodes+.
618 % The default is to use the current \TeX\ filename.
619 % This is set globally, and once the file is opened then changing the name will have no effect.
620 % (The file is not opened until it is actually needed to avoid creating empty files unnecessarily.)
621 %
622 % \item \Verb+restore nodes from file=<name>+
623 %
624 % This loads the node information from the file into the current document.
625 %
626 % The \Verb+<name>+ can have the syntax \Verb+[options]{name}+, where \Verb+options+ can be used to influence how the nodes are restored.
627 % The key \Verb+transform saved nodes+ (see below) can be given here.
628 % Another useful key is the \Verb+name prefix+ key which is applied to all restored nodes.
629 %
630 % \item \Verb+transform saved nodes+
631 %
632 % A particular use-case for restoring saved nodes is to safely include one \Verb+tikzpicture+ inside another by creating an image out of the inner picture and including it back in as a picture inside a node.
633 % In that situation, restoring the nodes from the inner picture can make it possible to refer to coordinates from the inner picture to the outer one.
634 % If there is a transformation in place on the containing node, this key applies that transformation to all the nodes in the inner picture.
635 %
636 % \end{itemize}
637 %
638 % \section{Examples}
639 %
640 % The \Verb+\tikzmark+ command has been used in numerous answers on \href{http://tex.stackexchange.com}{TeX-SX}.
641 %
642 % \subsection{Basic Examples}
643 %
644 % A simple example of the \Verb+\tikzmark+ macro is the following.
645 %
646 % \begin{example}
647 % \tikzset{tikzmark prefix=ex1-}
648 % \[
649 % \tikzmark{a} e^{i \pi/2} = i
650 % \]
651 %
652 % This\tikz[remember picture,overlay,baseline=0pt] \draw[->] (0,1em) to[bend left] ([shift={(-1ex,1ex)}]pic cs:a); is an important equation.
653 % \end{example}
654 %
655 % \begin{example}
656 % \tikzset{tikzmark prefix=ex2-}
657 % \begin{itemize}
658 % \item A first item,\tikzmark{b}
659 % \item A second item,\tikzmark{c}
660 % \item A third item.\tikzmark{d}
661 % \end{itemize}
662 % \begin{tikzpicture}[remember picture,overlay]
663 % \draw[decorate,decoration={brace}] ({pic cs:c} |- {pic cs:b}) +(0,1em) -- node[right,inner sep=1em] {some items} ({pic cs:c} |- {pic cs:d});
664 % \end{tikzpicture}
665 % \end{example}
666 %
667 % \begin{example}
668 % \tikzset{tikzmark prefix=ex3-}
669 % \begin{tikzpicture}[remember picture]
670 % \node (a) at (0,0) {This has a \subnode{sub}{subnode} in it};
671 % \draw[->] (0,-1) to[bend right] (sub);
672 % \end{tikzpicture}
673 % \end{example}
674 %
675 % An example using \Verb+\tikzmark+ inside a \Verb+tikzpicture+
676 %
677 % \begin{example}
678 % \tikzset{tikzmark prefix=ex4-}
679 % \begin{tikzpicture}[remember picture,overlay]
680 % \draw[->,line width=1mm,cyan] (pic cs:a) to[bend left] (pic cs:b);
681 % \end{tikzpicture}
682 %
683 % By placing the \tikzmark{a}code before the marks, the arrow goes under the subsequent text and picture.
684 %
685 % \begin{tikzpicture}
686 % \filldraw[fill=gray] (0,0) circle[radius=1cm];
687 % \tikzmark{b}{(-1,-1)}
688 % \end{tikzpicture}
689 % \end{example}
690 %
691 % The \Verb+\tikmarknode+ puts a node around some text, which can be referred to later, and adds a \Verb+\tikzmark+ at its origin.
692 %
693 % \begin{example}
694 % \tikzset{tikzmark prefix=ex5-}
695 % Putting a node around \tikzmarknode{txt}{some text} means we can connect text together, including in maths:
696 % \[
697 % \tikzmarknode{a}{\sum_{k=1}^n} k^{\tikzmarknode{b}{2}}
698 % \]
699 %
700 % \begin{tikzpicture}[remember picture,overlay]
701 % \draw[->] (txt) -- (a);
702 % \draw[->] (a.south) to[out=-90,in=-45] (b.south east);
703 % \end{tikzpicture}
704 % \end{example}
705 %
706 % The syntax for saving node data is illustrated by the following example.
707 %
708 % File \Verb+firstpicture.tex+:
709 %
710 % \begin{justexample}
711 % \documentclass[tikz,border=10pt]{standalone}
712 % \usetikzlibrary{tikzmark,shapes.geometric}
713 % \begin{document}
714 % \begin{tikzpicture}[save nodes to file]
715 % \node[draw,rotate=-30,save node](1) at (-2,0) {1};
716 % \draw[->] (0,0) -- (1);
717 % \node[draw,ellipse,save node] (c) at (current bounding box.center) {};
718 % \end{tikzpicture}
719 % \end{document}
720 % \end{justexample}
721 %
722 % File \Verb+secondpicture.tex+:
723 %
724 % \begin{justexample}
725 % \documentclass[tikz,border=10pt]{standalone}
726 % \usetikzlibrary{tikzmark,shapes.geometric}
727 % \begin{document}
728 % \begin{tikzpicture}[save nodes to file]
729 % \node[draw,rotate=-70,save node] (2) at (2,0) {2};
730 % \draw[->] (0,0) -- (2);
731 % \node[draw,ellipse,save node] (c) at (current bounding box.center) {};
732 % \end{tikzpicture}
733 % \end{document}
734 % \end{justexample}
735 %
736 % Main file:
737 %
738 % \begin{justexample}
739 % \documentclass{article}
740 % \usepackage{tikz}
741 % \usetikzlibrary{tikzmark}
742 %
743 % \begin{document}
744 % \begin{tikzpicture}
745 %
746 % \node[draw,
747 % rotate=30,
748 % restore nodes from file={[transform saved nodes,name prefix=pic-1-]{firstpicture}}
749 % ] (a-1) at (-2,-3) {\includegraphics{firstpicture.pdf}};
750 %
751 % \node[draw,
752 % rotate=70,
753 % restore nodes from file={[transform saved nodes,name prefix=pic-2-]{secondpicture}}
754 % ] (a-2) at (+2,+2) {\includegraphics{secondpicture.pdf}};
755 %
756 % \draw[red] (pic-1-1.north west) -- (pic-1-1.north east) -- (pic-1-1.south east) -- (pic-1-1.south west) -- cycle;
757 % \draw[red] (pic-2-2.north west) -- (pic-2-2.north east) -- (pic-2-2.south east) -- (pic-2-2.south west) -- cycle;
758 %
759 % \node[red] at (pic-1-1) {1};
760 % \node[red] at (pic-2-2) {2};
761 %
762 % \draw (a-1) circle[radius=5pt];
763 % \draw (a-2) circle[radius=5pt];
764 %
765 % \draw (pic-1-1) -- (pic-2-2);
766 % \end{tikzpicture}
767 % \end{document}
768 % \end{justexample}
769 %
770 % This produces:
771 %
772 % \begin{center}
773 % \fbox{\IfFileExists{tikzrefextnodes.pdf}{\includegraphics{tikzrefextnodes}}{}}
774 % \end{center}
775 %
776 % \section{Additional Libraries}
777 %
778 % Some of the more ambitious uses of \Verb!\tikzmark! involve a fair bit of extra code and so are worth gathering in to extra libraries of their own.
779 % These can be loaded via \Verb+\usetikzmarklibrary+.
780 %
781 % At present, there are three libraries: one for code listings which works with the \Verb+listings+ package, one for AMSMath equations, and one for highlighting.
782 %
783 % \subsection{Code Listings}
784 %
785 % If the \Verb+listings+ package has been loaded then issuing
786 %
787 % \Verb+\usetikzmarklibrary{listings}+
788 %
789 % \noindent will load in some code to add marks to \Verb+lstlisting+ environments.
790 % This code places a mark at three places on a line of code in a \Verb+listings+ environment.
791 % The marks are placed at the start of the line, the first non-whitespace character, and the end of the line (if the line is blank the latter two are not placed).
792 % (This has not been extensively tested, it works by adding code to various ``hooks'' that are made available by the \Verb+listings+ package; it is quite possible that the hooks chosen are both wrong and insufficient to cover all desired cases.)
793 %
794 %
795 % These are inspired by questions such as \href{http://tex.stackexchange.com/q/79762/86}{Marking lines in listings} and \href{http://tex.stackexchange.com/q/86309/86}{Macros for code annotations}.
796 %
797 % In more detail, the \Verb+listings+ library places lots of marks around the code.
798 % The marks are:
799 %
800 % \begin{itemize}
801 % \item \Verb+line-<name>-<number>-start+ at the start of each line.
802 % \item \Verb+line-<name>-<number>-end+ at the end of each line.
803 % \item \Verb+line-<name>-<number>-first+ at the first non-space character of the line (assuming it exists).
804 % \end{itemize}
805 %
806 % The line numbers \emph{should} match up with the line numbers in the code in that any initial offset is also applied.
807 %
808 % Not every mark is available on every line.
809 % If a line is blank, in particular, it will only have a \Verb+start+ mark.
810 % The following example shows this, where the red dots are the \Verb+start+, the blue are \Verb+end+, and the green are \Verb+first+.
811 %
812 % \begin{example}
813 % \tikzset{tikzmark prefix=ex6-}
814 % \begin{tikzpicture}[remember picture]
815 % \foreach \k in {0,...,7} {
816 % \iftikzmark{line-code-\k-start}{\fill[red,overlay] (pic cs:line-code-\k-start) circle[radius=4pt];}{\message{No start for \k}}
817 % \iftikzmark{line-code-\k-end}{\fill[blue,overlay] (pic cs:line-code-\k-end) circle[radius=2pt];}{\message{No end for \k}}
818 % \iftikzmark{line-code-\k-first}{\fill[green,overlay] (pic cs:line-code-\k-first) circle[radius=2pt];}{\message{No first for \k}}
819 % }
820 % \draw[->,overlay] (0,0) -- (pic cs:line-code-5-first);
821 % \draw[->,overlay] (0,0) -- (pic cs:line-code-5-start);
822 % \draw[->,overlay] (0,0) -- (pic cs:line-code-5-end);
823 % \node[above] at (0,0) {Line 5};
824 % \end{tikzpicture}
825 %
826 % \begin{lstlisting}[language=c,name=code,numbers=left]
827 % #include <stdio.h>
828 %
829 % int main(void)
830 % {
831 % printf("hello, world\n");
832 % return 0;
833 % }
834 % \end{lstlisting}
835 % \end{example}
836 %
837 % This example puts a fancy node behind certain lines of the code, computing the necessary extents.
838 %
839 % \begin{example}
840 % \balloon{comment}{more code}{3}{3}
841 % \balloon{comment}{more code}{7}{8}
842 % \begin{lstlisting}[language=c,name=more code,numbers=left,firstnumber=3]
843 % #include <stdio.h>
844 %
845 % int main(void)
846 % {
847 % printf("hello, world\n");
848 % return 0;
849 % }
850 % \end{lstlisting}
851 % \end{example}
852 %
853 % \subsection{AMS Equation Environments}
854 %
855 % \textbf{This is an experimental library.}
856 %
857 % If the \Verb+amsmath+ package has been loaded then issuing
858 %
859 % \Verb+\usetikzmarklibrary{ams}+
860 %
861 % \noindent loads some code that places pseudo-nodes around the boxes that are used in AMSMath's various equation alignment environments, such as \Verb!align! and \Verb!gather!.
862 % These environments work by constructing boxes with each of the pieces of the equations that are then put together into the grid.
863 % This library hooks in to the unboxing code, before the box is typeset then it measures it and stores that information in various macros as if it were a TikZ node.
864 % The aim is that this doesn't disturb the placement, but as far as TikZ is concerned then there is a node there that can be referred to later.
865 %
866 % As it is experimental, even if this library is loaded then it isn't automatically switched on.
867 % To do that, use either the \Verb!tikzmarkmath! environment or the \Verb!\tikzmarkmath! command.
868 % Each has an optional argument which is a prefix for the node names (the default is \Verb!equation!).
869 % The node names are then of the form \Verb!<prefix>-<number>!.
870 % The numbering is held in a counter called \Verb!tikzmarkequation! and is reset when the command is invoked or the environment is started.
871 % As usual, redefining \Verb!\thetikzmarkequation! changes the styling of the \Verb!<number>!.
872 %
873 % To disable the marking, either end the environment or use \Verb!\endtikzmarkmath!.
874 % The ending command explicitly removes the hook rather than rely on \TeX\ groupings.
875 % It also prints out the number of nodes created to the log file and terminal.
876 % This can be useful with figuring out which nodes to use, since the box that this library hooks into is used many times.
877 % For example, equation numbers are included with this.
878 %
879 % The box is also used when assembling a \Verb!\sqrt[3]{4}! command, and as that uses \Verb!\mathchoice! then there are more boxes created than used.
880 % So the count of number of nodes created can be more than are actually there.
881 %
882 % \begin{example}
883 % \begin{tikzmarkmath}[pythagoras]
884 %
885 % \begin{gather}
886 % a^2 = b^2 + c^2
887 % \end{gather}
888 %
889 % \begin{gather}
890 % a = \sqrt[2]{b^2 + c^2}
891 % \end{gather}
892 % \end{tikzmarkmath}
893 %
894 % \begin{tikzpicture}[remember picture, overlay]
895 % \foreach \k in {1,2,3,7,8} {
896 % \draw[red] (pic cs:pythagoras-\k) -- ++(135:1) node[draw,red,circle,font=\tiny,above left] {\k};
897 % \node[draw,blue,fit=(pythagoras-\k),inner sep=0pt] {};
898 % }
899 % \end{tikzpicture}
900 % % \end{example}
901 %
902 %
903 % \subsection{Highlighting}
904 %
905 % I've returned to the highlighting library.
906 % The \LaTeX3 hook mechanism makes a couple of things possible that were tricky before.
907 %
908 % The idea of the highlighting mechanism is to use two \Verb!\tikzmark!s to mark a start and end of a region to be highlighted.
909 % The region is considered to be formed by lines of text, with the first mark at the baseline of the start and the second at the baseline of the end.
910 %
911 % The highlighting itself is done by inserting code in the shipout routine before the page itself is laid out.
912 % So the highlighting is on a separate layer to the text itself, which can be either behind or in front of the text layer.
913 % The hook mechanisem also makes it relatively simple to support page breaks between the start and end of highlighting.
914 %
915 % Since the highlighting is separate to the flow of the text, it doesn't make sense to use an environment to mark the start and end of the highlighting so instead there are two commands: \Verb!\StartHighlighting[options]! and \Verb!\StopHighlighting!, or a single command \Verb!\Highlight[options]{text}! that just highlights the \Verb!text!.
916 % At the moment, nesting highlighting is not supported.
917 %
918 % The optional argument to \Verb!\StartHighlighting! (or \Verb!\Highlight!) consists of key-value pairs that control the behaviour of the highlighted region.
919 % There are particular keys in the \Verb!/tikz/highlighter! family which control the size of the highlighted region.
920 %
921 % The keys are as follows:
922 %
923 % \begin{itemize}
924 % \item \texttt{direction}
925 % \item \texttt{layer}
926 % \item \texttt{initial height}
927 % \item \texttt{initial depth}
928 % \item \texttt{initial offset}
929 % \item \texttt{final height}
930 % \item \texttt{final depth}
931 % \item \texttt{final offset}
932 % \item \texttt{left margin}
933 % \item \texttt{right margin}
934 % \item \texttt{height}
935 % \item \texttt{depth}
936 % \item \texttt{offset}
937 % \item \texttt{margin}
938 % \end{itemize}
939 %
940 % The highlighting code draws a region which can be styled with standard TikZ keys, more of which in a moment.
941 % Although it is a single region, the \emph{intention} is to simulate using an actual highlighter.
942 % The first key, \Verb!direction!, is used to draw the region as if the highlighter were used in a particular direction.
943 % The options are \Verb!horizontal!, \Verb!vertical!, or \Verb!box!.
944 % The default is \Verb!horizontal!.
945 %
946 % The second key, \Verb!layer!, determines whether the highlighter is rendered on the \Verb!background! or \Verb!foreground! layer.
947 % Using the \Verb!background! layer puts the highlighting underneath the text, which will make the text easier to read.
948 % The \Verb!foreground! option puts the highlighting over the text, which can be used to fade the text.
949 % The default is \Verb!background!.
950 %
951 % The shape of the region depends on a few things, such as whether the highlighting starts and ends on the same line.
952 %
953 % \begin{tikzpicture}[>=Latex]
954 % \fill (0,0) circle[radius=2pt];
955 % \fill (4,0) circle[radius=2pt];
956 % \draw (-1,-1) rectangle (5,1);
957 % \draw[dashed] (0,0) -- (4,0);
958 % \draw[<->] (0,0) -- +(-1,0);
959 % \draw[<->] (0,0) -- +(0,1);
960 % \draw[<->] (4,0) -- +(0,-1);
961 % \draw[<->] (4,0) -- +(1,0);
962 % \draw[<-] (-.5,0) to[out=90,in=-90] +(-1,1.5) node[above] {initial offset};
963 % \draw[<-] (4.5,0) to[out=90,in=-90] +(1,1.5) node[above] {final offset};
964 % \draw[<-] (0,.5) to[out=0,in=180] +(1,1.5) node[right] {initial height};
965 % \draw[<-] (4,-.5) to[out=0,in=180] +(1,-1.5) node[right] {final depth};
966 % \node[anchor=base] at (2,0) {Single line};
967 % \end{tikzpicture}
968 %
969 % \begin{tikzpicture}[>=Latex]
970 % \fill (0,0) circle[radius=2pt];
971 % \draw (-1,-1) rectangle (4,1);
972 % \draw[dashed] (0,0) -- (4,0);
973 % \draw[<->] (0,0) -- +(-1,0);
974 % \draw[<->] (0,0) -- +(0,1);
975 % \draw[<->] (0,0) -- +(0,-1);
976 % \draw[<-] (-.5,0) to[out=90,in=-90] +(-1,1.5) node[above] {initial offset};
977 % \draw[<-] (0,.5) to[out=0,in=180] +(1,1.5) node[right] {initial height};
978 % \draw[<-] (0,-.5) to[out=0,in=180] +(1,-1.5) node[right] {initial depth};
979 % \node[anchor=base] at (2,0) {Split line};
980 % \begin{scope}[yshift=-3cm,xshift=-5cm]
981 % \fill (4,0) circle[radius=2pt];
982 % \draw (0,-1) rectangle (5,1);
983 % \draw[dashed] (0,0) -- (4,0);
984 % \draw[<->] (4,0) -- +(1,0);
985 % \draw[<->] (4,0) -- +(0,1);
986 % \draw[<->] (4,0) -- +(0,-1);
987 % \draw[<-] (4.5,0) to[out=90,in=180] +(2.5,0) node[right] {final offset};
988 % \draw[<-] (4,.5) to[out=180,in=0] +(-1,1.5) node[left] {final height};
989 % \draw[<-] (4,-.5) to[out=0,in=180] +(1.5,-1) node[right] {final depth};
990 % \node[anchor=base] at (2,0) {Split line};
991 % \end{scope}
992 % \end{tikzpicture}
993 %
994 % \begin{tikzpicture}[>=Latex]
995 % \fill (0,0) circle[radius=2pt];
996 % \fill (4,0) circle[radius=2pt];
997 % \fill (-4,-1) circle[radius=2pt];
998 % \fill (4,-1) circle[radius=2pt];
999 % \fill (-4,-2) circle[radius=2pt];
1000 % \fill (0,-2) circle[radius=2pt];
1001 % \draw (-1,0) -- (-1,1) -- (5,1) -- (5,-2) -- (1,-2) -- (1,-3) -- (-5,-3) -- (-5,0) -- cycle;
1002 % \draw[dashed] (0,0) -- (4,0);
1003 % \draw[dashed] (-4,-1) -- (4,-1);
1004 % \draw[dashed] (-4,-2) -- (0,-2);
1005 % \draw[<->] (0,0) -- +(-1,0);
1006 % \draw[<->] (0,0) -- +(0,1);
1007 % \draw[<->] (4,0) -- +(1,0);
1008 % \draw[<->] (0,-2) -- +(1,0);
1009 % \draw[<->] (0,-2) -- +(0,-1);
1010 % \draw[<->] (-4,-2) -- +(-1,0);
1011 % \draw[<-] (-.5,0) to[out=90,in=0] +(-1,.5) node[left] {initial offset};
1012 % \draw[<-] (0,.5) to[out=0,in=180] +(1,0) node[right] {initial height};
1013 % \draw[<-] (4.5,0) to[out=90,in=-90] +(1,1.5) node[above] {right margin};
1014 % \draw[<-] (.5,-2) to[out=-90,in=180] +(1,-.5) node[right] {final offset};
1015 % \draw[<-] (0,-2.5) to[out=180,in=0] +(-1,0) node[left] {final depth};
1016 % \draw[<-] (-4.5,-2) to[out=90,in=-90] +(-1,1.5) node[above] {left margin};
1017 % \node[anchor=base] at (0,-1) {Multiple lines};
1018 % \end{tikzpicture}
1019 %
1020 % The \texttt{vertical} regions and the \texttt{box} are defined similarly.
1021 % With the vertical regions then the meaning of the \Verb!height!, \Verb!depth!, and \Verb!offset! are rotated \(90^\circ\), and the vertial regions don't stretch to the page boundaries.
1022 % The \texttt{box} region is always a rectangle.
1023 %
1024 % Once the region is defined, it can be styled using options directly on the \Verb!StartHighlighting! or \Verb!\Highlight! command and by using the following styles:
1025 %
1026 % \begin{itemize}
1027 % \item \texttt{every highlight picture}
1028 % \item \texttt{every <direction> highlight picture}
1029 % \item \texttt{every <layer> highlight picture}
1030 % \item \texttt{every highlight path}
1031 % \item \texttt{every <direction> highlight path}
1032 % \item \texttt{every <layer> highlight path}
1033 % \item \texttt{highlight path}
1034 % \item \texttt{<direction> highlight path}
1035 % \item \texttt{<layer> highlight path}
1036 % \end{itemize}
1037 %
1038 % The \Verb!picture! keys are for the surrounding \Verb!tikzpicture!, while the \Verb!path! keys are for the path itself.
1039 %
1040 % Lastly, a word about scoping the options.
1041 % Since the code that actually renders the highlighting is processed when the page is shipped out, it may well be that the settings in force when the highlighting was defined have changed.
1042 % The keys that adjust the size of the region (in the \Verb!highlighter! family) are saved at the moment of invocation but keys such as the colour or whether to fill or draw the path are not.
1043 % Therefore, it is wise to use styles that persist to set the rendering styles.
1044 %
1045 % \begin{example}
1046 % The sun was shining on the sea, shining with all its might.
1047 % \StartHighlighting[fill=cyan!50]
1048 % And this was very odd because it was the middle of the night.
1049 % \StopHighlighting
1050 % The moon was up there sulkily because she thought the sun had no
1051 % business to be there after the day was done.
1052 % \StartHighlighting[fill=magenta!50]
1053 % ``It's very rude of him,'' she said, ``to come and spoil the fun.''
1054 % \StopHighlighting
1055
1056 % \noindent The sun was shining on the sea, shining with all its might.
1057 % And this was very odd because it was the middle of the night.
1058 % \StartHighlighting[fill=yellow!50]
1059 % The moon was up there sulkily because she thought the sun had no business to be there after the day was done\StopHighlighting.
1060 % ``It's very rude of him,'' she said, ``to come and spoil the fun.''
1061 % \end{example}
1062 %
1063 % \section{Acknowledgements}
1064 %
1065 % The \Verb+\tikzmark+ macro has been used and abused by many users of \href{http://tex.stackexchange.com}{TeX-SX}.
1066 % Of particular note (but in no particular order) are \href{https://tex.stackexchange.com/users/4301/peter-grill}{Peter Grill}, \href{https://tex.stackexchange.com/users/3954/gonzalo-medina}{Gonzalo Medina}, \href{https://tex.stackexchange.com/users/13304/claudio-fiandrino}{Claudio Fiandrino}, \href{https://tex.stackexchange.com/users/3235/percusse}{percusse}, and \href{https://tex.stackexchange.com/users/121799/marmot}{marmot}.
1067 % I would also like to mention \href{https://tex.stackexchange.com/users/1090/david-carlisle}{David Carlisle} whose knowledge of TikZ continues to astound us all.
1068 %
1069 %
1070 % \StopEventually{}
1071 %
1072 % \section{Implementation}
1073 %
1074 % \iffalse
1075 %<*tikzlibrary>
1076 % \fi
1077 % \subsection{Main Code}
1078 %
1079 %
1080 % The \Verb+save nodes+ code uses \LaTeX3.
1081 % \begin{macrocode}
1082 \ProvidesFile{tikzlibrarytikzmark.code.tex}[%
1083 2022/08/24
1084 v1.15
1085 TikZ library for marking positions in a document]
1086 \RequirePackage{expl3, l3keys2e, xparse}
1087 % \end{macrocode}
1088 %
1089 % \begin{macrocode}
1090 \tikzset{%
1091 remember picture with id/.style={%
1092 remember picture,
1093 overlay,
1094 save picture id=#1,
1095 },
1096 % \end{macrocode}
1097 % Not totally happy with using \Verb+every picture+ here as it's too easily overwritten by the user.
1098 % Maybe it would be better to patch \Verb+endtikzpicture+ directly.
1099 % \begin{macrocode}
1100 every picture/.append style={%
1101 execute at end picture={%
1102 \ifpgfrememberpicturepositiononpage%
1103 \edef\pgf@temp{%
1104 \noexpand\write\noexpand\pgfutil@auxout{%
1105 \string\savepicturepage%
1106 {\pgfpictureid}{\noexpand\arabic{page}}%
1107 }%
1108 }%
1109 \pgf@temp
1110 \fi%
1111 },
1112 },
1113 % \end{macrocode}
1114 % There are times when some code is executed and then discarded, such as in \Verb+\mathchoice+.
1115 % This can seriously mess with how TikZ pictures are remembered as the last \Verb+pgfpictureid+ to be \emph{processed} is the one that is used, but it is the one that is \emph{used} that is recorded in the \Verb+aux+ file.
1116 % This isn't particularly a tikzmark issue, but does come up from time to time with tikzmark as it's all about remembering locations.
1117 %
1118 % In actual fact, it only occurs with \Verb+\tikzmarknode+ since the issue is about how nodes are associated with pictures.
1119 %
1120 % The solution is to check to see if the \Verb+pgfpictureid+ has been recorded in the \Verb+aux+ file and if it hasn't, quietly prefix the node names with a discard term.
1121 % This needs to be used \emph{after} \Verb+remember picture+ has been invoked.
1122 % It probably messes with some other stuff so should only be used under controlled conditions, such as \Verb+\tikzmarknode+.
1123 % \begin{macrocode}
1124 check picture id/.code={
1125 \ifpgfrememberpicturepositiononpage
1126 \@ifundefined{pgf@sys@pdf@mark@pos@\pgfpictureid}{%
1127 \tikzset{%
1128 name prefix/.get=\tzmk@name@prefix,
1129 name prefix/.prefix=discard-,
1130 execute at end picture={%
1131 \tikzset{name prefix/.expand once=\tzmk@name@prefix}%
1132 },
1133 }%
1134 }{}%
1135 \fi
1136 },
1137 % \end{macrocode}
1138 % We also want a failsafe that quietly handles the case where the document hasn't been compiled enough times (once) to get the information into the \Verb+aux+ file.
1139 % There will already be messages about needing reruns so we don't need to add to that.
1140 % We simply ensure that the node exists.
1141 % \begin{macrocode}
1142 maybe define node/.style={%
1143 execute at end picture={%
1144 \ifpgfrememberpicturepositiononpage
1145 \@ifundefined{pgf@sh@pi@\tikz@pp@name{#1}}{%
1146 \pgfnodealias{\tikz@pp@name{#1}}{discard-\tikz@pp@name{#1}}%
1147 }{}%
1148 \fi
1149 }%
1150 },
1151 % \end{macrocode}
1152 % The positions are already recorded in the \Verb+aux+ file, all we really need to do is provide them with better names.
1153 % \begin{macrocode}
1154 save picture id/.code={%
1155 \protected@write\pgfutil@auxout{}{%
1156 \string\savepointas%
1157 {\tikzmark@pp@name{#1}}{\pgfpictureid}{0pt}{0pt}}%
1158 },
1159 % \end{macrocode}
1160 % Provides a way to test if a picture has already been saved (in particular, can avoid errors on first runs)
1161 % \begin{macrocode}
1162 if picture id/.code args={#1#2#3}{%
1163 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1164 \pgfkeysalso{#3}%
1165 }{
1166 \pgfkeysalso{#2}%
1167 }
1168 },
1169 % \end{macrocode}
1170 % Page handling
1171 % \begin{macrocode}
1172 next page/.is choice,
1173 next page vector/.initial={\pgfqpoint{0pt}{0pt}},
1174 next page/below/.style={%
1175 next page vector={\pgfqpoint{0pt}{-\the\paperheight}}%
1176 },
1177 next page/above/.style={%
1178 next page vector={\pgfqpoint{0pt}{\the\paperheight}}%
1179 },
1180 next page/left/.style={%
1181 next page vector={\pgfqpoint{-\the\paperwidth}{0pt}}%
1182 },
1183 next page/right/.style={%
1184 next page vector={\pgfqpoint{\the\paperwidth}{0pt}}%
1185 },
1186 next page/ignore/.style={%
1187 next page vector={\pgfqpoint{0pt}{0pt}}%
1188 },
1189 if tikzmark on current page/.code n args={3}{%
1190 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1191 \pgfkeysalso{#3}%
1192 }{%
1193 \@ifundefined{%
1194 save@pg@\csname save@pt@\tikzmark@pp@name{#1}\endcsname
1195 }{%
1196 \pgfkeysalso{#3}%
1197 }{%
1198 \ifnum\csname save@pg@%
1199 \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
1200 \endcsname=\the\value{page}\relax%
1201 \pgfkeysalso{#2}%
1202 \else
1203 \pgfkeysalso{#3}%
1204 \fi
1205 }%
1206 }%
1207 },
1208 if tikzmark on page/.code n args={4}{%
1209 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1210 \pgfkeysalso{#4}%
1211 }{%
1212 \@ifundefined{%
1213 save@pg@\csname save@pt@\tikzmark@pp@name{#1}@label\endcsname%
1214 }{%
1215 \pgfkeysalso{#4}%
1216 }{%
1217 \ifnum\csname save@pg@%
1218 \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
1219 \endcsname=#2\relax%
1220 \pgfkeysalso{#3}%
1221 \else
1222 \pgfkeysalso{#4}%
1223 \fi
1224 }%
1225 }%
1226 },
1227 % \end{macrocode}
1228 % Prefix and suffix for tikzmark names, shamelessly borrowed from the main tikz code
1229 % \begin{macrocode}
1230 tikzmark prefix/.initial=,%
1231 tikzmark suffix/.initial=,%
1232 tikzmark clear ixes/.style={
1233 tikzmark prefix={},
1234 tikzmark suffix={}
1235 },
1236 % \end{macrocode}
1237 % Tikzmarks can be used to adjust the position of a scope or pic so that an internally defined coordinate is used to locate the scope or pic.
1238 %
1239 % The key used to adjust the location is \Verb!scope anchor={coordinate}! for \Verb!scope!s and \Verb!pic anchor={coordinate}! for \Verb!pic!s, where \Verb!coordinate! is evaluated internally to the \Verb!scope! or \Verb!pic!, so can use node names.
1240 % \begin{macrocode}
1241 scope anchor location/.initial={(0,0)},
1242 scope anchor location/.default=@auto,
1243 pic anchor/.style={
1244 scope anchor location={#1},
1245 next pic/.append style={
1246 adjust scope position,
1247 }
1248 },
1249 scope anchor/.style={
1250 scope anchor location={#1},
1251 adjust scope position,
1252 },
1253 % \end{macrocode}
1254 % The code that does the adjustment is added to the \Verb!pic! on its enclosing scope using the \Verb!every pic! key.
1255 % \begin{macrocode}
1256 adjust scope position/.code={%
1257 \pgfutil@ifundefined{tikz@fig@name}%
1258 {\let\tikz@fig@name=\pgfutil@empty}{}%
1259 \tikz@resetexpandcount%
1260 \tikz@fig@mustbenamed
1261 \pgfkeysgetvalue{/tikz/scope anchor location}\tkzmk@anchor
1262 \ifx\tkzmk@anchor\tikz@auto@text
1263 \tikzset{local bounding box/.expanded=\tikz@fig@name}%
1264 \def\tkzmk@anchor{(\tikz@fig@name.\tikz@anchor)}%
1265 \fi
1266 \tikz@scan@one@point
1267 \pgfutil@firstofone(pic cs:\tikz@fig@name-origin)\relax
1268 \pgf@xa=\pgf@x
1269 \pgf@ya=\pgf@y
1270 \tikz@scan@one@point
1271 \pgfutil@firstofone(pic cs:\tikz@fig@name-anchor)\relax
1272 \advance\pgf@xa by -\pgf@x
1273 \advance\pgf@ya by -\pgf@y
1274 \tikzset{
1275 shift={(\the\pgf@xa,\the\pgf@ya)},
1276 execute at end scope={%
1277 \tikzmark{\tikz@fig@name-origin}{(0,0)}%
1278 \tikzmark{\tikz@fig@name-anchor}{\tkzmk@anchor}%
1279 }
1280 }
1281 },
1282 % \end{macrocode}
1283 %
1284 % To install this code on a pic, we hook in to the pic's enclosing scope using the \Verb+every pic+ key.
1285 % To avoid this bubbling down to pics within pics, we clear it once it has been executed.
1286 % So any code that triggers this adjustment adds \Verb!adjust pic position! to the !next pic! style.
1287 % \begin{macrocode}
1288 every pic/.append style={
1289 next pic/.try,
1290 next pic/.style={}
1291 },
1292 % \end{macrocode}
1293 %
1294 % This code remembers the bounding box of a pic, saving it as if it were a node.
1295 % \begin{macrocode}
1296 save pic bounding box/.code={
1297 \tikz@fig@mustbenamed
1298 \tikzset{local bounding box/.expanded=\tikz@fig@name}
1299 },
1300 surround pic/.style={
1301 next pic/.append style={
1302 save pic bounding box
1303 }
1304 },
1305 }
1306 % \end{macrocode}
1307 %
1308 % \begin{macro}{\tikzmark@pp@name}
1309 % \begin{macrocode}
1310 \def\tikzmark@pp@name#1{%
1311 \csname pgfk@/tikz/tikzmark prefix\endcsname%
1312 #1%
1313 \csname pgfk@/tikz/tikzmark suffix\endcsname%
1314 }%
1315 % \end{macrocode}
1316 % \end{macro}
1317 %
1318 % \begin{macro}{\savepointas}
1319 % This is what gets written to the \Verb+aux+ file.
1320 % \begin{macrocode}
1321 \def\savepointas#1#2#3#4{%
1322 \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
1323 \expandafter\gdef\csname save@pt@#1@offset\endcsname%
1324 {\pgfqpoint{#3}{#4}}%
1325 }
1326 \def\savepicturepage#1#2{%
1327 \expandafter\gdef\csname save@pg@#1\endcsname{#2}%
1328 }
1329 % \end{macrocode}
1330 % \end{macro}
1331 %
1332 % \begin{macro}{\tikzmarkalias}
1333 % Alias a tikzmark to another name (used in tikzmarknode).
1334 % The alias is saved to the aux-file so that it is available prior to the definition.
1335 % The private one doesn't use the prefix-suffix for greater internal flexibility.
1336 % The public one does.
1337 % \begin{macrocode}
1338 \def\@tikzmarkalias#1#2{%
1339 \@ifundefined{save@pt@#2}{}{%
1340 \pgf@node@gnamelet{save@pt@#1}{save@pt@#2}%
1341 \pgf@node@gnamelet{save@pt@#1@offset}{save@pt@#2@offset}%
1342 \protected@write\pgfutil@auxout{}{%
1343 \string\savepointas%
1344 {#1}{\csname save@pt@#2\endcsname}%
1345 \expandafter\expandafter\expandafter
1346 \@gobble\csname save@pt@#2@offset\endcsname
1347 }%
1348 }%
1349 }
1350 \def\tikzmarkalias#1#2{%
1351 \@tikzmarkalias{\tikzmark@pp@name{#1}}{\tikzmark@pp@name{#2}}%
1352 }
1353 % \end{macrocode}
1354 % \end{macro}
1355 %
1356 % \begin{macro}{\tmk@labeldef}
1357 % Auxiliary command for the coordinate system.
1358 % \begin{macrocode}
1359 \def\tmk@labeldef#1,#2\@nil{%
1360 \edef\tmk@label{\tikzmark@pp@name{#1}}%
1361 \def\tmk@def{#2}%
1362 }
1363 % \end{macrocode}
1364 % \end{macro}
1365 %
1366 % \begin{macro}{pic}
1367 % This defines the new coordinate system.
1368 % \begin{macrocode}
1369 \tikzdeclarecoordinatesystem{pic}{%
1370 \pgfutil@in@,{#1}%
1371 \ifpgfutil@in@%
1372 \tmk@labeldef#1\@nil
1373 \else
1374 \tmk@labeldef#1,(0pt,0pt)\@nil
1375 \fi
1376 \@ifundefined{save@pt@\tmk@label}{%
1377 \expandafter\tikz@scan@one@point
1378 \expandafter\pgfutil@firstofone\tmk@def\relax
1379 }{%
1380 \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}%
1381 \save@orig@pic%
1382 \pgfsys@getposition{\pgfpictureid}\save@this@pic%
1383 \pgf@process{\pgfpointorigin\save@this@pic}%
1384 \pgf@xa=\pgf@x
1385 \pgf@ya=\pgf@y
1386 \pgf@process{\pgfpointorigin\save@orig@pic}%
1387 \advance\pgf@x by -\pgf@xa
1388 \advance\pgf@y by -\pgf@ya
1389 \pgf@xa=\pgf@x
1390 \pgf@ya=\pgf@y
1391 \pgf@process%
1392 {\pgfpointorigin\csname save@pt@\tmk@label @offset\endcsname}%
1393 \advance\pgf@xa by \pgf@x
1394 \advance\pgf@ya by \pgf@y
1395 \@ifundefined{save@pg@\csname save@pt@\tmk@label\endcsname}{}{%
1396 \@ifundefined{save@pg@\pgfpictureid}{}{%
1397 \pgfkeysvalueof{/tikz/next page vector}%
1398 \edef\tmk@pg{%
1399 \the\numexpr \csname save@pg@%
1400 \csname save@pt@\tmk@label\endcsname\endcsname%
1401 -
1402 \csname save@pg@\pgfpictureid\endcsname\relax%
1403 }%
1404 \ifnum \tmk@pg > 0 \relax
1405 \advance \pgf@xa by \pgf@x\relax
1406 \advance \pgf@ya by \pgf@y\relax
1407 \fi
1408 \ifnum \tmk@pg < 0 \relax
1409 \advance \pgf@xa by -\pgf@x\relax
1410 \advance \pgf@ya by -\pgf@y\relax
1411 \fi
1412 }%
1413 }%
1414 \pgf@x=\pgf@xa
1415 \pgf@y=\pgf@ya
1416 \pgftransforminvert
1417 \pgf@pos@transform{\pgf@x}{\pgf@y}%
1418 }%
1419 }
1420 % \end{macrocode}
1421 % \end{macro}
1422 %
1423 % \begin{macro}{\tikzmark}
1424 % The active/non-active semi-colon is proving somewhat hazardous to \Verb+\tikzmark+ (see \href{http://tex.stackexchange.com/q/110014/86}{ Tikzmark and french seem to conflict} and \href{http://tex.stackexchange.com/q/335485/86}{Clash between tikzmark, babel package (french) and babel tikzlibrary}) so \Verb+\tikzmark+ now uses the brace-delimited version of the \Verb+\tikz+ command.
1425 %
1426 % This version is for when we're outside a tikzpicture environment
1427 % \begin{macrocode}
1428 \newcommand\tikzmark@outside[2][]{%
1429 \tikzset{external/export next/.try=false}%
1430 \tikz[remember picture with id=#2]{#1}%
1431 }
1432 % \end{macrocode}
1433 % This is for when we're inside a tikzpicture environment
1434 % \begin{macrocode}
1435 \def\tikzmark@inside#1#2{%
1436 \tikzset{remember picture}%
1437 \tikz@resetexpandcount%
1438 \tikz@scan@one@point\pgfutil@firstofone#2\relax
1439 \pgf@pos@transform{\pgf@x}{\pgf@y}%
1440 \protected@write\pgfutil@auxout{}{%
1441 \string\savepointas%
1442 {\tikzmark@pp@name{#1}}{\pgfpictureid}{\the\pgf@x}{\the\pgf@y}}%
1443 }
1444 % \end{macrocode}
1445 % And finally, the ultimate invoker:
1446 % \begin{macrocode}
1447 \def\tikzmark{%
1448 \ifx\pgfpictureid\@undefined
1449 \let\tikzmark@next=\tikzmark@outside
1450 \else
1451 \relax
1452 \ifx\scope\tikz@origscope\relax
1453 \let\tikzmark@next=\tikzmark@outside
1454 \else
1455 \let\tikzmark@next=\tikzmark@inside
1456 \fi
1457 \fi
1458 \tikzmark@next%
1459 }
1460 % \end{macrocode}
1461 % \end{macro}
1462 %
1463 % \begin{macro}{\pgfmark}
1464 % \begin{macrocode}
1465 \newcommand\pgfmark[1]{%
1466 \bgroup
1467 \global\advance\pgf@picture@serial@count by1\relax%
1468 \edef\pgfpictureid{pgfid\the\pgf@picture@serial@count}%
1469 \pgfsys@markposition{\pgfpictureid}%
1470 \edef\pgf@temp{%
1471 \noexpand\write\noexpand\pgfutil@auxout{%
1472 \string\savepicturepage
1473 {\pgfpictureid}{\noexpand\arabic{page}}%
1474 }%
1475 }%
1476 \pgf@temp
1477 \protected@write\pgfutil@auxout{}{%
1478 \string\savepointas
1479 {\tikzmark@pp@name{#1}}{\pgfpictureid}{0pt}{0pt}}%
1480 \egroup
1481 }
1482 % \end{macrocode}
1483 % \end{macro}
1484 %
1485 %
1486 % If the beamer class is used, make the commands overlay aware.
1487 % \begin{macro}{\tikzmark<>}
1488 % \begin{macrocode}
1489 \@ifclassloaded{beamer}{
1490 \renewcommand<>{\tikzmark@outside}[2][]{%
1491 \only#3{\beameroriginal{\tikzmark@outside}[{#1}]{#2}}%
1492 }
1493 \renewcommand<>{\tikzmark@inside}[2]{%
1494 \only#3{\beameroriginal{\tikzmark@inside}{#1}{#2}}%
1495 }
1496 }{}
1497 % \end{macrocode}
1498 % \end{macro}
1499 %
1500 % \begin{macro}{\pgfmark<>}
1501 % \begin{macrocode}
1502 \@ifclassloaded{beamer}{
1503 \renewcommand<>{\pgfmark}[1]{\only#2{\beameroriginal{\pgfmark}{#1}}}
1504 }{}
1505 % \end{macrocode}
1506 % \end{macro}
1507 %
1508 % If beamer is loaded, add a suffix based on the frame number
1509 % \begin{macrocode}
1510 \@ifclassloaded{beamer}{
1511 \tikzset{
1512 tikzmark suffix=-\the\beamer@slideinframe
1513 }
1514 }{}
1515 % \end{macrocode}
1516 %
1517 %
1518 % \begin{macro}{\iftikzmark}
1519 % \begin{macrocode}
1520 \newif\iftikzmark@
1521 \newcommand\iftikzmark[3]{%
1522 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1523 #3%
1524 }{%
1525 #2%
1526 }%
1527 }%
1528 % \end{macrocode}
1529 %
1530 % A version suitable for \Verb+\if ... \else ... \fi+.
1531 % \begin{macrocode}
1532 \newcommand\iftikzmarkexists[1]{%
1533 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1534 \tikzmark@false%
1535 }{%
1536 \tikzmark@true%
1537 }%
1538 \iftikzmark@
1539 }%
1540 % \end{macrocode}
1541 % \end{macro}
1542 %
1543 %
1544 % \begin{macro}{\iftikzmarkonpage}
1545 % \begin{macrocode}
1546 \newcommand\iftikzmarkonpage[2]{%
1547 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1548 \tikzmark@false
1549 }{%
1550 \@ifundefined{save@pg@%
1551 \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
1552 }{%
1553 \tikzmark@false
1554 }{%
1555 \ifnum\csname save@pg@%
1556 \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
1557 \endcsname=#2\relax%
1558 \tikzmark@true
1559 \else
1560 \tikzmark@false
1561 \fi
1562 }%
1563 }%
1564 \iftikzmark@
1565 }
1566 % \end{macrocode}
1567 % \end{macro}
1568 %
1569 % \begin{macro}{\iftikzmarkoncurrentpage}
1570 % \begin{macrocode}
1571 \newcommand\iftikzmarkoncurrentpage[1]{%
1572 \@ifundefined{save@pt@\tikzmark@pp@name{#1}}{%
1573 \tikzmark@false
1574 }{%
1575 \@ifundefined{save@pg@%
1576 \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
1577 }{%
1578 \tikzmark@false
1579 }{%
1580 \ifnum\csname save@pg@%
1581 \csname save@pt@\tikzmark@pp@name{#1}\endcsname%
1582 \endcsname=\the\value{page}\relax%
1583 \tikzmark@true
1584 \else
1585 \tikzmark@false
1586 \fi
1587 }%
1588 }%
1589 \iftikzmark@
1590 }
1591 % \end{macrocode}
1592 % \end{macro}
1593 %
1594 % \begin{macro}{\subnode}
1595 % Note: much of this code was inevitably adapted from the node defining code in the TikZ/PGF sources.
1596 %
1597 % The \Verb!\pgfmark! applies the current tikzmark prefix/suffix.
1598 % The current node prefix/suffix is applied by using the \Verb!name=! key.
1599 % \begin{macrocode}
1600 \def\subnode@#1#2#3{%
1601 \begingroup
1602 \pgfmark{#2}%
1603 \setbox\pgfnodeparttextbox=\hbox\bgroup #3\egroup
1604 \tikzset{every subnode/.try,#1,name=#2}%
1605 \pgfpointorigin
1606 \tikz@scan@one@point\pgfutil@firstofone(pic cs:#2)\relax
1607 \advance\pgf@x by .5\wd\pgfnodeparttextbox
1608 \advance\pgf@y by .5\ht\pgfnodeparttextbox
1609 \advance\pgf@y by -.5\dp\pgfnodeparttextbox
1610 \pgftransformshift{}%
1611 \setbox\@tempboxa=\hbox\bgroup
1612 {%
1613 \let\pgf@sh@savedmacros=\pgfutil@empty% MW
1614 \let\pgf@sh@savedpoints=\pgfutil@empty%
1615 \def\pgf@sm@shape@name{rectangle}% CJ % TT added prefix!
1616 \pgf@sh@s@rectangle%
1617 \pgf@sh@savedpoints%
1618 \pgf@sh@savedmacros% MW
1619 \pgftransformshift{%
1620 \pgf@sh@reanchor{rectangle}{center}%
1621 \pgf@x=-\pgf@x%
1622 \pgf@y=-\pgf@y%
1623 }%
1624 \expandafter\pgfsavepgf@process
1625 \csname pgf@sh@sa@\tikz@fig@name\endcsname{%
1626 \pgf@sh@reanchor{rectangle}{center}% FIXME : this is double work!
1627 }%
1628 % Save the saved points and the transformation matrix
1629 \edef\pgf@node@name{\tikz@fig@name}%
1630 \ifx\pgf@node@name\pgfutil@empty%
1631 \else%
1632 \expandafter\xdef
1633 \csname pgf@sh@ns@\pgf@node@name\endcsname{rectangle}%
1634 \edef\pgf@sh@@temp{%
1635 \noexpand\gdef\expandafter
1636 \noexpand\csname pgf@sh@np@\pgf@node@name\endcsname}%
1637 \expandafter\pgf@sh@@temp\expandafter{%
1638 \pgf@sh@savedpoints}%
1639 \edef\pgf@sh@@temp{%
1640 \noexpand\gdef\expandafter
1641 \noexpand\csname pgf@sh@ma@\pgf@node@name\endcsname}% MW
1642 \expandafter\pgf@sh@@temp\expandafter{\pgf@sh@savedmacros}% MW
1643 \pgfgettransform\pgf@temp
1644 \expandafter\xdef
1645 \csname pgf@sh@nt@\pgf@node@name\endcsname{\pgf@temp}%
1646 \expandafter\xdef
1647 \csname pgf@sh@pi@\pgf@node@name\endcsname{\pgfpictureid}%
1648 \fi%
1649 }%
1650 \egroup
1651 \box\pgfnodeparttextbox
1652 \endgroup
1653 }
1654
1655 \newcommand\subnode[3][]{%
1656 \ifmmode
1657 \mathchoice{%
1658 \subnode@{#1}{#2-d}{\(\displaystyle #3\)}%
1659 }{%
1660 \subnode@{#1}{#2-t}{\(\textstyle #3\)}%
1661 }{%
1662 \subnode@{#1}{#2-s}{\(\scriptstyle #3\)}%
1663 }{%
1664 \subnode@{#1}{#2-ss}{\(\scriptscriptstyle #3\)}%
1665 }%
1666 \let\pgf@nodecallback\pgfutil@gobble
1667 \def\tzmk@prfx{pgf@sys@pdf@mark@pos@pgfid}%
1668 \edef\tzmk@pic{\tzmk@prfx\the\pgf@picture@serial@count}
1669 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1670 \edef\tzmk@pic%
1671 {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-1\relax}%
1672 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1673 \edef\tzmk@pic%
1674 {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-2\relax}%
1675 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1676 \edef\tzmk@pic%
1677 {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-3\relax}%
1678 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1679 \pgfutil@ifundefined{pgf@sh@ns@\tikz@pp@name{#2}}{%
1680 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
1681 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
1682 }{}%
1683 \else
1684 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-d}}%
1685 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-d}}%
1686 \fi
1687 \else
1688 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
1689 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
1690 \fi
1691 \else
1692 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-s}}%
1693 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-s}}%
1694 \fi
1695 \else
1696 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-ss}}%
1697 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-ss}}%
1698 \fi
1699 \else
1700 \subnode@{#1}{#2}{#3}%
1701 \fi
1702 }
1703
1704 % \end{macrocode}
1705 % \end{macro}
1706 %
1707 % \begin{macro}{\tikzmarknode}
1708 % The \Verb+\tikzmark+ macro has changed considerably since its first inception, but there does still seem to be a use for the original version which put stuff inside a node. This command reintroduces that command.
1709 %
1710 % It does its best to work inside a math environment by a sneaky trick involving \Verb+\mathchoice+: the \Verb+remember picture+ key means that only the picture id of the typeset box is saved to the aux file. So comparing the possible picture ids of the four options with the one read from the aux file, we can figure out which box was actually used.
1711 % \begin{macrocode}
1712 \def\tikzmarknode@#1#2#3{%
1713 \tikzset{external/export next/.try=false}%
1714 \tikz[%
1715 remember picture,
1716 save picture id={#2},
1717 check picture id,
1718 maybe define node={#2},
1719 baseline=(#2.base),
1720 every tikzmarknode picture/.try
1721 ] {
1722 \node[
1723 anchor=base,
1724 inner sep=0pt,
1725 minimum width=0pt,
1726 name={#2},
1727 node contents={#3},
1728 every tikzmarknode/.try,
1729 #1
1730 ]}%
1731 }
1732
1733 \newcommand\tikzmarknode[3][]{%
1734 \ifmmode
1735 \mathchoice{%
1736 \tikzmarknode@{#1}{#2-d}{\(\displaystyle #3\)}%
1737 }{%
1738 \tikzmarknode@{#1}{#2-t}{\(\textstyle #3\)}%
1739 }{%
1740 \tikzmarknode@{#1}{#2-s}{\(\scriptstyle #3\)}%
1741 }{%
1742 \tikzmarknode@{#1}{#2-ss}{\(\scriptscriptstyle #3\)}%
1743 }%
1744 \let\pgf@nodecallback\pgfutil@gobble
1745 \def\tzmk@prfx{pgf@sys@pdf@mark@pos@pgfid}%
1746 \edef\tzmk@pic{\tzmk@prfx\the\pgf@picture@serial@count}%
1747 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1748 \edef\tzmk@pic%
1749 {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-1\relax}%
1750 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1751 \edef\tzmk@pic%
1752 {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-2\relax}%
1753 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1754 \edef\tzmk@pic%
1755 {\tzmk@prfx\the\numexpr\the\pgf@picture@serial@count-3\relax}%
1756 \expandafter\ifx\csname\tzmk@pic\endcsname\relax
1757 \pgfutil@ifundefined{pgf@sh@ns@\tikz@pp@name{#2}}{%
1758 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
1759 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
1760 }{}%
1761 \else
1762 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-d}}%
1763 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-d}}%
1764 \fi
1765 \else
1766 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-t}}%
1767 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-t}}%
1768 \fi
1769 \else
1770 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-s}}%
1771 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-s}}%
1772 \fi
1773 \else
1774 \pgfnodealias{\tikz@pp@name{#2}}{\tikz@pp@name{#2-ss}}%
1775 \@tikzmarkalias{\tikzmark@pp@name{#2}}{\tikzmark@pp@name{#2-ss}}%
1776 \fi
1777 \else
1778 \tikzmarknode@{#1}{#2}{#3}%
1779 \fi
1780 }
1781 % \end{macrocode}
1782 % \end{macro}
1783 %
1784 % \begin{macro}{\tikzmark@box}
1785 % This macro takes a name and a box.
1786 % It pretends that there is a tight-fitting rectangular PGF node around that box with the given name, and saves the required information so that that node can be used later on in a tikzpicture drawing.
1787 %
1788 % It does not actually build a node, and it doesn't create a TikZ drawing.
1789 % Rather, it measures the box and uses that information to define the various macros that store the information about the node.
1790 %
1791 % Apart from assigning a load of macros, it does also place a \Verb+\pgfmark+ just before the box.
1792 % This is needed to be able to locate the node on the page.
1793 %
1794 % The command is defined with an \Verb!@! because it is more likely to be used in other packages than by a user.
1795 % \begin{macrocode}
1796 \def\tikzmark@box#1#2{%
1797 \begingroup
1798 \pgfmark{#1}%
1799 \let\pgfnodeparttextbox=#2%
1800 \edef\pgfpictureid{pgfid\the\pgf@picture@serial@count}%
1801 \def\tikz@fig@name{#1}%
1802 \pgfpointorigin
1803 \advance\pgf@x by .5\wd\pgfnodeparttextbox
1804 \advance\pgf@y by .5\ht\pgfnodeparttextbox
1805 \advance\pgf@y by -.5\dp\pgfnodeparttextbox
1806 \pgftransformshift{}%
1807 \setbox\@tempboxa=\hbox\bgroup
1808 {%
1809 \tikzset{
1810 inner sep=0pt,
1811 minimum size=0pt,
1812 outer sep=0pt,
1813 anchor=base
1814 }%
1815 \let\pgf@sh@savedmacros=\pgfutil@empty% MW
1816 \let\pgf@sh@savedpoints=\pgfutil@empty
1817 \def\pgf@sm@shape@name{rectangle}% CJ % TT added prefix!
1818 \pgf@sh@s@rectangle
1819 \pgf@sh@savedpoints
1820 \pgf@sh@savedmacros% MW
1821 \pgftransformshift{%
1822 \pgf@sh@reanchor{rectangle}{center}%
1823 \pgf@x=-\pgf@x
1824 \pgf@y=-\pgf@y
1825 }%
1826 \expandafter\pgfsavepgf@process
1827 \csname pgf@sh@sa@\tikz@fig@name\endcsname{%
1828 \pgf@sh@reanchor{rectangle}{center}% FIXME : this is double work!
1829 }%
1830 % Save the saved points and the transformation matrix
1831 \edef\pgf@node@name{\tikz@fig@name}%
1832 \ifx\pgf@node@name\pgfutil@empty
1833 \else
1834 \expandafter\xdef
1835 \csname pgf@sh@ns@\pgf@node@name\endcsname{rectangle}%
1836 \edef\pgf@sh@@temp{%
1837 \noexpand\gdef\expandafter
1838 \noexpand\csname pgf@sh@np@\pgf@node@name\endcsname}%
1839 \expandafter\pgf@sh@@temp\expandafter{%
1840 \pgf@sh@savedpoints}%
1841 \edef\pgf@sh@@temp{%
1842 \noexpand\gdef\expandafter
1843 \noexpand\csname pgf@sh@ma@\pgf@node@name\endcsname}% MW
1844 \expandafter\pgf@sh@@temp\expandafter{\pgf@sh@savedmacros}% MW
1845 \pgfgettransform\pgf@temp
1846 \expandafter\xdef
1847 \csname pgf@sh@nt@\pgf@node@name\endcsname{\pgf@temp}%
1848 \expandafter\xdef
1849 \csname pgf@sh@pi@\pgf@node@name\endcsname{\pgfpictureid}%
1850 \fi
1851 }%
1852 \egroup
1853 \endgroup
1854 \box#2%
1855 }
1856 % \end{macrocode}
1857 % \end{macro}
1858 %
1859 % \begin{macro}{\usetikzmarklibrary}
1860 % \begin{macrocode}
1861 \def\usetikzmarklibrary{%
1862 \pgfutil@ifnextchar[{\use@tikzmarklibrary}{\use@@tikzmarklibrary}%
1863 }%}
1864 \def\use@tikzmarklibrary[#1]{\use@@tikzmarklibrary{#1}}
1865 \def\use@@tikzmarklibrary#1{%
1866 \edef\pgf@list{#1}%
1867 \pgfutil@for\pgf@temp:=\pgf@list\do{%
1868 \expandafter\pgfkeys@spdef
1869 \expandafter\pgf@temp\expandafter{\pgf@temp}%
1870 \ifx\pgf@temp\pgfutil@empty
1871 \else
1872 \expandafter\ifx
1873 \csname tikzmark@library@\pgf@temp @loaded\endcsname\relax%
1874 \expandafter\global\expandafter\let%
1875 \csname tikzmark@library@\pgf@temp @loaded\endcsname
1876 =\pgfutil@empty%
1877 \expandafter\edef
1878 \csname tikzmark@library@#1@atcode\endcsname{\the\catcode`\@}
1879 \expandafter\edef
1880 \csname tikzmark@library@#1@barcode\endcsname{\the\catcode`\|}
1881 \catcode`\@=11
1882 \catcode`\|=12
1883 \pgfutil@InputIfFileExists{tikzmarklibrary\[email protected]}{}{
1884 \PackageError{tikzmark}{
1885 I did not find the tikzmark extras library '\pgf@temp'.}{}
1886 }%
1887 \catcode`\@=\csname tikzmark@library@#1@atcode\endcsname
1888 \catcode`\|=\csname tikzmark@library@#1@barcode\endcsname
1889 \fi%
1890 \fi
1891 }%
1892 }
1893 % \end{macrocode}
1894 % \end{macro}
1895 %
1896 % The \Verb+save node+ code is written in \LaTeX3.
1897 %
1898 % \begin{macrocode}
1899 \ExplSyntaxOn
1900 \cs_new_protected:Nn \tikzmark_tl_put_right_braced:Nn
1901 {
1902 \tl_put_right:Nn #1 { { #2 } }
1903 }
1904 \cs_generate_variant:Nn \tikzmark_tl_put_right_braced:Nn { NV, cV, cv, Nx, cx }
1905 % \end{macrocode}
1906 %
1907 % This is how we handle return values from functions
1908 % \begin{macrocode}
1909 \tl_new:N \g__sn_output_tl
1910 % \end{macrocode}
1911 % We save our information in a ``property list'', which is L3's
1912 % version of an associative array or dictionary. They keys will give
1913 % the ability to store several groups of nodes and restore them at
1914 % will.
1915 % \begin{macrocode}
1916 \prop_new:N \g__sn_prop
1917 % \end{macrocode}
1918 % We'll need a couple of spare token lists
1919 % \begin{macrocode}
1920 \tl_new:N \l__sn_tmpa_tl
1921 \tl_new:N \l__sn_tmpb_tl
1922 % \end{macrocode}
1923 %
1924 % Another useful token list
1925 % \begin{macrocode}
1926 \tl_new:N \l__open_bracket_tl
1927 \tl_set:Nn \l__open_bracket_tl {[} %]
1928 % \end{macrocode}
1929 %
1930 % This token list is used for our current node group name
1931 % \begin{macrocode}
1932 \tl_new:N \l__sn_group_tl
1933 % \end{macrocode}
1934 %
1935 % We store up the nodes in a list and save them at the end of a given tikzpicture.
1936 % Has to be global as we're often in a group.
1937 % \begin{macrocode}
1938 \clist_new:N \g__sn_nodes_clist
1939 % \end{macrocode}
1940 %
1941 % This boolean is for whether we save to a file or not.
1942 % \begin{macrocode}
1943 \bool_new:N \l__sn_file_bool
1944 % \end{macrocode}
1945 %
1946 % This boolean is for whether we are in the preamble or not.
1947 % \begin{macrocode}
1948 \bool_new:N \g__sn_preamble_bool
1949 \bool_gset_true:N \g__sn_preamble_bool
1950 % \end{macrocode}
1951 %
1952 % Key interface for setting some of the options
1953 % \begin{macrocode}
1954 \keys_define:nn {tikzmark / save nodes}
1955 {
1956 file .bool_set:N = \l__sn_file_bool,
1957 group .tl_set:N = \l__sn_group_tl,
1958 }
1959 % \end{macrocode}
1960 %
1961 %
1962 % \begin{macrocode}
1963 \msg_new:nnn {tikzmark} {no file} {File~ "#1"~ doesn't~ exist.}
1964 \msg_new:nnn {tikzmark} {loading nodes} {Loading~ nodes~ from~ "#1".}
1965 % \end{macrocode}
1966 %
1967 % Dimensions and token lists for shifting
1968 % \begin{macrocode}
1969 \dim_new:N \l__sn_x_dim
1970 \dim_new:N \l__sn_y_dim
1971 \dim_new:N \l__sn_xa_dim
1972 \dim_new:N \l__sn_ya_dim
1973 \tl_new:N \l__sn_centre_tl
1974
1975 \tl_new:N \l__sn_transformation_tl
1976 \tl_set:Nn \l__sn_transformation_tl {{1}{0}{0}{1}{0pt}{0pt}}
1977 % \end{macrocode}
1978 %
1979 % Set up a stream for saving the nodes data to a file
1980 % \begin{macrocode}
1981 \iow_new:N \g__sn_stream
1982 \bool_new:N \g__sn_stream_bool
1983 \tl_new:N \g__sn_filename_tl
1984 \tl_gset:Nx \g__sn_filename_tl {\c_sys_jobname_str}
1985
1986 \cs_new_nopar:Npn \sn_open_stream:
1987 {
1988 \bool_if:NF \g__sn_stream_bool
1989 {
1990 \iow_open:Nn \g__sn_stream {\tl_use:N \g__sn_filename_tl .nodes}
1991 \bool_gset_true:N \g__sn_stream_bool
1992 }
1993 }
1994
1995 \AtEndDocument
1996 {
1997 \ExplSyntaxOn
1998 \bool_if:NT \g__sn_stream_bool
1999 {
2000 \iow_close:N \g__sn_stream
2001 }
2002 \ExplSyntaxOff
2003 }
2004 % \end{macrocode}
2005 %
2006 % LaTeX3 wrappers around some PGF functions (to avoid @-catcode issues)
2007 % \begin{macrocode}
2008 \makeatletter
2009 \cs_set_eq:NN \tikz_set_node_name:n \tikz@pp@name
2010 \cs_set_eq:NN \tikz_fig_must_be_named: \tikz@fig@mustbenamed
2011
2012 \cs_new_nopar:Npn \tikz_scan_point:n #1
2013 {
2014 \tikz@scan@one@point\pgfutil@firstofone#1\relax
2015 }
2016
2017 \cs_new_nopar:Npn \tikz_scan_point:NNn #1#2#3
2018 {
2019 \tikz@scan@one@point\pgfutil@firstofone#3\relax
2020 \dim_set_eq:NN #1 \pgf@x
2021 \dim_set_eq:NN #2 \pgf@y
2022 }
2023
2024 \makeatother
2025 \cs_generate_variant:Nn \tikz_scan_point:n {V}
2026 \cs_generate_variant:Nn \tikz_scan_point:NNn {NNV}
2027 % \end{macrocode}
2028 %
2029 % \begingroup
2030 % \catcode`_=12
2031 % \begin{macro}{\process_node:Nn}
2032 % This is the command that actually does the work. It constructs a
2033 % token list which contains the code that will restore the node data
2034 % when invoked. The two arguments are the token list to store this in
2035 % and the node name to be saved.
2036 % \begin{macrocode}
2037 \cs_new_nopar:Npn \__sn_process_node:n #1
2038 {
2039 \group_begin:
2040 % \end{macrocode}
2041 % Clear our token list
2042 % \begin{macrocode}
2043 \tl_clear:N \l__sn_tmpa_tl
2044 % \end{macrocode}
2045 % Set the centre of the picture
2046 % \begin{macrocode}
2047 \tikz_scan_point:NNn \l__sn_x_dim \l__sn_y_dim
2048 {(current~ bounding~ box.center)}
2049 \dim_set:Nn \l__sn_x_dim {-\l__sn_x_dim}
2050 \dim_set:Nn \l__sn_y_dim {-\l__sn_y_dim}
2051 \tl_set:Nx \l__sn_centre_tl {
2052 {1}{0}{0}{1}{\dim_use:N \l__sn_x_dim}{\dim_use:N \l__sn_y_dim}
2053 }
2054 % \end{macrocode}
2055 % Test to see if the node has been defined
2056 % \begin{macrocode}
2057 \tl_if_exist:cT {pgf@sh@ns@#1}
2058 {
2059 % \end{macrocode}
2060 % The node information is stored in a series of macros of the form
2061 % \Verb+\pgf@sh@XX@nodename+ where XX is one of the following.
2062 % \begin{macrocode}
2063 \clist_map_inline:nn {ns,np,ma,pi}
2064 {
2065 % \end{macrocode}
2066 % Our token list will look like:
2067 %
2068 % \Verb+\tl_set:cn {pgf@sh@XX@nodename}+ {<current contents of that macro>}
2069 %
2070 % This will restore \Verb+\pgf@sh@XX@nodename+ to its current value
2071 % when this list is invoked.
2072 %
2073 % This part puts the \Verb+\tl_set:cn {pgf@sh@XX@nodename}+ in place
2074 % \begin{macrocode}
2075 \tl_put_right:Nn \l__sn_tmpa_tl
2076 {
2077 \tl_gset:cn {pgf@sh@##1@ \tikz_set_node_name:n{#1} }
2078 }
2079 % \end{macrocode}
2080 % Now we put the current contents in place. We're doing this in
2081 % an expansive context to get at the contents. The \Verb+\exp_not:v+
2082 % part takes the current value of \Verb+\pgf@sh@XX@nodename+ and puts
2083 % it in place, preventing further expansion.
2084 % \begin{macrocode}
2085 \tl_if_exist:cTF {pgf@sh@##1@#1}
2086 {
2087 \tl_put_right:Nx \l__sn_tmpa_tl {
2088 {\exp_not:v {pgf@sh@##1@ \tikz_set_node_name:n {#1}}}
2089 }
2090 }
2091 {
2092 \tl_put_right:Nx \l__sn_tmpa_tl {{}}
2093 }
2094 }
2095 \tl_put_right:Nn \l__sn_tmpa_tl
2096 {
2097 \tl_gset:cn {pgf@sh@nt@ \tikz_set_node_name:n{#1} }
2098 }
2099 \compose_transformations:NVv
2100 \l__sn_tmpb_tl \l__sn_centre_tl {pgf@sh@nt@#1}
2101 \tl_put_right:Nx \l__sn_tmpa_tl {{\exp_not:V \l__sn_tmpb_tl}}
2102 \tl_put_right:Nn \l__sn_tmpa_tl {
2103 \transform_node:Nn \l__sn_transformation_tl {
2104 \tikz_set_node_name:n{#1}
2105 }
2106 }
2107 }
2108 % \end{macrocode}
2109 % Once we've assembled our token list, we store it in the given
2110 % token list
2111 % \begin{macrocode}
2112 \tl_gset_eq:NN \g__sn_output_tl \l__sn_tmpa_tl
2113 \group_end:
2114 }
2115 \cs_new_protected_nopar:Npn \process_node:Nn #1#2
2116 {
2117 \__sn_process_node:n {#2}
2118 \tl_set_eq:NN #1 \g__sn_output_tl
2119 \tl_gclear:N \g__sn_output_tl
2120 }
2121 \cs_new_protected_nopar:Npn \process_gnode:Nn #1#2
2122 {
2123 \__sn_process_node:n {#2}
2124 \tl_gset_eq:NN #1 \g__sn_output_tl
2125 \tl_gclear:N \g__sn_output_tl
2126 }
2127 % \end{macrocode}
2128 % \end{macro}
2129 %
2130 % \begin{macro}{\save_nodes_to_list:nn}
2131 % Save the nodes to a list, given a key
2132 % \begin{macrocode}
2133 \cs_new_nopar:Npn \save_nodes_to_list:nn #1#2
2134 {
2135 \tl_clear:N \l__sn_tmpa_tl
2136 \clist_map_inline:nn {#2}
2137 {
2138 \process_node:Nn \l__sn_tmpb_tl {##1}
2139 \tl_put_right:NV \l__sn_tmpa_tl \l__sn_tmpb_tl
2140 }
2141 \prop_gput:NnV \g__sn_prop {#1} \l__sn_tmpa_tl
2142 }
2143 % \end{macrocode}
2144 % \end{macro}
2145 %
2146 % \begin{macro}{\save_nodes_to_file:n}
2147 % Save the nodes to a file
2148 % \begin{macrocode}
2149 \cs_generate_variant:Nn \iow_now:Nn {NV}
2150 \cs_new_nopar:Npn \save_nodes_to_file:n #1
2151 {
2152 \sn_open_stream:
2153 \clist_map_inline:nn {#1}
2154 {
2155 \process_node:Nn \l__sn_tmpa_tl {##1}
2156 % \end{macrocode}
2157 % Save the token list to the nodes file so that on reading it back in, we restore the node definitions
2158 % \begin{macrocode}
2159 \iow_now:Nx \g__sn_stream
2160 {
2161 \iow_newline:
2162 \exp_not:V \l__sn_tmpa_tl
2163 }
2164 }
2165 }
2166 % \end{macrocode}
2167 % \end{macro}
2168 % \begin{macrocode}
2169 \cs_generate_variant:Nn \save_nodes_to_list:nn {VV, Vn}
2170 \cs_generate_variant:Nn \save_nodes_to_file:n {V}
2171 % \end{macrocode}
2172 %
2173 % \begin{macro}{\restore_nodes_from_list:n}
2174 % \begin{macrocode}
2175 \cs_new_nopar:Npn \restore_nodes_from_list:n #1
2176 {
2177 % \end{macrocode}
2178 % Restoring nodes is simple: look in the property list for the key
2179 % and if it exists, invoke the macro stored there.
2180 % \begin{macrocode}
2181 \prop_get:NnNT \g__sn_prop {#1} \l__sn_tmpa_tl
2182 {
2183 \tl_use:N \l__sn_tmpa_tl
2184 }
2185 }
2186 % \end{macrocode}
2187 % \end{macro}
2188 %
2189 % \begin{macro}{\restore_nodes_from_file:n}
2190 % \begin{macrocode}
2191 \cs_new_nopar:Npn \restore_nodes_from_file:n #1
2192 {
2193 \file_if_exist:nTF {#1.nodes}
2194 {
2195 \msg_log:nnn {tikzmark} {loading nodes} {#1}
2196 \ExplSyntaxOn
2197 \file_input:n {#1.nodes}
2198 \ExplSyntaxOff
2199 }
2200 {
2201 \msg_warning:nnn {tikzmark} {no file} {#1}
2202 }
2203 }
2204 \cs_generate_variant:Nn \restore_nodes_from_file:n {x}
2205 \AtBeginDocument{\bool_gset_false:N \g__sn_preamble_bool}
2206 % \end{macrocode}
2207 % \end{macro}
2208 %
2209 % \begin{macro}{\compose_transformations:Nnn}
2210 % Compose PGF transformations \Verb+#2 * #3+, storing the result in \Verb+#1+
2211
2212 % I think the PGF Manual might be incorrect. It implies that the
2213 % matrix is stored row-major, but experimentation implies column-major.
2214 %
2215 % That is, \Verb+{a}{b}{c}{d}{s}{t}+ is:
2216 %
2217 % \[
2218 % \begin{bmatrix} a & c \\ b & d \end{bmatrix}
2219 % \]
2220 %
2221 % \begin{macrocode}
2222 \cs_new_nopar:Npn \compose_transformations:Nnn #1#2#3
2223 {
2224 \tl_gset:Nx #1
2225 {
2226 {\fp_eval:n {
2227 \tl_item:nn {#2} {1}
2228 * \tl_item:nn {#3} {1}
2229 +
2230 \tl_item:nn {#2} {3}
2231 * \tl_item:nn {#3} {2}
2232 }
2233 }
2234 {\fp_eval:n {
2235 \tl_item:nn {#2} {2}
2236 * \tl_item:nn {#3} {1}
2237 +
2238 \tl_item:nn {#2} {4}
2239 * \tl_item:nn {#3} {2}
2240 }
2241 }
2242 {\fp_eval:n {
2243 \tl_item:nn {#2} {1}
2244 * \tl_item:nn {#3} {3}
2245 +
2246 \tl_item:nn {#2} {3}
2247 * \tl_item:nn {#3} {4}
2248 }
2249 }
2250 {\fp_eval:n {
2251 \tl_item:nn {#2} {2}
2252 * \tl_item:nn {#3} {3}
2253 +
2254 \tl_item:nn {#2} {4}
2255 * \tl_item:nn {#3} {4}
2256 }
2257 }
2258 {\fp_to_dim:n {
2259 \tl_item:nn {#2} {1}
2260 * \tl_item:nn {#3} {5}
2261 +
2262 \tl_item:nn {#2} {3}
2263 * \tl_item:nn {#3} {6}
2264 +
2265 \tl_item:nn {#2} {5}
2266 }
2267 }
2268 {\fp_to_dim:n {
2269 \tl_item:nn {#2} {2}
2270 * \tl_item:nn {#3} {5}
2271 +
2272 \tl_item:nn {#2} {4}
2273 * \tl_item:nn {#3} {6}
2274 +
2275 \tl_item:nn {#2} {6}
2276 }
2277 }
2278 }
2279 }
2280 % \end{macrocode}
2281 % \end{macro}
2282 %
2283 % \begin{macrocode}
2284 \cs_generate_variant:Nn \compose_transformations:Nnn
2285 {cVv,NVv,NVn,NvV,NnV}
2286 % \end{macrocode}
2287 %
2288 % \begin{macro}{\transform_node:Nn}
2289 % \begin{macrocode}
2290 \cs_new_nopar:Npn \transform_node:Nn #1#2
2291 {
2292 \compose_transformations:cVv {pgf@sh@nt@#2} #1 {pgf@sh@nt@#2}
2293 }
2294 % \end{macrocode}
2295 % \end{macro}
2296 %
2297 % \begin{macro}{\set_transform_from_node:n}
2298 % \begin{macrocode}
2299 \cs_new_nopar:Npn \set_transform_from_node:n #1
2300 {
2301 \tl_set_eq:Nc \l__sn_transformation_tl {pgf@sh@nt@#1}
2302 \tikz_scan_point:NNn \l__sn_x_dim \l__sn_y_dim {(#1.center)}
2303
2304 \dim_set:Nn \l__sn_x_dim {
2305 \l__sn_x_dim - \tl_item:cn {pgf@sh@nt@#1}{5}
2306 }
2307 \dim_set:Nn \l__sn_y_dim {
2308 \l__sn_y_dim - \tl_item:cn {pgf@sh@nt@#1}{6}
2309 }
2310
2311 \compose_transformations:NnV \l__sn_transformation_tl {
2312 {1}{0}{0}{1}{\dim_use:N \l__sn_x_dim}{\dim_use:N \l__sn_y_dim}
2313 } \l__sn_transformation_tl
2314 }
2315 % \end{macrocode}
2316 % \end{macro}
2317 %
2318 % \begin{macrocode}
2319 \cs_generate_variant:Nn \set_transform_from_node:n {v}
2320 % \end{macrocode}
2321 %
2322 % Set the TikZ keys for access to the above commands.
2323 % \begin{macrocode}
2324 \tikzset{
2325 set~ saved~ nodes~ file~ name/.code={
2326 \tl_gset:Nx \g__sn_filename_tl {#1}
2327 },
2328 transform~ saved~ nodes/.code={
2329 \set_transform_from_node:v {tikz@last@fig@name}
2330 },
2331 set~ node~ group/.code={
2332 \tl_set:Nn \l__sn_group_tl {#1}
2333 \pgfkeysalso{
2334 execute~ at~ end~ scope={
2335 \maybe_save_nodes:
2336 }
2337 }
2338 },
2339 % \end{macrocode}
2340 % Are we saving to a file?
2341 % \begin{macrocode}
2342 save~ nodes~ to~ file/.code={
2343 \tl_if_eq:nnTF {#1}{false}
2344 {
2345 \bool_set_false:N \l__sn_file_bool
2346 }
2347 {
2348 \bool_set_true:N \l__sn_file_bool
2349 }
2350 \pgfkeysalso{
2351 execute~ at~ end~ scope={
2352 \maybe_save_nodes:
2353 }
2354 }
2355 },
2356 % \end{macrocode}
2357 % Append current node or named node to the list of nodes to be saved
2358 % \begin{macrocode}
2359 save~ node/.code={
2360 \tl_if_eq:nnTF {#1} {\pgfkeysnovalue}
2361 {
2362 \tikz_fig_must_be_named:
2363 \pgfkeysalso{
2364 append~ after~ command={
2365 \pgfextra{
2366 \clist_gput_right:Nv \g__sn_nodes_clist {tikz@last@fig@name}
2367 }
2368 }
2369 }
2370 }
2371 {
2372 \clist_gput_right:Nn \g__sn_nodes_clist {#1}
2373 }
2374 },
2375 % \end{macrocode}
2376 % Restore nodes from file
2377 % \begin{macrocode}
2378 restore~ nodes~ from~ file/.code={
2379 \bool_if:NTF \g__sn_preamble_bool
2380 {
2381 \restore_nodes_from_file:x {#1}
2382 }
2383 {
2384 \tikz_fig_must_be_named:
2385 \pgfkeysalso{append~ after~ command={
2386 \pgfextra{
2387 \scope
2388 \split_argument:NNn \tikzset \restore_nodes_from_file:x {#1}
2389 \endscope
2390 }
2391 }
2392 }
2393 }
2394 },
2395 restore~ nodes~ from~ file/.default = \g__sn_filename_tl,
2396 % \end{macrocode}
2397 % Restore nodes from list
2398 % \begin{macrocode}
2399 restore~ nodes~ from~ list/.code={
2400 \tikz_fig_must_be_named:
2401 \pgfkeysalso{append~ after~ command={
2402 \pgfextra{
2403 \scope
2404 \split_argument:NNn \tikzset \restore_nodes_from_list:n {#1}
2405 \endscope
2406 }
2407 }
2408 }
2409 }
2410 }
2411 \cs_generate_variant:Nn \clist_gput_right:Nn {Nv}
2412 % \end{macrocode}
2413 %
2414 % \begin{macro}{\split_argument:NNn}
2415 % \begin{macrocode}
2416 \cs_new_nopar:Npn \split_argument:NNn #1#2#3
2417 {
2418 \tl_set:Nx \l__sn_tmpa_tl {\tl_head:n {#3}}
2419 \tl_if_eq:NNTF \l__sn_tmpa_tl \l__open_bracket_tl
2420 {
2421 \split_argument_aux:NNp #1#2#3
2422 }
2423 {
2424 #2 {#3}
2425 }
2426 }
2427 % \end{macrocode}
2428 % \end{macro}
2429 %
2430 % \begin{macro}{\split_argument_aux:NNp}
2431 % \begin{macrocode}
2432 \cs_new_nopar:Npn \split_argument_aux:NNp #1#2[#3]#4
2433 {
2434 #1 {#3}
2435 #2 {#4}
2436 }
2437 % \end{macrocode}
2438 % \end{macro}
2439 %
2440 % \begin{macro}{\maybe_save_nodes:}
2441 % \begin{macrocode}
2442 \cs_new_nopar:Npn \maybe_save_nodes:
2443 {
2444 \clist_if_empty:NF \g__sn_nodes_clist
2445 {
2446 \bool_if:NTF \l__sn_file_bool
2447 {
2448 \save_nodes_to_file:V \g__sn_nodes_clist
2449 }
2450 {
2451 \tl_if_empty:NF \l__sn_group_tl
2452 {
2453 \save_nodes_to_list:VV \l__sn_group_tl \g__sn_nodes_clist
2454 }
2455 }
2456 \clist_gclear:N \g__sn_nodes_clist
2457 }
2458 }
2459 % \end{macrocode}
2460 % \end{macro}
2461 %
2462 % \begin{macro}{\SaveNode}
2463 % Command for saving a node outside a TikZ picture.
2464 % \begin{macrocode}
2465 \DeclareDocumentCommand \SaveNode { o m }
2466 {
2467 \group_begin:
2468 \IfNoValueF {#1}
2469 {
2470 \keys_set:nn {tikzmark / save nodes}
2471 {
2472 file=false,
2473 group=#1
2474 }
2475 }
2476 \bool_if:NTF \l__sn_file_bool
2477 {
2478 \save_nodes_to_file:n {#2}
2479 }
2480 {
2481 \tl_if_empty:NF \l__sn_group_tl
2482 {
2483 \save_nodes_to_list:Vn \l__sn_group_tl {#2}
2484 }
2485 }
2486 \group_end:
2487 }
2488 % \end{macrocode}
2489 % \end{macro}
2490 % \endgroup
2491 % \begin{macrocode}
2492 \ExplSyntaxOff
2493 % \end{macrocode}
2494 % \iffalse
2495 %</tikzlibrary>
2496 % \fi
2497 %
2498 % \subsection{Listings}
2499 %
2500 % \iffalse
2501 %<*listings>
2502 % \fi
2503 %
2504 % From \url{http://tex.stackexchange.com/q/79762/86}
2505 %
2506 % \begin{macrocode}
2507 \@ifpackageloaded{listings}{%
2508 % \end{macrocode}
2509 %
2510 % \begin{macro}{\iflst@linemark}
2511 % A conditional to help with placing the mark at the first non-whitespace character.
2512 % Should be set to true so that we notice the first line of the code.
2513 % \begin{macrocode}
2514 \newif\iflst@linemark
2515 \lst@linemarktrue
2516 % \end{macrocode}
2517 % \end{macro}
2518 %
2519 % \begin{macro}{EveryLine}
2520 % This hook places the mark at the start of the line.
2521 % \begin{macrocode}
2522 \lst@AddToHook{EveryLine}{%
2523 \begingroup
2524 \advance\c@lstnumber by 1\relax
2525 \pgfmark{line-\lst@name-\the\c@lstnumber-start}%
2526 \endgroup
2527 }
2528 % \end{macrocode}
2529 % \end{macro}
2530 %
2531 % \begin{macro}{EOL}
2532 % This hook places the mark at the end of the line and resets the conditional for placing the first mark.
2533 % \begin{macrocode}
2534 \lst@AddToHook{EOL}{\pgfmark{line-\lst@name-\the\c@lstnumber-end}%
2535 \global\lst@linemarktrue
2536 }
2537 % \end{macrocode}
2538 % \end{macro}
2539 %
2540 % \begin{macro}{OutputBox}
2541 % Experimenting shows that this is the right place to set the mark at the first non-whitespace character.
2542 % But we only want to do this once per line.
2543 % \begin{macrocode}
2544 \lst@AddToHook{OutputBox}{%
2545 \iflst@linemark
2546 \pgfmark{line-\lst@name-\the\c@lstnumber-first}%
2547 \global\lst@linemarkfalse
2548 \fi
2549 }
2550 % \end{macrocode}
2551 % \end{macro}
2552 %
2553 % \begin{macro}{\tikzmk@lst@fnum}
2554 % An auxiliary macro to figure out if the \Verb+firstnumber+ key was set.
2555 % If so, it has the form \Verb+<number>\relax+.
2556 % If not, it expands to a single token.
2557 % \begin{macrocode}
2558 \def\tkzmk@lst@fnum#1\relax#2\@STOP{%
2559 \def\@test{#2}%
2560 \ifx\@test\@empty
2561 \def\tkzmk@lst@start{0}%
2562 \else
2563 \@tempcnta=#1\relax
2564 \advance\@tempcnta by -1\relax
2565 \def\tkzmk@lst@start{\the\@tempcnta}%
2566 \fi
2567 }
2568 % \end{macrocode}
2569 % \end{macro}
2570 %
2571 % \begin{macro}{Init}
2572 % Adds a mark at the start of the listings environment.
2573 % \begin{macrocode}
2574 \lst@AddToHook{Init}{%
2575 \expandafter\tkzmk@lst@fnum\lst@firstnumber\relax\@STOP
2576 \pgfmark{line-\lst@name-\tkzmk@lst@start-start}%
2577 }
2578 % \end{macrocode}
2579 % \end{macro}
2580 %
2581 % \begin{macrocode}
2582 }{%
2583 \PackageError{tikzmark listings}%
2584 {The listings package has not been loaded.}{}
2585 }
2586 % \end{macrocode}
2587 % \iffalse
2588 %</listings>
2589 % \fi
2590 %
2591 % \subsection{AMS Math}
2592 %
2593 % This tikzmark library defines a routine that puts a pseudo-node (using \Verb+\tikzmark@box+) around all the pieces used in constructing the various math environments that the AMS Math package provides, such as \Verb+gather+ and \Verb+align+.
2594 % All of these (and their labels) work by putting various pieces into a box and then typesetting that box in the cells of an \Verb+halign+.
2595 % By using \Verb+\tikzmark@box+, this can be infiltrated to put nodes around each of those boxes as it is placed.
2596 %
2597 % \iffalse
2598 %<*ams>
2599 % \fi
2600 % \begin{macrocode}
2601 \@ifpackageloaded{amsmath}{%
2602 % \end{macrocode}
2603 %
2604 % \begin{macro}{tikzmarkmath}
2605 %
2606 % Defines an environment in which any AMS mathematical aligned environments get nodes around each piece of their contents.
2607 %
2608 % Start by saving the original \Verb+\boxz@+ command.
2609 % \begin{macrocode}
2610 \let\tikzmark@ams@boxz@=\boxz@
2611 % \end{macrocode}
2612 %
2613 % We'll need a counter to keep track of the nodes.
2614 % \begin{macrocode}
2615 \newcounter{tikzmarkequation}
2616 % \end{macrocode}
2617 %
2618 % The nodes will be labelled \Verb!<name>-<number>!.
2619 % By default the name is \Verb!equation! but this can be customised.
2620 % \begin{macrocode}
2621 \def\tikzmark@ams@name{equation}
2622 % \end{macrocode}
2623 %
2624 % This is the substitute command.
2625 % I don't know if the \Verb=\ifmeasuring@= actually does anything, but it's here just in case at the moment.
2626 % \begin{macrocode}
2627 \def\tikzmark@boxz@{%
2628 \ifmeasuring@
2629 \tikzmark@ams@boxz@
2630 \else
2631 \stepcounter{tikzmarkequation}%
2632 \tikzmark@box{\tikzmark@ams@name-\thetikzmarkequation}{\z@}%
2633 \fi
2634 }
2635 % \end{macrocode}
2636 %
2637 % This is the environment that sets the node name and swaps out the box code.
2638 % At the end of the environment we swap back the code so that the commands can be used as standalone \Verb+\tikzmarkmath+ and \Verb+\endtikzmarkmath+ in occasions when it isn't appropriate to use an environment (for example, if it crosses sections, or if it is wanted to turn on this feature for an entire document).
2639 % At the end of the environment, the number of nodes is written out to the terminal and log file to make it easier to keep track.
2640 % \begin{macrocode}
2641 \newenvironment{tikzmarkmath}[1][equation]{%
2642 \def\tikzmark@ams@name{#1}%
2643 \setcounter{tikzmarkequation}{0}%
2644 \let\boxz@=\tikzmark@boxz@
2645 }{%
2646 \let\boxz@=\tikzmark@ams@boxz@
2647 \message{%
2648 Tikzmark math environment
2649 \tikzmark@ams@name\space had
2650 \the\value{tikzmarkequation} nodes in it
2651 }%
2652 }
2653 % \end{macrocode}
2654 % \end{macro}
2655 % \begin{macrocode}
2656 }{%
2657 \PackageError{tikzmark AMS}%
2658 {The amsmath package has not been loaded.}%
2659 {}
2660 }
2661 % \end{macrocode}
2662 % \iffalse
2663 %</ams>
2664 % \fi
2665 %
2666 % \iffalse
2667 %<*highlighting>
2668 % \fi
2669 %
2670 % \subsection{Highlighting}
2671 %
2672 % An early use of \Verb!\tikzmark! was to add highlighting to text by drawing over or under the text between two tikzmarks, for example the question \href{http://tex.stackexchange.com/q/46434/86}{How to "highlight" text/formulas with tikz?}.
2673 %
2674 % I was never totally happy with the overall mechanism, so didn't include it in the main tikzmark package.
2675 % Recently, I had occasion to revisit it and by using the new \LaTeX3 hook facility I got something that I was sufficiently happy with to add to the main package.
2676 %
2677 % The key idea is to hook into the \texttt{shipout/background} routine to insert the highlighting behind the text.
2678 % This allows us to draw the highlighting before the page is laid out and so is under the text.
2679 %
2680 % \LaTeX3 makes life just that little bit easier.
2681 % \begin{macrocode}
2682 \ExplSyntaxOn
2683 % \end{macrocode}
2684 %
2685 % Since the code that draws the highlighting will probably be very separate from the code that defines it, when storing the highlighting code then we want to expand the tikzmark full name.
2686 % \begin{macrocode}
2687 \cs_new_protected_nopar:Npn \tikzmark_fix_name:Nn #1#2
2688 {
2689 \tl_set:Nx #1 {\tikzmark@pp@name{#2}}
2690 }
2691 % \end{macrocode}
2692 %
2693 % \begin{macro}{\StartHighlighting,\EndHighlighting,\Highlight}
2694 % These are the user interfaces for highlighting a section.
2695 % The first command inserts the drawing code into the relevant hook and places a tikzmark at the current location.
2696 % The second command indicates when the highlighting should stop.
2697 % The third is a short cut for highlighting its argument.
2698 %
2699 % These are commands rather than an environment to allow it to span, for example, different parts of an aligned equation.
2700 % \begin{macrocode}
2701 \tl_new:N \g__tikzmark_highlighter_tl
2702 \tl_set:Nn \g__tikzmark_highlighter_tl {tikzmark~ highlighter~}
2703 \int_new:N \g__tikzmark_highlighter_int
2704 \tl_new:N \l__tikzmark_start_tl
2705 \tl_new:N \l__tikzmark_end_tl
2706 \tl_new:N \l__tikzmark_highlighter_name_tl
2707 \tl_new:N \l__tikzmark_tmpa_tl
2708 \tl_new:N \l__tikzmark_tmpb_tl
2709 \tl_new:N \l__tikzmark_tmpc_tl
2710
2711 \cs_new_protected_nopar:Npn \tikzmark_bake_highlighter:N #1
2712 {
2713 \tl_clear:N #1
2714 \clist_map_inline:nn {direction,layer}
2715 {
2716 \tl_put_right:Nx #1 {
2717 /tikz/highlighter/##1=\pgfkeysvalueof{/tikz/highlighter/##1},
2718 }
2719 }
2720 \clist_map_inline:nn {
2721 initial~ height,
2722 initial~ depth,
2723 initial~ offset,
2724 final~ height,
2725 final~ depth,
2726 final~ offset,
2727 left~ margin,
2728 right~ margin,
2729 top~ margin,
2730 bottom~ margin,
2731 }
2732 {
2733 \tl_put_right:Nx #1 {
2734 /tikz/highlighter/##1=\dim_eval:n {\pgfkeysvalueof{/tikz/highlighter/##1}},
2735 }
2736 }
2737 }
2738
2739 \cs_new_protected_nopar:Npn \tikzmark_start_highlighting:n #1
2740 {
2741 \int_gincr:N \g__tikzmark_highlighter_int
2742 \tl_set:Nx \l__tikzmark_highlighter_name_tl
2743 {
2744 \tl_use:N \g__tikzmark_highlighter_tl
2745 \int_use:N \g__tikzmark_highlighter_int
2746 }
2747 \tl_set:Nn \l__tikzmark_tmpb_tl
2748 {
2749 every~ highlighter/.try,
2750 }
2751 \tikzmark_bake_highlighter:N \l__tikzmark_tmpc_tl
2752 \tl_put_right:NV \l__tikzmark_tmpb_tl \l__tikzmark_tmpc_tl
2753 \tl_put_right:Nn \l__tikzmark_tmpb_tl {#1}
2754 \tikzmark_process_highlighting:VV
2755 \l__tikzmark_tmpb_tl
2756 \l__tikzmark_highlighter_name_tl
2757 \tikzmark{highlight-start-\tl_use:N \l__tikzmark_highlighter_name_tl}
2758 }
2759 \cs_new_protected_nopar:Npn \tikzmark_end_highlighting:
2760 {
2761 \tl_set:Nx \l__tikzmark_highlighter_name_tl
2762 {
2763 \tl_use:N \g__tikzmark_highlighter_tl
2764 \int_use:N \g__tikzmark_highlighter_int
2765 }
2766 \tikzmark{highlight-end-\tl_use:N \l__tikzmark_highlighter_name_tl}
2767 }
2768
2769 \NewDocumentCommand \StartHighlighting {O{}}
2770 {%
2771 \tikzmark_start_highlighting:n {#1}
2772 }
2773 \NewDocumentCommand \StopHighlighting {}
2774 {%
2775 \tikzmark_end_highlighting:
2776 }
2777 \NewDocumentCommand \Highlight {O{} m}
2778 {%
2779 \tikzmark_start_highlighting:n {#1}
2780 #2
2781 \tikzmark_end_highlighting:
2782 }
2783 % \end{macrocode}
2784 % \end{macro}
2785 %
2786 % The following code inserts the drawing command into the shipout hook.
2787 % \begin{macrocode}
2788 % \end{macrocode}
2789 % We need an ordinary colon, rather than a \LaTeX3 one
2790 % \begin{macrocode}
2791 \tl_const:Nx \c__tikzmark_colon_tl
2792 {
2793 \char_generate:nn {`:} {12}
2794 }
2795
2796 \cs_generate_variant:Nn \hook_gput_next_code:nn {nV}
2797 % \end{macrocode}
2798 %
2799 % \begin{macrocode}
2800 \cs_new_protected_nopar:Npn \tikzmark_highlight_or_shunt:nnnn #1#2#3#4
2801 {
2802 % \end{macrocode}
2803 % First, test to check if the tikzmarks are actually defined yet,
2804 % if not then bail out.
2805 % \begin{macrocode}
2806 \bool_lazy_all:nT
2807 {
2808 {\tl_if_exist_p:c {save@pt@\tikzmark@pp@name{#2}}}
2809 {\tl_if_exist_p:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#2}}}}
2810 {\tl_if_exist_p:c {save@pt@\tikzmark@pp@name{#3}}}
2811 {\tl_if_exist_p:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#3}}}}
2812 }
2813 {
2814 % \end{macrocode}
2815 % Okay, so all the tikzmarks are defined. Now see if we're on the
2816 % right page. Is our start tikzmark in the future?
2817 % \begin{macrocode}
2818 \int_compare:nTF
2819 {
2820 \tl_use:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#2}}}
2821 >
2822 \the\value{page}
2823 }
2824 {
2825 % \end{macrocode}
2826 % It is, so we just punt our highlighting down the line
2827 % \begin{macrocode}
2828 \hook_gput_next_code:nn {#1} {
2829 \tikzmark_highlight_or_shunt:nnnn {#1}{#2}{#3}{#4}
2830 }
2831 }
2832 {
2833 % \end{macrocode}
2834 % It isn't, so we have some highlighting to do.
2835 % We need to build our highlighting code.
2836 % \begin{macrocode}
2837 \tl_set:Nn \l__tikzmark_tmpa_tl {#4}
2838 % \end{macrocode}
2839 % Is our starting tikzmark on \emph{this} page?
2840 % \begin{macrocode}
2841 \int_compare:nTF
2842 {
2843 \tl_use:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#2}}}
2844 =
2845 \the\value{page}
2846 }
2847 {
2848 % \end{macrocode}
2849 % It is, so we use the starting tikzmark as our first coordinate.
2850 % \begin{macrocode}
2851 \tl_put_right:Nx \l__tikzmark_tmpa_tl
2852 {
2853 {
2854 pic~ cs
2855 \tl_use:N \c__tikzmark_colon_tl
2856 #2
2857 }
2858 }
2859 }
2860 {
2861 % \end{macrocode}
2862 % It isn't, so we use the north west corner of the page
2863 % \begin{macrocode}
2864 \tl_put_right:Nn \l__tikzmark_tmpa_tl
2865 {
2866 {
2867 page.north~ west
2868 }
2869 }
2870 }
2871 % \end{macrocode}
2872 % Is our ending tikzmark on \emph{this} page?
2873 % \begin{macrocode}
2874 \int_compare:nTF
2875 {
2876 \tl_use:c {save@pg@\tl_use:c{save@pt@\tikzmark@pp@name{#3}}}
2877 =
2878 \the\value{page}
2879 }
2880 {
2881 % \end{macrocode}
2882 % It is, so we use the ending tikzmark as our second coordinate.
2883 % \begin{macrocode}
2884 \tl_put_right:Nx \l__tikzmark_tmpa_tl
2885 {
2886 {
2887 pic~ cs
2888 \tl_use:N \c__tikzmark_colon_tl
2889 #3
2890 }
2891 }
2892 }
2893 {
2894 % \end{macrocode}
2895 % It isn't, so we use the south east corner of the page, and
2896 % we have to shunt the code to the next page.
2897 % \begin{macrocode}
2898 \tl_put_right:Nn \l__tikzmark_tmpa_tl
2899 {
2900 {
2901 page.south~ east
2902 }
2903 }
2904 \hook_gput_next_code:nn {#1} {
2905 \tikzmark_highlight_or_shunt:nnnn {#1}{#2}{#3}{#4}
2906 }
2907 }
2908 % \end{macrocode}
2909 % We've built our highlighting code, now's time to execute it.
2910 % \begin{macrocode}
2911 \tl_use:N \l__tikzmark_tmpa_tl
2912 }
2913 }
2914 }
2915 % \end{macrocode}
2916 %
2917 % \begin{macrocode}
2918 \cs_new_protected_nopar:Npn \tikzmark_process_highlighting:nn #1#2
2919 {
2920 \pgfkeys{/tikz/highlighter/configuration/.activate~ family}
2921 \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
2922
2923 \tikzmark_fix_name:Nn \l__tikzmark_start_tl {highlight-start-#2}
2924 \tikzmark_fix_name:Nn \l__tikzmark_end_tl {highlight-end-#2}
2925 \tl_set:Nx \l__tikzmark_tmpa_tl {\pgfkeysvalueof{/tikz/highlighter/direction}}
2926 \tl_clear:N \l__tikzmark_tmpb_tl
2927 \tl_clear:N \l__tikzmark_tmpc_tl
2928 \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {vertical}
2929 {
2930 \tl_put_right:Nn \l__tikzmark_tmpb_tl
2931 {
2932 \vl@draw
2933 }
2934 }
2935 {
2936 \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {box}
2937 {
2938 \tl_put_right:Nn \l__tikzmark_tmpb_tl
2939 {
2940 \box@draw
2941 }
2942 }
2943 {
2944 \tl_put_right:Nn \l__tikzmark_tmpb_tl
2945 {
2946 \hl@draw
2947 }
2948 }
2949 }
2950
2951 \tl_put_right:Nn \l__tikzmark_tmpb_tl
2952 {
2953 {tikzmark~ clear~ ixes,#1}
2954 }
2955
2956 \tl_set:Nx \l__tikzmark_tmpa_tl {\pgfkeysvalueof{/tikz/highlighter/layer}}
2957 \tl_set:Nn \l__tikzmark_tmpc_tl
2958 {
2959 \tikzmark_highlight_or_shunt:nnnn
2960 }
2961 \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {foreground}
2962 {
2963 \tl_put_right:Nn \l__tikzmark_tmpc_tl {{shipout/foreground}}
2964 }
2965 {
2966 \tl_put_right:Nn \l__tikzmark_tmpc_tl {{shipout/background}}
2967 }
2968
2969 \tikzmark_tl_put_right_braced:NV \l__tikzmark_tmpc_tl \l__tikzmark_start_tl
2970 \tikzmark_tl_put_right_braced:NV \l__tikzmark_tmpc_tl \l__tikzmark_end_tl
2971 \tikzmark_tl_put_right_braced:NV \l__tikzmark_tmpc_tl \l__tikzmark_tmpb_tl
2972
2973 \tl_if_eq:NnTF \l__tikzmark_tmpa_tl {foreground}
2974 {
2975 \hook_gput_next_code:nV {shipout/foreground} \l__tikzmark_tmpc_tl
2976 }
2977 {
2978 \hook_gput_next_code:nV {shipout/background} \l__tikzmark_tmpc_tl
2979 }
2980 }
2981 \cs_generate_variant:Nn \tikzmark_process_highlighting:nn {nV,VV}
2982 \ExplSyntaxOff
2983 % \end{macrocode}
2984 %
2985 % The command that draws the horizontal highligher or fader.
2986 % This fills a shape determined by two coordinates assumed to be (in effect) on the baseline of the start and end of the region to be highlighted.
2987 % \begin{macrocode}
2988 \def\hl@draw#1#2#3{%
2989 \pgfkeys{/tikz/highlighter/configuration/.activate family}
2990 \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
2991 \begin{tikzpicture}[
2992 remember picture,
2993 overlay,
2994 highlight picture action,
2995 #1,
2996 ]%
2997 %
2998 \page@node
2999 %
3000 \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
3001 \pgf@ya=\pgf@y
3002 \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
3003 \pgf@yb=\pgf@y
3004 %
3005 \ifdim\pgf@ya=\pgf@yb
3006 %
3007 \path (#2)
3008 ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
3009 \pgfkeysvalueof{/tikz/highlighter/initial height})
3010 coordinate (start);
3011 %
3012 \path (#3)
3013 ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
3014 -1*\pgfkeysvalueof{/tikz/highlighter/final depth})
3015 coordinate (end);
3016 %
3017 \path[
3018 highlight action,
3019 #1
3020 ] (start) rectangle (end);
3021 %
3022 \else
3023 %
3024 \path (page.east)
3025 ++(\pgfkeysvalueof{/tikz/highlighter/right margin},0pt)
3026 coordinate (east);
3027 %
3028 \path (page.west)
3029 ++(-1*\pgfkeysvalueof{/tikz/highlighter/left margin},0pt)
3030 coordinate (west);
3031 %
3032 \pgfmathsetlength\pgf@x{%
3033 \pgfkeysvalueof{/tikz/highlighter/initial height}%
3034 }%
3035 %
3036 \advance\pgf@yb by \pgf@x\relax
3037 %
3038 \pgfmathsetlength\pgf@x{%
3039 -1*\pgfkeysvalueof{/tikz/highlighter/final depth}%
3040 }%
3041 %
3042 \advance\pgf@ya by \pgf@x\relax
3043 %
3044 \ifdim\pgf@yb>\pgf@ya
3045 %
3046 \path (#2)
3047 ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
3048 \pgfkeysvalueof{/tikz/highlighter/initial height})
3049 coordinate (start);
3050 %
3051 \path (#2)
3052 ++(0pt,-1*\pgfkeysvalueof{/tikz/highlighter/final depth})
3053 coordinate (end);
3054 %
3055 \path[
3056 highlight action,
3057 #1
3058 ] (start) rectangle (end -| east);
3059 %
3060 \path (#3)
3061 ++(0pt,\pgfkeysvalueof{/tikz/highlighter/initial height})
3062 coordinate (start);
3063 %
3064 \path (#3)
3065 ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
3066 -1*\pgfkeysvalueof{/tikz/highlighter/final depth})
3067 coordinate (end);
3068 %
3069 \path[
3070 highlight action,
3071 #1
3072 ] (start -| west) rectangle (end);
3073 %
3074 \else
3075 %
3076 \path (#2)
3077 ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
3078 \pgfkeysvalueof{/tikz/highlighter/initial height})
3079 coordinate (tl);
3080 %
3081 \path (#2)
3082 ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial offset},
3083 -1*\pgfkeysvalueof{/tikz/highlighter/initial depth})
3084 coordinate (start);
3085 %
3086 \path (#3)
3087 ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
3088 -1*\pgfkeysvalueof{/tikz/highlighter/final depth})
3089 coordinate (end);
3090 %
3091 \path (#3)
3092 ++(\pgfkeysvalueof{/tikz/highlighter/final offset},
3093 \pgfkeysvalueof{/tikz/highlighter/final height})
3094 coordinate (mr);
3095 %
3096 \path[
3097 highlight action,
3098 #1
3099 ] (start) -- (tl) -- (tl -| east) -- (mr -| east) -- (mr) --
3100 (end) -- (end -| west) -- (start -| west) -- cycle;
3101 %
3102 \fi
3103 \fi
3104 \end{tikzpicture}%
3105 }
3106 % \end{macrocode}
3107 %
3108 % This one draws a box.
3109 % \begin{macrocode}
3110 \def\box@draw#1#2#3{%
3111 \pgfkeys{/tikz/highlighter/configuration/.activate family}
3112 \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
3113 \begin{tikzpicture}[
3114 remember picture,
3115 overlay,
3116 highlight picture action,
3117 #1,
3118 ]%
3119 %
3120 \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
3121 \pgf@xa=\pgf@x
3122 \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
3123 \pgf@xb=\pgf@x
3124 %
3125 \def\tkmk@high@bscale{1}%
3126 \ifdim\pgf@xa>\pgf@xb
3127 \def\tkmk@high@bscale{-1}%
3128 \fi
3129 %
3130 \path (#2)
3131 ++({\tkmk@high@bscale*(-1)*\pgfkeysvalueof{/tikz/highlighter/initial offset}},
3132 \pgfkeysvalueof{/tikz/highlighter/initial height})
3133 coordinate (start);
3134 %
3135 \path (#3)
3136 ++(\tkmk@high@bscale*\pgfkeysvalueof{/tikz/highlighter/final offset},
3137 -1*\pgfkeysvalueof{/tikz/highlighter/final depth})
3138 coordinate (end);
3139 %
3140 \path[
3141 highlight action,
3142 #1
3143 ] (start) rectangle (end);
3144 \end{tikzpicture}%
3145 }
3146 % \end{macrocode}
3147 %
3148 % In this one the region is defined vertically.
3149 % \begin{macrocode}
3150 \def\vl@draw#1#2#3{%
3151 \pgfkeys{/tikz/highlighter/configuration/.activate family}
3152 \pgfkeysfiltered{/tikz/.cd,highlighter/direction,highlighter/layer,#1}
3153 \begin{tikzpicture}[
3154 remember picture,
3155 overlay,
3156 highlight picture action,
3157 #1,
3158 ]%
3159 %
3160 \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
3161 \pgf@ya=\pgf@y
3162 \pgf@xa=\pgf@x
3163 \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
3164 \pgf@yb=\pgf@y
3165 \pgf@xb=\pgf@x
3166 %
3167 \pgfmathsetlength\pgf@y{%
3168 \pgfkeysvalueof{/tikz/highlighter/initial offset}%
3169 }%
3170 \advance\pgf@yb by \pgf@y
3171 \pgfmathsetlength\pgf@y{%
3172 -1*\pgfkeysvalueof{/tikz/highlighter/final offset}%
3173 }%
3174 \advance\pgf@ya by \pgf@y
3175 %
3176 \ifdim\pgf@yb>\pgf@ya
3177 %
3178 \ifdim\pgf@xa>\pgf@xb
3179 %
3180 \path (#2)
3181 ++(\pgfkeysvalueof{/tikz/highlighter/initial height},
3182 \pgfkeysvalueof{/tikz/highlighter/initial offset})
3183 coordinate (start);
3184 %
3185 \path (#3)
3186 ++(-1*\pgfkeysvalueof{/tikz/highlighter/final depth},
3187 -1*\pgfkeysvalueof{/tikz/highlighter/final offset})
3188 coordinate (end);
3189 %
3190 \else
3191 %
3192 \path (#2)
3193 ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial depth},
3194 \pgfkeysvalueof{/tikz/highlighter/initial offset})
3195 coordinate (start);
3196 %
3197 \path (#3)
3198 ++(\pgfkeysvalueof{/tikz/highlighter/final height},
3199 -1*\pgfkeysvalueof{/tikz/highlighter/final offset})
3200 coordinate (end);
3201 %
3202 \fi
3203 %
3204 \path[
3205 highlight action,
3206 #1
3207 ] (start) rectangle (end);
3208 %
3209 \else
3210 %
3211 \path (#2)
3212 ++(\pgfkeysvalueof{/tikz/highlighter/initial height},0)
3213 coordinate (tr);
3214 %
3215 \path (#2)
3216 ++(0,\pgfkeysvalueof{/tikz/highlighter/initial offset})
3217 coordinate (start);
3218 %
3219 \path (#2)
3220 ++(-1*\pgfkeysvalueof{/tikz/highlighter/initial depth},0)
3221 coordinate (tl);
3222 %
3223 \path (#3)
3224 ++(\pgfkeysvalueof{/tikz/highlighter/final height},0)
3225 coordinate (br);
3226 %
3227 \path (#3)
3228 ++(0,-1*\pgfkeysvalueof{/tikz/highlighter/final offset})
3229 coordinate (end);
3230 %
3231 \path (#3)
3232 ++(-1*\pgfkeysvalueof{/tikz/highlighter/final depth},0)
3233 coordinate (bl);
3234 %
3235 \tikz@scan@one@point\pgfutil@firstofone(#2)\relax
3236 \pgf@xa=\pgf@x
3237 \tikz@scan@one@point\pgfutil@firstofone(#3)\relax
3238 \pgf@xb=\pgf@x
3239 %
3240 \ifdim\pgf@xa<\pgf@xb
3241 %
3242 \path[
3243 highlight action,
3244 #1
3245 ] (tl) |- (start) -| (tr) -| (br) |- (end) -| (bl) -| cycle;
3246 %
3247 \else
3248 %
3249 \path[
3250 highlight action,
3251 #1
3252 ] (tl) |- (start) -| (tr) |- (br) |- (end) -| (bl) |- cycle;
3253 %
3254 \fi
3255 %
3256 \fi
3257 \end{tikzpicture}
3258 }
3259 % \end{macrocode}
3260 %
3261 % These set various options.
3262 % \begin{macrocode}
3263 \tikzset{%
3264 /tikz/highlighter/.is family,
3265 /tikz/highlighter/.unknown/.code={%
3266 \let\tk@searchname=\pgfkeyscurrentname%
3267 \pgfkeysalso{%
3268 /tikz/\tk@searchname=#1
3269 }
3270 },
3271 every highlight path/.style={
3272 fill=yellow!50,
3273 rounded corners,
3274 },
3275 every foreground highlight path/.style={
3276 fill opacity=.5,
3277 },
3278 highlight picture action/.style={
3279 every highlight picture/.try,
3280 every \pgfkeysvalueof{/tikz/highlighter/direction} highlight picture/.try,
3281 every \pgfkeysvalueof{/tikz/highlighter/layer} highlight picture/.try,
3282 },
3283 highlight action/.style={
3284 every highlight path/.try,
3285 every \pgfkeysvalueof{/tikz/highlighter/direction} highlight path/.try,
3286 every \pgfkeysvalueof{/tikz/highlighter/layer} highlight path/.try,
3287 highlight path/.try,
3288 \pgfkeysvalueof{/tikz/highlighter/direction} highlight path/.try,
3289 \pgfkeysvalueof{/tikz/highlighter/layer} highlight path/.try,
3290 },
3291 /tikz/highlighter/.cd,
3292 direction/.initial=horizontal,
3293 layer/.initial=background,
3294 direction/.default=horizontal,
3295 layer/.default=background,
3296 initial height/.initial=\baselineskip,
3297 initial depth/.initial=.5ex,
3298 initial offset/.initial=.5\baselineskip,
3299 final height/.initial=\baselineskip,
3300 final depth/.initial=.5ex,
3301 final offset/.initial=.5\baselineskip,
3302 left margin/.initial=.5\baselineskip,
3303 right margin/.initial=.5\baselineskip,
3304 top margin/.initial=.5\baselineskip,
3305 bottom margin/.initial=-.5\baselineskip,
3306 height/.style={
3307 initial height=#1,
3308 final height=#1
3309 },
3310 depth/.style={
3311 initial depth=#1,
3312 final depth=#1
3313 },
3314 offset/.style={
3315 initial offset=#1,
3316 final offset=#1
3317 },
3318 margin/.style={
3319 left margin=#1,
3320 right margin=#1,
3321 top margin=#1,
3322 bottom margin=#1,
3323 },
3324 /tikz/highlighter/configuration/.is family,
3325 /tikz/highlighter/direction/.belongs to family=/tikz/highlighter/configuration,
3326 /tikz/highlighter/layer/.belongs to family=/tikz/highlighter/configuration,
3327 }
3328 % \end{macrocode}
3329 %
3330 %
3331 % \begin{macrocode}
3332 \def\page@node{
3333 \path (current page.north west)
3334 ++(\hoffset + 1in + \oddsidemargin + \leftskip,
3335 -\voffset - 1in - \topmargin - \headheight - \headsep)
3336 node[
3337 minimum width=\textwidth - \leftskip - \rightskip,
3338 minimum height=\textheight,
3339 anchor=north west,
3340 line width=0mm,
3341 inner sep=0pt,
3342 outer sep=0pt,
3343 ] (page) {};
3344 }
3345 % \end{macrocode}
3346 %
3347 %
3348 %
3349 % \iffalse
3350 %</highlighting>
3351 % \fi
3352 %
3353 % \Finale
3354 \endinput