返回跨多个页面的TikZ箭头

返回跨多个页面的TikZ箭头

我想在边注之间添加箭头,我使用 \tikzmarknode 实现了这一点。我还修复了多个页面的问题(请参阅这个问题)。

然而,当我放入一个返回箭头来展示一个循环时,它不起作用,我无法得到它(图 2)

图1图 2

梅威瑟:

\documentclass[10pt,a4paper]{article}
\usepackage[left=32.5mm, right=25mm, top=25mm, bottom=20mm, marginparsep=3mm]{geometry}
\usepackage{marginnote}

\usepackage{tikz}
    \usetikzlibrary{tikzmark}

\usepackage{eso-pic}

\usepackage{tikzpagenodes}
    \makeatletter
    \newcommand\PageOfTikzmark[1]{%
    \csname save@pg@\csname save@pt@\tikzmark@pp@name{#1}\endcsname\endcsname}
    \makeatother
    \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 
        \ifcase\myflag
        \or
        \draw[-] ([xshift=7pt]#2.south west)
            to ([xshift=7pt]#2.south west|-current page text area.south);
        \or
        \draw[->] ([xshift=7pt]#3.north west|-current page text area.north) 
            to ([xshift=7pt]#3.north west);  
        \or
        \draw[->] ([xshift=7pt]#2.south west)
            to ([xshift=7pt]#3.north west);
        \fi
        \ifnum\value{page}>\pageA
        \ifnum\value{page}<\pageB
        \draw ([xshift=7pt]current page text area.north east) to
        ([xshift=7pt]current page text area.south east);
        \fi
        \fi
    \end{tikzpicture}}}

\usepackage{lipsum}

\begin{document}
    \ConnectTikZmarknodes{1}{2}
    \ConnectTikZmarknodes{2}{3}
    \ConnectTikZmarknodes{3}{4}

    \lipsum[1-7]
    \marginnote{\tikzmarknode{1}{Test Test}}
    \lipsum[1][1-5]
    \marginnote{\tikzmarknode{2}{Test Test}}
    \lipsum[1]  % <- doesn't work
    % \lipsum[1][1-4] % <- works
    \marginnote{\tikzmarknode{3}{Test Test}}
    \lipsum[1][1-4]
    \marginnote{\tikzmarknode{4}{Test Test}}
    \begin{tikzpicture}[remember picture, overlay, shorten >=1.5mm, shorten <=1.5mm]
        \draw[->,rounded corners] ([xshift=7pt]4.south west) |- ([shift={(6mm,-5mm)}]4.south east) -- ([shift={(6mm,5mm)}]2.north east) -| ([xshift=-7pt]2.north east);
    \end{tikzpicture}

\end{document}

答案1

好消息是,这或多或少包含在您链接的答案中,可以升级而不会失去向下兼容性。 只需将之前代码中固定的一些值变为 pgf 键。 然后,您可以为提供\ConnectTikZmarknodes一些选项,在其中更改to path以满足您的要求。

为了遵循惯例,我们将按时间顺序排列节点,即较早的节点将被称为 ,start较晚的节点将被称为target,尽管对于向后连接,箭头指向起始节点。这就是相应键有箭头的原因<-

正如链接的答案所示,有四种情况:

  1. 当前页面上没有任何节点。如果页面位于这些节点之间,则将使用样式绘制连接between
  2. 当前页面上的第一个节点。在这种情况下from将使用样式。
  3. 当前页面上的第二个节点。在这种情况下target将使用样式。
  4. 两个节点位于同一页面上。在这种情况下same将使用样式。

我们指出哪种风格适用于哪种情况。每种风格通常包含一个to path键,用于控制如何详细绘制连接。例如,

from/.style={-,to path={([xshift=7pt]\tikztostart.south west) --
([xshift=7pt]\tikztostart.south west|-S)}},

表示我们讨论的是情况 2。连接将从起始节点锚点西南方向右侧 7pt 处开始。它将转到([xshift=7pt]\tikztostart.south west|-S),其中|-节点中的语法表示“取 的 x 坐标[xshift=7pt]\tikztostart.south west和 的 y 坐标S,参见例如这个帖子进行讨论。S节点是位于文本区域底部下方的辅助节点,距离存储在中south margin。类似的语句适用于N位于north margin文本区域顶部上方的。坐标R位于right distance页面右边框的左侧,可用于设置向后循环的水平位置。

反向循环要做的就是改变这些样式的初始值。样式集合可以存储在一个样式中,loop up这里称为样式。给定这个样式,获取循环就像说的那样简单。

\ConnectTikZmarknodes[connected tikzmarks/loop up]{2}{4}

让我再次强调一下,尽管箭头从 到42但节点在命令中出现的顺序是按时间顺序排列的,即2在之前,4因为这个节点位于较早的页面上。

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

在此处输入图片描述

相关内容