通过“空心”线连接 tikz 节点

通过“空心”线连接 tikz 节点

我想这有点牵强。下面的 MWE 产生以下输出:

在此处输入图片描述

我想用“空心”电线替换电线,使节点内部看起来像一个连续的区域,以便看起来更像这个模型:

在此处输入图片描述

重要的是我不想只画这一张图。这是我正在编写的库的一部分,所以我真的想制作一个命令,说“从这个坐标到另一个坐标画一条空心线”。坐标将始终放置在形状的垂直边缘上,就像在示例中一样。

如果有办法无缝地完成它,那么这样的事情会更好:

在此处输入图片描述

这是MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\node[draw,rounded corners=0.5mm] (a) at (0,0) {$A$};
\node[draw,rounded corners=0.5mm] (b) at (1.5,0.5) {$B$};
\coordinate (a out) at ([yshift=-1mm]a.east);
\coordinate (b in) at ([yshift=-1mm]b.west);
\draw (a out) to[out=0,in=180] (b in);
\end{tikzpicture}

\end{document}

答案1

您可以使用该选项,然后根据线条粗细(使用负值)double延长电线。shorten

在此处输入图片描述

以下是代码:

\documentclass{article}
\usepackage{tikz}
\tikzset{connector/.style={double, double distance=1mm, shorten <=-\pgflinewidth, shorten >=-\pgflinewidth}}

\begin{document}

\begin{tikzpicture}
\node[draw,rounded corners=0.5mm] (a) at (0,0) {$A$};
\node[draw,rounded corners=0.5mm] (b) at (1.5,0.5) {$B$};
\coordinate (a out) at ([yshift=-1mm]a.east);
\coordinate (b in) at ([yshift=-1mm]b.west);
\draw[connector] (a out) to[out=0,in=180] (b in);
\end{tikzpicture}

\end{document}

连接点处的细灰线是double施工过程中的产物。要消除它并不容易,但如果你知道连接点都沿着垂直线,你可以通过在它上面画一条短白线来掩盖它:

在此处输入图片描述

以下是代码:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
    connector/.style={
        double, double distance=1mm, shorten <=-\pgflinewidth, shorten >=-\pgflinewidth,
        postaction=decorate, decoration={show path construction, 
            curveto code={
                \draw[line width=1mm, white]([xshift=.2mm]\tikzinputsegmentlast)--++(.01,0);
                \draw[line width=1mm, white]([xshift=-.2mm]\tikzinputsegmentfirst)--++(-.01,0);
            }
            lineto code={
                \draw[line width=1mm, white]([xshift=.2mm]\tikzinputsegmentlast)--++(.01,0);
                \draw[line width=1mm, white]([xshift=-.2mm]\tikzinputsegmentfirst)--++(-.01,0);
            }
        }
    }
}

\begin{document}

\begin{tikzpicture}
\node[draw,rounded corners=0.5mm] (a) at (0,0) {$A$};
\node[draw,rounded corners=0.5mm] (b) at (1.5,0.5) {$B$};
\coordinate (a out) at ([yshift=-1mm]a.east);
\coordinate (b in) at ([yshift=-1mm]b.west);
\draw[connector] (a out) to[out=0,in=180] (b in);
\end{tikzpicture}

\end{document}

答案2

这是自我回答,以补充 Sandy G 的回答。那个答案很棒,但它继承了 的一个问题/tikz/double,即双线的末端并不是完全无缝的,因此您会看到非常细的黑线将线内与节点内分开。

一种解决方案是通过绘制一条黑线然后绘制一条白线来“伪造”加倍。然后白线可以稍微长一点,避免可见的接缝。以下是执行此操作的一些代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\node[draw,rounded corners=0.5mm] (a) at (0,0) {$A$};
\node[draw,rounded corners=0.5mm] (b) at (1.5,0.5) {$B$};
\coordinate (a out) at ([yshift=-1mm]a.east);
\coordinate (b in) at ([yshift=-1mm]b.west);
\draw[line width=1mm + 0.3mm,shorten <=-0.15mm,shorten >=-0.15mm] (a out) to[out=0,in=180] (b in);
\draw[white,line width=1mm,shorten <=-0.17mm,shorten >=-0.17mm] (a out) to[out=0,in=180] (b in);
\end{tikzpicture}

\end{document}

在此处输入图片描述

这段代码有一堆硬编码的数字,我不太喜欢,但我对 tikz 的了解又不足以避开它们。

原则上,应该可以使用带有预作用的样式来实现,如答案中所示这里,但我无法让它工作。

作为奖励,这是我要求的第二件事,使用相同的想法和背景层:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{tikzpicture}
\node[draw,rounded corners=0.5mm,fill=white] (a) at (0,0) {$A$};
\node[draw,rounded corners=0.5mm,fill=white] (b) at (1.5,0.5) {$B$};
\coordinate (a out) at ([yshift=1mm] a.south east);
\coordinate (b in) at ([yshift=1mm] b.south west);
\draw[white,line width=2mm-0.3mm,shorten <=-0.5mm,shorten >=-0.5mm] (a out) to[out=0,in=180] (b in);
\begin{scope}[on background layer]
    \draw[line width=2mm,shorten <=-0.5mm,shorten >=-0.5mm] (a out) to[out=0,in=180] (b in);
\end{scope}
\end{tikzpicture}

\end{document}

相关内容