下面的代码生成的图很难解释,因为节点和边是重叠的。我参考了另一个Stackchange 帖子寻找解决方案,但在这种情况下似乎不起作用。有人能解释一下如何防止边相互重叠或与节点重叠,以使图更具可读性吗?
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 7mm and 12mm,
V/.style = {circle, draw, font=\sffamily, minimum size=2em, inner sep =2pt},
every edge/.style = {draw, -{Straight Barb[scale=0.8]}, shorten > = 1mm},
every edge quotes/.style = {auto, font=\small\sffamily, sloped}
]
\node[V] (s) {s};
\node[V, right=of s] (j4) {j4};
\node[V, above =of j4] (j3) {j3};
\node[V, above =of j3] (j2) {j2};
\node[V, above =of j2] (j1) {j1};
\node[V, below =of j4] (j5) {j5};
\node[V, below =of j5] (j6) {j6};
\node[V, below =of j6] (j7) {j7};
\node[V, right=of j4] (t) {t};
\path (s) edge [red, "30"] (j1)
(s) edge [red, "40"] (j3)
(s) edge [red, "5"] (j7)
(s) edge [blue, "13"] (j4)
(s) edge [blue, "3"] (j5)
(s) edge [blue, "18"] (j6)
(s) edge [green, "14"] (j5)
(s) edge [black, "18"] (j2)
(s) edge [black, "15"] (j7)
(j3) edge [red, "1"] (j1)
(j7) edge [red, "1"] (j4)
(j6) edge [blue, "2"] (j3)
(j5) edge [green, "2"] (j3)
(j2) edge [black, "1"] (j1)
;
\end{tikzpicture}
\end{document}
答案1
我(再次)猜测你可能正在寻找:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 7mm and 12mm,
V/.style = {circle, draw, font=\sffamily, minimum size=2em, inner sep =2pt},
every edge/.style = {draw, -{Straight Barb[scale=0.8]}, shorten > = 1mm},
every edge quotes/.style = {auto, inner sep = 1pt, font=\footnotesize\sffamily, sloped}
]
\node[V] (s) {s};
\node[V, right=of s] (j4) {j4};
\node[V, above =of j4] (j3) {j3};
\node[V, above =of j3] (j2) {j2};
\node[V, above =of j2] (j1) {j1};
\node[V, below =of j4] (j5) {j5};
\node[V, below =of j5] (j6) {j6};
\node[V, below =of j6] (j7) {j7};
\node[V, right=of j4] (t) {t};
\foreach \i/\j [count=\k] in {30/red, 18/black, 40/red,
13/blue, 14/green, 18/blue,
15/black}
\path (s) edge [\j, "\i"] (j\k);
\begin{scope}[bend right=45]
\path (j3) edge [red, "1"] (j1)
(j7) edge [red, "1"] (j4)
(j6) edge [blue, "2"] (j3)
(j5) edge [green, "2"] (j3)
(j2) edge [black, "1"] (j1);
\end{scope}
\end{tikzpicture}
\end{document}
附录:
- 对于较短的代码,在循环 中绘制从节点
s
到节点的箭头j...
foreach
- 可以使用
chgains
库进一步缩短代码:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
chains,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 7mm and 12mm,
start chain = going below,
V/.style = {circle, draw, font=\sffamily, minimum size=2em, inner sep =2pt},
every edge/.style = {draw, -{Straight Barb[scale=0.8]}, shorten > = 1mm},
every edge quotes/.style = {auto, inner sep = 1pt,
font=\footnotesize\sffamily, sloped}
]
\foreach \i in {1,2,...,7}
\node[V, on chain] (j\i) {j4};
\node[V, left=of j4] (s) {s};
\node[V, right=of j4] (t) {t};
\foreach \i/\j [count=\k] in {30/red, 18/black, 40/red,
13/blue, 14/green, 18/blue,
15/black}
\path (s) edge [\j, "\i"] (j\k);
\begin{scope}[bend right=45]
\path (j3) edge [red, "1"] (j1)
(j7) edge [red, "1"] (j4)
(j6) edge [blue, "2"] (j3)
(j5) edge [green, "2"] (j3)
(j2) edge [black, "1"] (j1);
\end{scope}
\end{tikzpicture}
\end{document}
编译结果和以前相同。