在 Tikz 图中绘制节点之间的边

在 Tikz 图中绘制节点之间的边

我有一个 tikz 图像:

\begin{tikzpicture}[roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm}, squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=5mm},>=stealth]
\node[squarednode, rounded corners] (1) {Some Text};
\node[squarednode, rounded corners, right=of 1] (2) {Some Text};
\node[squarednode, rounded corners, right=of 2] (3) {Some Text};
\draw[->] (1) edge node {} (2);
\draw[->] (2) edge node {} (3);
\end{tikzpicture}

我想在第 3 个块和第 1 个块之间创建连接,如下所示。

在此处输入图片描述

我不知道如何让边缘从第 3 个块的右侧开始并在第 1 个块的左侧结束。

答案1

有几种方法可以实现这一点,所有这些都是你已经做过的方法的变体。你可以像这样使用带角的线条:

\documentclass[tikz,border=5pt]{standalone}

\usetikzlibrary{positioning,calc}

\begin{document}
    \begin{tikzpicture}[squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=5mm,rounded corners},>=stealth]
        \node[squarednode] (1) {Some Text};
        \node[squarednode, right=of 1] (2) {Some Text};
        \node[squarednode, right=of 2] (3) {Some Text};
        \draw[->] (1) -- (2);
        \draw[->] (2) -- (3);
        \draw[->] (3.east) -| ++(.5,-1) -- ($(1.west)+(-.5,-1)$) |- (1.west);
    \end{tikzpicture}
\end{document}

看起来像这样:

在此处输入图片描述

或者:你可以使用具有指定出口角和入口角的线,如下所示:

\documentclass[tikz,border=5pt]{standalone}

\usetikzlibrary{positioning,calc}

\begin{document}
    \begin{tikzpicture}[squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=5mm,rounded corners},>=stealth]
        \node[squarednode] (1) {Some Text};
        \node[squarednode, right=of 1] (2) {Some Text};
        \node[squarednode, right=of 2] (3) {Some Text};
        \draw[->] (1) -- (2);
        \draw[->] (2) -- (3);
        \draw[->] (3.east) to[out=-20,in=200] (1.west);
    \end{tikzpicture}
\end{document}

看起来像这样:

在此处输入图片描述

注意:使用出射角-入射角,图像两侧会出现多余的空白,因此您可能需要手动将clip图像调整到所需的尺寸。

编辑:要将图像剪辑到所需的大小,同样有很多方法可以做到这一点,但对我来说,最简单的方法是先draw在图像周围画一个矩形,以确保所有内容都在里面,然后用draw替换clip

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning,calc}

\begin{document}
    \begin{tikzpicture}[squarednode/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=5mm,rounded corners},>=stealth]
        \draw (-1.5,-1) rectangle (7.3,1); % replace \draw with \clip
        \node[squarednode] (1) {Some Text};
        \node[squarednode, right=of 1] (2) {Some Text};
        \node[squarednode, right=of 2] (3) {Some Text};
        \draw[->] (1) -- (2);
        \draw[->] (2) -- (3);
        \draw[->] (3.east) to[out=-20,in=200] (1.west);
    \end{tikzpicture}
\end{document}

相关内容