不久前,我偶然发现了一个用 TikZ 绘制平行路径的非常好的解决方案,可以在这里找到:在 TikZ 中绘制额外的平行路径。
shorten >=
然而,当我尝试将其用于我的目的时(即与和选项结合使用shorten <=
),我发现此解决方案的表现并不像预期的那样。这是一个 MWE:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{positioning}
\def\pgfdecoratedcontourdistance{0pt}
\pgfkeys{/pgf/decoration/contour distance/.code={%
\pgfmathparse{#1}%
\let\pgfdecoratedcontourdistance=\pgfmathresult}%
}
\pgfdeclaredecoration{contour lineto}{start}
{
\state{start}[next state=draw, width=0pt]{
\pgfpathmoveto{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
}
\state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{
\pgfmathparse{-\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
\let\shorten=\pgfmathresult%
\pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}%
}
\state{final}{
\pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength}{\pgfdecoratedcontourdistance}}%
}
}
\begin{document}
\tikzset{
box/.style={draw=black, fill=blue!20, inner sep=1em},
arrow/.style={->,draw=black, shorten >=1mm,shorten <=1mm},
quintuple/.style={
postaction={decoration={contour lineto, contour distance=-4pt},draw=black, decorate},
postaction={decoration={contour lineto, contour distance=-2pt},draw=black, decorate},
postaction={decoration={contour lineto, contour distance=+2pt},draw=black, decorate},
postaction={decoration={contour lineto, contour distance=+4pt},draw=black, decorate},
},
}
\begin{tikzpicture}
\node[box] (box1) {Box 1};
\node[box,above right=of box1] (box2) {Box 2};
\draw[arrow,quintuple] (box1) |- (box2);
\end{tikzpicture}
\end{document}
生成的图片如下所示:
箭头末端全都弄乱了,我不明白为什么会这样。有人能提供一个解决方案,让箭头末端看起来漂亮吗(或者至少解释一下这里发生了什么)?
答案1
正是final
状态把事情搞乱了,但在您的情况下,这种final
状态是不需要的,因为您不需要或不希望添加的路径在与基本路径相同的点结束。
只需删除状态声明final
,一切就会按我们想要的方式运行:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{positioning}
\def\pgfdecoratedcontourdistance{0pt}
\pgfkeys{/pgf/decoration/contour distance/.code={%
\pgfmathparse{#1}%
\let\pgfdecoratedcontourdistance=\pgfmathresult}%
}
\pgfdeclaredecoration{contour lineto}{start}
{
\state{start}[next state=draw, width=0pt]{
\pgfpathmoveto{\pgfpoint{0pt}{\pgfdecoratedcontourdistance}}%
}
\state{draw}[next state=draw, width=\pgfdecoratedinputsegmentlength]{
\pgfmathparse{-\pgfdecoratedcontourdistance*cot(-\pgfdecoratedangletonextinputsegment/2+90)}%
\let\shorten=\pgfmathresult%
\pgfpathlineto{\pgfpoint{\pgfdecoratedinputsegmentlength+\shorten}{\pgfdecoratedcontourdistance}}%
}
}
\begin{document}
\tikzset{
box/.style={draw=black, fill=blue!20, inner sep=1em},
arrow/.style={->,draw=black, shorten >=1mm,shorten <=1mm},
quintuple/.style={
postaction={decoration={contour lineto, contour distance=-4pt},draw=black, decorate},
postaction={decoration={contour lineto, contour distance=-2pt},draw=black, decorate},
postaction={decoration={contour lineto, contour distance=+2pt},draw=black, decorate},
postaction={decoration={contour lineto, contour distance=+4pt},draw=black, decorate},
},
}
\begin{tikzpicture}
\node[box] (box1) {Box 1};
\node[box,above right=of box1] (box2) {Box 2};
\draw[arrow,quintuple] (box1) |- (box2);
\draw[arrow,quintuple] (box1) -| (box2);
\end{tikzpicture}
\end{document}