已给出:下面的代码片段将绘制一个不相交边的图形。
\usepackage{pgf}
\usetikzlibrary{automata, positioning, arrows}
\begin{document}
\begin{tikzpicture}[->,node distance=3cm, initial text=$ $]
\node[initial, state] (A) {$0$};
\node[state] (C) [right of=A] {$2$};
\node[state] (D) [above of=C] {$3$};
\node[state] (E) [right of=D] {$4$};
\node[state] (F) [below of=E] {$5$};
\node[state, accepting] (G) [right of=F] {$6$};
\path (A) edge [above] (C);
\path (C) edge [above] (D);
\path (D) edge [above] (E);
\path (E) edge [above] (F);
\path (F) edge [above] (G);
\path (A) edge [bend left] (G);
\end{tikzpicture}
结果:
从 A 到 G 的边与其他边相交。
问题:
如何通过节点 3 和 4 弯曲的箭头来避免这种情况?
答案1
欢迎!主要问题可以通过链接bend left
到例如来回答bend right
。我写这篇文章是为了通知您,即使您正在加载库positioning
,您也不会使用它。如果您想使用它(我认为您应该使用它),请更改
right of=
到
right=<distance> of
其中<distance>
是可选的,如果未指定,则设置为node distance
。您也正在加载但未使用该arrows
库。如果您想访问更多箭头,请arrows.meta
改为加载。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{automata, positioning}
\begin{document}
\begin{tikzpicture}[->,node distance=2.2cm, initial text=\empty]
\node[initial, state] (A) {$0$};
\node[state] (C) [right=of A] {$2$};
\node[state] (D) [above=of C] {$3$};
\node[state] (E) [right=of D] {$4$};
\node[state] (F) [below=of E] {$5$};
\node[state, accepting] (G) [right=of F] {$6$};
\path (A) edge [above] (C)
(C) edge [above] (D)
(D) edge [above] (E)
(E) edge [above] (F)
(F) edge [above] (G)
(A) edge [bend right] (G);
\end{tikzpicture}
\end{document}
附录:可以改变弯曲角度和松紧度。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{automata, positioning}
\begin{document}
\begin{tikzpicture}[->,node distance=2.2cm, initial text=\empty]
\node[initial, state] (A) {$0$};
\node[state] (C) [right=of A] {$2$};
\node[state] (D) [above=of C] {$3$};
\node[state] (E) [right=of D] {$4$};
\node[state] (F) [below=of E] {$5$};
\node[state, accepting] (G) [right=of F] {$6$};
\path (A) edge [above] (C)
(C) edge [above] (D)
(D) edge [above] (E)
(E) edge [above] (F)
(F) edge [above] (G)
(A) edge [bend left=80,looseness=1.7] (G);
\end{tikzpicture}
\end{document}
如果您担心边界框估计过高,请使用该bbox
库。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{automata, positioning,bbox}
\begin{document}
\begin{tikzpicture}[->,node distance=2.2cm, initial text=\empty,bezier
bounding box]
\node[initial, state] (A) {$0$};
\node[state] (C) [right=of A] {$2$};
\node[state] (D) [above=of C] {$3$};
\node[state] (E) [right=of D] {$4$};
\node[state] (F) [below=of E] {$5$};
\node[state, accepting] (G) [right=of F] {$6$};
\path (A) edge [above] (C)
(C) edge [above] (D)
(D) edge [above] (E)
(E) edge [above] (F)
(F) edge [above] (G)
(A) edge [bend left=80,looseness=1.7] (G);
\end{tikzpicture}
\end{document}