The good news is that this more or less included in the answer you are linking to, which one can upgrade without losing downward compatibility. All one has to do is to make some values that were fixed in the previous code to become pgf keys. Then you can supply the \ConnectTikZmarknodes with some options in which you change the to path to fit your requirement.
In order to have some conventions, we will order the chronological nodes chronologically, i.e. the earlier node will be referred to as start and the later one as target, even though for the backwards connection the arrow head attaches then at the start node. This is why the corresponding keys have a <- arrow.
As in the linked answer, there are four cases:
- None of the nodes on the current page. If the page is between these nodes, the connection will be drawn using the
between style.
- The first node on the current page. In this case the
from style will be used.
- The second node on the current page. In this case the
target style will be used.
- Both nodes on the same page. In this case the
same style will be used.
We indicate which style is responsible for which situation. Each style will usually contain a to path key, which controls how the connection is drawn in detail. For instance,
from/.style={-,to path={([xshift=7pt]\tikztostart.south west) --
([xshift=7pt]\tikztostart.south west|-S)}},
means that we are talking about case 2. The connection will start 7pt right of the south west of anchor of the start node. It will go to ([xshift=7pt]\tikztostart.south west|-S), where the |- syntax in the node means "take the x coordinate of [xshift=7pt]\tikztostart.south west and the y coordinate of S, see e.g. this post for a discussion. The S node is an auxiliary node that sits below the bottom of the text area, with the distance being stored in south margin. Analogous statements apply to N, which is north margin above the top of the text area. The coordinate R is left by right distance of the right border of the page, and can be used to set the horizontal position of the backwards loop.
All one has to do for the backward loop is to change these styles from their initial values. The collection of styles can be stored in a style, which is called loop up here. Given this style, getting the loop is as simple as saying
\ConnectTikZmarknodes[connected tikzmarks/loop up]{2}{4}
Let me stress once more that even though the arrow goes from 4 to 2, the order in which the nodes appear in the command is chronological, i.e. 2 is before 4 because this node is on an earlier page.
\documentclass[10pt,a4paper]{article}
\usepackage[left=32.5mm, right=25mm, top=25mm, bottom=20mm, marginparsep=3mm]{geometry}
\usepackage{marginnote}
\usepackage{tikzpagenodes}
\usetikzlibrary{tikzmark}
\usepackage{eso-pic}
\makeatletter
\newcommand\PageOfTikzmark[1]{%
\csname save@pg@\csname save@pt@\tikzmark@pp@name{#1}\endcsname\endcsname}
\makeatother
\tikzset{connected tikzmarks/.cd,
indent/.initial=7pt,% distance from anchor
north margin/.initial=5mm, % distance the connections go beyond the text area at the top
south margin/.initial=5mm, % distance the connections go beyond the text area at the bottom
right distance/.initial=4mm, % distance of loop arrow from the right boundary of
% the page (not text area)
same/.style={->,to path={([xshift=7pt]\tikztostart.south west) --
([xshift=7pt]\tikztotarget.north west)}},% style for the same page
between/.style={-,to path={([xshift=20pt]current page text area.east|-N)
-- ([xshift=20pt]current page text area.east|-S)}},% style for the that none of the marks is on a page between them
target/.style={->,to path={([xshift=7pt]\tikztotarget.north west|-N) --
([xshift=7pt]\tikztotarget.north west)}},% style for the connection to target while from is on other page
from/.style={-,to path={([xshift=7pt]\tikztostart.south west) --
([xshift=7pt]\tikztostart.south west|-S)}},% style for the connection from start while from is on other page
loop up/.style={rounded corners,/tikz/connected tikzmarks/.cd,
same/.style={<-,to path={([xshift=-7pt]\tikztostart.north east)
|- ([yshift=5mm]\tikztostart.north-|R) --
([yshift=-5mm]\tikztotarget.south-|R) -|
([xshift=7pt]\tikztotarget.south west)}},
from/.style={<-,to path={([xshift=-7pt]\tikztostart.north east)
|- ([yshift=5mm]\tikztostart.north-|R) -- (S-|R)}},
target/.style={-,to path={(N-|R) -- ([yshift=-5mm]\tikztotarget.south-|R)
-| ([xshift=7pt]\tikztotarget.south west)}},
between/.style={-,to path={(N-|R) -- (S-|R)}}}
}
\newcommand{\ConnectTikZmarknodes}[3][]{\AddToShipoutPictureFG{%
\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
\def\pv##1{\pgfkeysvalueof{/tikz/connected tikzmarks/##1}}
\path
([yshift=-\pv{south margin}]current page text area.south) coordinate (S)
([yshift=\pv{north margin}]current page text area.north) coordinate (N)
([xshift=-\pv{right distance}]current page.east) coordinate (R)
;
\ifcase\myflag
\or
\draw[connected tikzmarks/from] (#2) to (#3);
\or
\draw[connected tikzmarks/target] (#2) to (#3);
\or
\draw[connected tikzmarks/same] (#2) to (#3);
\fi
\ifnum\value{page}>\pageA
\ifnum\value{page}<\pageB
\draw[connected tikzmarks/between] (#2) to (#3);
\fi
\fi
\end{tikzpicture}}}
\usepackage{lipsum}
\begin{document}
\ConnectTikZmarknodes{1}{2}
\ConnectTikZmarknodes{2}{3}
\ConnectTikZmarknodes{3}{4}
\ConnectTikZmarknodes[connected tikzmarks/loop up]{2}{4}
\ConnectTikZmarknodes[connected tikzmarks/loop up]{5}{6}
\lipsum[1-7]
\marginnote{\tikzmarknode{1}{Test Test}}
\lipsum[1][1-5]
\marginnote{\tikzmarknode{2}{Test Test}}
\lipsum[1] %
\marginnote{\tikzmarknode{3}{Test Test}}
\lipsum[1][1-4]
\marginnote{\tikzmarknode{4}{Test Test}}
\lipsum[1][1-8]
\marginnote{\tikzmarknode{5}{Test Test}}
\lipsum[1][1-4]
\marginnote{\tikzmarknode{6}{Test Test}}
\end{document}
