# Draw TikZ arrow across multiple pages

I want to have arrows between my marginnotes and I achieved this with \tikzmarknode (figure 1). But it does not work if one mark is on another page (figure 2).

What I would like to have is a continous line going down to the end of the page and then reappear on top of the next page (figure 3).

MWE:

\documentclass[10pt,oneside]{scrbook}
\usepackage{lipsum}
\usepackage[left=32.5mm, right=22.5mm, top=25mm, bottom=20mm, marginparsep=3mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{marginnote}
\renewcommand*{\marginfont}{\footnotesize\itshape}

\begin{document}

\marginnote{\tikzmarknode{a}{Thermoplast}}
\lipsum[1]
\marginnote{\tikzmarknode{b}{teilkristallin}}
\lipsum[1]
\marginnote{\tikzmarknode{c}{aliphatisch}}
\begin{tikzpicture}[remember picture, overlay, shorten >=1.5mm, shorten <=1.5mm,->]
\draw ([xshift=10pt]a.south west) to ([xshift=10pt]b.north west);
\draw ([xshift=10pt]b.south west) to ([xshift=10pt]c.north west);
\end{tikzpicture}
% \lipsum[1-6]    % Works fine
\lipsum[1-7]    % Doesn't work
\marginnote{\tikzmarknode{d}{aliphatisch}}
\begin{tikzpicture}[remember picture, overlay, shorten >=1.5mm, shorten <=1.5mm,->]
\draw ([xshift=10pt]c.south west) to ([xshift=10pt]d.north west);
\end{tikzpicture}

\end{document}

• There is a specific check \iftikzmarkoncurrentpage, and some additional checks, see tex.stackexchange.com/a/483565.
– user194703
Mar 25 '20 at 20:40

The tikzmark library comes with a check \iftikzmarkoncurrentpage. This allows us to distinguish the cases

1. none of the nodes on the current page (with the possibility that the current page is between two pages carrying subsequent marks that are to be connected),
2. first node on the current page,
3. second node on the current page,
4. both nodes on the same page.

These cases are distinguished in the macro \ConnectTikZmarknodes, which connects the nodes also over page breaks. It uses eso-pic to avoid requiring the user to add the macro on several pages. Thanks to the nice checks coming with tikzmark it is possible to (and, if the connection runs over more than one pages, also necessary) to add \ConnectTikZmarknodes before the nodes are actually defined.

\documentclass[10pt,oneside]{scrbook}
\usepackage{lipsum}
\usepackage[left=32.5mm, right=22.5mm, top=25mm, bottom=20mm, marginparsep=3mm]{geometry}
\usepackage{tikzpagenodes}
\usepackage{eso-pic}
\usetikzlibrary{tikzmark}
\usepackage{marginnote}
\renewcommand*{\marginfont}{\footnotesize\itshape}
\makeatletter
\newcommand\PageOfTikzmark[1]{%
\csname save@pg@\csname save@pt@\tikzmark@pp@name{#1}\endcsname\endcsname}
\makeatother
\begin{tikzpicture}[remember picture, overlay, shorten >=1.5mm,
shorten <=1.5mm,#1]
\iftikzmark{#2}{\edef\pageA{\PageOfTikzmark{#2}}}{\edef\pageA{-1}}%
\iftikzmark{#3}{\edef\pageB{\PageOfTikzmark{#3}}}{\edef\pageB{-1}}%
\iftikzmarkoncurrentpage{#2}
\edef\myflag{1}%
\else
\edef\myflag{0}%
\fi
\iftikzmarkoncurrentpage{#3}
\edef\myflag{\the\numexpr\myflag+2}%
\fi
\ifcase\myflag
\or
\draw[-] ([xshift=10pt]#2.south west)
to ([xshift=10pt]#2.south west|-current page text area.south);
\or
\draw[->] ([xshift=10pt]#3.north west|-current page text area.north)
to ([xshift=10pt]#3.north west);
\or
\draw[->] ([xshift=10pt]#2.south west)
to ([xshift=10pt]#3.north west);
\fi
\ifnum\value{page}>\pageA
\ifnum\value{page}<\pageB
\draw ([xshift=20pt]current page text area.north east) to
([xshift=20pt]current page text area.south east);
\fi
\fi
\end{tikzpicture}}}
\begin{document}

\marginnote{\tikzmarknode{a}{Thermoplast}}
\lipsum[1]
\marginnote{\tikzmarknode{b}{teilkristallin}}
\ConnectTikZmarknodes{a}{b}
\lipsum[1]
\marginnote{\tikzmarknode{c}{aliphatisch}}
\ConnectTikZmarknodes{b}{c}
\lipsum[1-7]    % works fine
\marginnote{\tikzmarknode{d}{aliphatisch}}
\ConnectTikZmarknodes{c}{d}
\ConnectTikZmarknodes{d}{e}
\lipsum[1-23]
\marginnote{\tikzmarknode{e}{unproblematisch}}
\end{document}


• Your MWE doesn't work for me. producing this error: Extra \fi. \marginnote
– user108724
Apr 24 '20 at 20:12
• @C.F.G It does work for me. Please, if you post such comments, add the information on the compiler you are using (pdflatex or ...), your TeX installation (TeXLive 2020 or ...), and everything else that might be relevant.
– user194703
Apr 24 '20 at 20:17
• I am using pdflatex TeXLive 2019.
– user108724
Apr 24 '20 at 20:44
• @C.F.G All I can say is that on my updated TeXLive 2020 installation there is no problem. When I was writing the answer I had an updated TeXLive2019 installation. I was always using pdflatex. I'd suspect that you are loading an older version of the tikzmark library, the last update was v1.8 from 2019/10/04.
– user194703
Apr 24 '20 at 20:47