![如何使用 tikz 获取节点之间的矩形线?](https://linux22.com/image/352998/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20tikz%20%E8%8E%B7%E5%8F%96%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E7%9F%A9%E5%BD%A2%E7%BA%BF%EF%BC%9F.png)
抱歉,如果重复了(我确定是重复了),但我真的找不到手动操作的方法。我已经创建了下一个区块链。
这里你可以看到最后一个块和第三个块之间的曲线。但我想要的是这样的:
问题是:我怎样才能以这种方式连接它们?我相信这很简单。MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,automata}
\begin{document}
\begin{tikzpicture}
\node[draw, thick, rectangle] (0) {Data arrival};
\node[draw, thick, rectangle, below of=0] (1) {Coks backoff procedure};
\node[draw, thick, rectangle, below of= 1] (2) {Get trigger-frame?};
\node[draw, thick, rectangle, below of= 2] (3) {Performs backoff procedure};
\node[draw, thick, shape aspect=2.7, diamond, below =0.5cm of 3] (4) {$b<0$?};
\path[>=latex, auto = right,every loop]
(0) edge[] node {} (1)
(1) edge node {} (2)
(2) edge node {} (3)
(3) edge node {} (4)
(4.east) edge[in=0, out=0, looseness=3] node[right] {N} (2.east)
;
\end{tikzpicture}
\end{document}
答案1
下面的代码使用以下路径构造。还有更多,请参阅 tikz 手册或任何互联网上有很多例子)。
(a) -- (b)
a
描述从到 的直线路径b
。(a) -| (b)
a
描述从到 的路径b
,首先水平移动直到低于或高于b
然后垂直移动。(a) |- (b)
是一样的,但是从垂直方向开始,然后继续水平方向。++(1,-2)
表示相对于前一个位置的位置,向右移动 1 个单位,向下移动 2 个单位。因此,(a) -| ++(1,-2)
路径从 开始a
,向右移动 1 个单位,向下移动 2 个单位。作为副作用,新的起始位置已移动到路径的末尾。因此\draw (a) -| ++(1,-2) -| ++(-1,2);
绘制一个矩形,其最终位置再次为
a
。+(1,-2)
和 基本相同++(1,-2)
,只是位置不动。\draw (a) -| +(1,-2) -| +(-1,2);
绘制两条各有一定角度的线,一条在 的右边
a
,一条在 的左边a
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,automata}
\begin{document}
\begin{tikzpicture}
[>=latex,
action/.style={draw,thick},
test/.style={draw, thick, shape aspect=2.7, diamond}
]
\node[action] (0) {Data arrival};
\node[action, below=of 0] (1) {Coks backoff procedure};
\node[action, below=of 1] (2) {Get trigger-frame?};
\node[action, below=of 2] (3) {Performs backoff procedure};
\node[test, below= 0.5cm of 3] (4) {$b<0$?};
\node[action, left=of 3] (5) {Do something};
\path[->]
(0) edge node {} (1)
(1) edge node {} (2)
(2) edge node {} (3)
(3) edge node {} (4);
\draw[->] (4) -- node[below right,pos=0.2]{N} ++(3,0) |- (2);
\draw[->] (4) -| node[below left,pos=0.1]{Y} (5);
\draw[->] (5) |- (2);
\draw[->] (4) --node[right] {maybe} +(0,-1.5);
\end{tikzpicture}
\end{document}
答案2
略有修改格诺特答案(为了快乐和锻炼):
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows, chains, positioning, shapes}% added chains
\makeatletter
\tikzset{supress chain/.code={\def\tikz@after@path{}}}% added for suppress joining of nodes
\makeatother
\begin{document}
\begin{tikzpicture}[
> = latex,
node distance = 5mm and 7mm,% added (not used default value)
start chain = going below,% activation of chains
action/.style = {draw, thick, on chain, join= by ->},% nodes are in chain and connected by ->
test/.style = {diamond, draw, thick, shape aspect=2.4, on chain, join= by ->}% node is in the chain and connected by -> with previous node
]
\node[action] (n0) {Data arrival};
\node[action] (n1) {Coks backoff procedure};
\node[action] (n2) {Get trigger-frame?};
\node[action] (n3) {Performs backoff procedure};
\node[test] (n4) {$b<0$?};
\node[action,
supress chain, % this node is not connected with join
left=of n3] (n5) {Do something};
\draw[->] (n4) -| node[below,pos=0.25] {Y} (n5); % left feedback loop
\draw[->] (n5) |- (n0); % left feedback loop
\draw[->] (n4) -| ([xshift=5mm] n3.east) node[below,pos=0.25] {N} |- (n2); % right feedback loop
\draw[->] (n4.south) -- node[right] {maybe} ++ (0,-1.1);
\end{tikzpicture}
\end{document}
结果几乎是一样的: