我正在尝试为 tikzcd 中的箭头创建一种格式,该格式实际上是拉长的波浪号:
我使用选项 1 绘制了这个例子这个答案,但我必须手动计算蛇的正确段长度。
有没有办法自动计算这个?例如:
arrows={decorate, decoration={snake, segment length=DISTANCE/2, amplitude=0.5mm}}
如果没有的话,还有其他方法吗?
任何帮助将非常感激。
答案1
我有一个基于的部分解决方案这个答案。
我复制了他们的辅助宏的全部内容,以及样式定义的大部分前半部分。我唯一负责的代码是注释“绘制波浪”下方的代码:
\makeatletter
% This helper macro finds the start and endpoints of a line between the source and target nodes and stores them in \sourcecoordinate and \targetcoordinate.
% #1 -- source node
% #2 -- target node
\def\findedgesourcetarget#1#2{
\let\sourcecoordinate\pgfutil@empty
\ifx\tikzcd@startanchor\pgfutil@empty % Check that the source doesn't have a specified anchor
\def\tempa{\pgfpointanchor{#1}{center}}% if so, start by taking the center of that coordinate
\else
\edef\tempa{\noexpand\pgfpointanchor{#1}{\expandafter\@gobble\tikzcd@startanchor}} % If it has an anchor, use that
\let\sourcecoordinate\tempa
\fi
\ifx\tikzcd@endanchor\pgfutil@empty % check that the target doesn't have a specified anchor
\def\tempb{\pgfpointshapeborder{#2}{\tempa}}% if so, our end point is the point on the boundary of node b that is in the direction of our initial start coordinate
\else
\edef\tempb{\noexpand\pgfpointanchor{#2}{\expandafter\@gobble\tikzcd@endanchor}}% If it has a specified anchor, use that
\fi
\let\targetcoordinate\tempb
\ifx\sourcecoordinate\pgfutil@empty%
\def\sourcecoordinate{\pgfpointshapeborder{#1}{\tempb}}%
\fi
}
\tikzset{wave/.style = {
-,
to path={\pgfextra{
\findedgesourcetarget{\tikzcd@ar@start}{\tikzcd@ar@target} % find endpoints
% Rotate coordinate system so that line goes in x direction
\ifx\tikzcd@startanchor\pgfutil@empty
\def\tikzcd@startanchor{.center}
\fi
\ifx\tikzcd@endanchor\pgfutil@empty
\def\tikzcd@endanchor{.center}
\fi
\pgfmathanglebetweenpoints{\pgfpointanchor{\tikzcd@ar@start}{\expandafter\@gobble\tikzcd@startanchor}}{\pgfpointanchor{\tikzcd@ar@target}{\expandafter\@gobble\tikzcd@endanchor}}
\pgftransformrotate{\pgfmathresult}
% Draw the wave
\newdimen\xdiff
\pgfextractx{\xdiff}{\pgfpointdiff{\sourcecoordinate}{\targetcoordinate}}
\newdimen\ydiff
\pgfextracty{\ydiff}{\pgfpointdiff{\sourcecoordinate}{\targetcoordinate}}
\newdimen\finalDist
\pgfmathparse{abs(veclen(\xdiff,\ydiff))*0.85}
\pgfmathsetlength\finalDist{\pgfmathresult pt}
\pgfmathsetlength\pgfdecorationsegmentlength{\finalDist}
\pgfmathparse{0.038*\finalDist+0.6}
\pgfmathsetlength\pgfdecorationsegmentamplitude{\pgfmathresult pt}
\pgfsetarrows{->}
\pgfpathmoveto{\sourcecoordinate}
\pgfpathsnaketo{snake}{\targetcoordinate}
\pgfusepath{stroke}
}}}}
\makeatother
使用辅助宏,我检索要连接的点并计算它们之间的距离。然后我使用它来定义蛇的幅度和段长度。
我在这些定义中使用的常数看起来是任意的,我是通过试验和改进找到它们的,但我会采用一种更严格的方法。
然后
\begin{center}
Original method:
\begin{tikzcd}[arrows={decorate, decoration={snake,segment length=7.3mm, amplitude=0.5mm}}]
A \arrow[r,"",decorate=true] & B
\end{tikzcd}
\end{center}
\hspace{2cm}
\begin{center}
Custom style
\begin{tikzcd}
A \arrow[r, wave] & B & & \\
A \arrow[rr, wave] & & B & \\
A \arrow[rrr, wave] & & & B
\end{tikzcd}
\end{center}
我目前的主要问题是箭头上方的标签不再起作用。我会尝试修复此问题,并尽可能更新此答案。
如果有人有更简单的方法可以避免此类问题,请告诉我!