下图中有两个节点,s 和 e,我尝试将它们绘制在两个节点的中间,但位于上方或下方。我尝试了各种方法above right of=1, xshift=-1.2cm
,但below right =3.4cm and 2.4cm of 2
没有一种看起来完全令人满意。我确信有比我使用的反复试验更好的方法。
\documentclass[border=1cm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,fit,automata}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=6cm,
semithick]
\tikzstyle{every state}=[circle, black, draw=red, very thick]
\node[state] (1) {$1$};
\node[state] (2) [below of=1,yshift=1cm] {$2$};
\node[state] (3) [right of=1] {$3$};
\node[state] (4) [below of=3,yshift=1cm] {$4$};
\node[state] (s) [above right of=1, xshift=-1.2cm] {$s$};
\node[state] (e) [below right =3.4cm and 2.4cm of 2] {$e$};
\path (1) edge node [sloped,above] {6, 6} (2)
(4) edge node[sloped,above,near start] {2, 2} (1)
(1) edge node[sloped,above] {7, 4} (3)
(2) edge node[sloped,above ] {5, 6} (4)
(3) edge node[sloped,above,near start] {4, 4} (2)
(3) edge node [sloped,above] {3, 6} (4)
(s) edge node[sloped,above] {8, 0} (1)
(s) edge node[sloped,above] {6, 0} (3)
(2) edge node[sloped,above] {4, 0} (e)
(4) edge node[sloped,above] {9, 0} (e);
\end{tikzpicture}
\end{document}
答案1
看看,以下对 MWE 的修改是否符合您的要求。主要变化如下:
- 使用
calc
图书馆 - 使用
positioning
语法 - 使用
on grid
选择权
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata,
calc,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[>=Stealth, auto,
node distance = 2*\dis, on grid,
every state/.style = {circle, draw=red, very thick,
minimum size=2em, inner sep=0pt},
every edge/.style = {draw, semithick, -Stealth, shorten >=1pt},
every edge quotes/.style = {sloped, font=\small}
]
\def\dis{3cm}
\begin{scope}[nodes=state]
\node (n1) {$1$};
\node (n2) [below=of n1] {$2$};
\node (n3) [right=of n1] {$3$};
\node (n4) [below=of n3] {$4$};
\node (s) [above=\dis of $(n1)!0.5!(n3)$] {$s$};
\node (e) [below=\dis of $(n2)!0.5!(n4)$] {$e$};
\end{scope}
\path (n1) edge ["{6, 6}"] (n2)
(n1) edge ["{7, 4}"] (n3)
(n2) edge ["{5, 6}"] (n4)
(n2) edge ["{4, 0}"] (e)
(n3) edge [near start,"{4, 4}"] (n2)
(n3) edge ["{3, 6}"] (n4)
(n4) edge [near start,"{4, 4}"] (n1)
(n4) edge ["{9, 0}"] (e)
(s) edge ["{8, 0}"] (n1)
(s) edge ["{6, 0}"] (n3);
\end{tikzpicture}
\end{document}
答案2
那么使用 TikZ 怎么样matrix
?
\documentclass[border=1cm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata, matrix}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[->, >=Stealth, shorten >=1pt, semithick,
every state/.style={circle, black, draw=red, very thick},
descr/.style={sloped,above}]
\matrix[matrix of nodes, nodes={state},
column sep =2cm, row sep =4cm]
{&|[name=s]|$s$\\
|[name=1]|$1$&&|[name=3]|$3$\\
|[name=2]|$2$&&|[name=4]|$4$\\
&|[name=e]|$e$\\};
\path (1) edge node[descr] {6, 6} (2)
(4) edge node[descr,near start] {2, 2} (1)
(1) edge node[descr] {7, 4} (3)
(2) edge node[descr] {5, 6} (4)
(3) edge node[descr,near start] {4, 4} (2)
(3) edge node[descr] {3, 6} (4)
(s) edge node[descr] {8, 0} (1)
(s) edge node[descr] {6, 0} (3)
(2) edge node[descr] {4, 0} (e)
(4) edge node[descr] {9, 0} (e);
\end{tikzpicture}
\end{document}
您还可以为某些行选择不同的行分隔符:
\documentclass[border=1cm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, automata, matrix}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[->, >=Stealth, shorten >=1pt, semithick,
every state/.style={circle, black, draw=red, very thick},
descr/.style={sloped,above}]
\matrix[matrix of nodes, nodes={state},
column sep =2cm, row sep =4cm]
{&|[name=s]|$s$\\[-2cm]
|[name=1]|$1$&&|[name=3]|$3$\\
|[name=2]|$2$&&|[name=4]|$4$\\[-2cm]
&|[name=e]|$e$\\};
\path (1) edge node[descr] {6, 6} (2)
(4) edge node[descr,near start] {2, 2} (1)
(1) edge node[descr] {7, 4} (3)
(2) edge node[descr] {5, 6} (4)
(3) edge node[descr,near start] {4, 4} (2)
(3) edge node[descr] {3, 6} (4)
(s) edge node[descr] {8, 0} (1)
(s) edge node[descr] {6, 0} (3)
(2) edge node[descr] {4, 0} (e)
(4) edge node[descr] {9, 0} (e);
\end{tikzpicture}
\end{document}
答案3
Zarko 的答案的另一种选择是,仍然使用calc
库将节点精确定位在其他节点之间(因为我认为这是这里的主要问题)。
我尽可能地保留了您的代码,只添加了一些空格并删除了生成此示例不需要的库和包。
我还删除了该\tikzstyle
命令并将其选项合并到其他选项中,因为这似乎被视为当前标准。
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, calc}
\begin{document}
\begin{tikzpicture}[->, >=stealth', shorten >=1pt, node distance=6cm, semithick,
every state/.style={circle, black, draw=red, very thick}]
\node[state] (1) {$1$};
\node[state] (2) [below of=1, yshift=1cm] {$2$};
\node[state] (3) [right of=1] {$3$};
\node[state] (4) [below of=3, yshift=1cm] {$4$};
\node[state] (s) at ([yshift=3cm]$(1)!0.5!(3)$) {$s$};
\node[state] (e) at ([yshift=-3cm]$(2)!0.5!(4)$) {$e$};
\path (1) edge node[sloped, above] {6, 6} (2)
(4) edge node[sloped, above, near start] {2, 2} (1)
(1) edge node[sloped, above] {7, 4} (3)
(2) edge node[sloped, above] {5, 6} (4)
(3) edge node[sloped, above, near start] {4, 4} (2)
(3) edge node[sloped, above] {3, 6} (4)
(s) edge node[sloped, above] {8, 0} (1)
(s) edge node[sloped, above] {6, 0} (3)
(2) edge node[sloped, above] {4, 0} (e)
(4) edge node[sloped, above] {9, 0} (e);
\end{tikzpicture}
\end{document}