调整 TikZ 中的边缘路径以避免重叠节点和其他边缘

调整 TikZ 中的边缘路径以避免重叠节点和其他边缘

下面的代码

\begin{tikzpicture}[auto,node distance=2cm,>=latex]

  % Posizionamento manuale dei vertici
  \node[circle,draw] (A) at (1,2) {A};
  \node[circle,draw] (B) at (3,2) {B};
  \node[circle,draw] (C) at (5,2) {C};
  \node[circle,draw] (D) at (4,0) {D};
  \node[circle,draw] (E) at (0,-1) {E};
  \node[circle,draw] (F) at (2,0) {F};
  \node[circle,draw] (S) at (-0.5,1) {S};

  % Disegno degli archi con pesi
  \draw (A) -- node {$2$} (B);
  \draw (A) -- node {$2$} (S);
  \draw (S) to[out=90,in=120] node[above] {$3$} (B);
  \draw (S) -- node {$1$} (F);
  \draw (S) -- node {$4$} (E);
  \draw (E) -- node {$5$} (F);
  \draw (E) to[out=0,in=220] node[above] {$2$} (D);
  \draw (F) -- node {$3$} (D);
  \draw (C) -- node {$3$} (D);
  \draw (C) -- node {$3$} (B);
  \draw (B) -- node {$6$} (D);
  \draw (B) to[out=90,in=120] node[above] {$1$} (E);
\end{tikzpicture}

生成以下图表:

在此处输入图片描述

仔细检查后,很明显从 B 到 E 的边与其他边和节点 A 重叠。我正在寻求有关如何将这个特定边重新路由到节点 S 和 A 左侧的指导,以防止与其他边和节点重叠。任何有关实现此目的的帮助都将不胜感激。

答案1

解决方案是使用 键looseness=2执行命令。同时更改\drawout和。添加选项是为了使边界框不会变得太大。inoverlay

在此处输入图片描述

\documentclass[border=6pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[auto,node distance=2cm,>=latex]

  % Posizionamento manuale dei vertici
  \node[circle,draw] (A) at (1,2) {A};
  \node[circle,draw] (B) at (3,2) {B};
  \node[circle,draw] (C) at (5,2) {C};
  \node[circle,draw] (D) at (4,0) {D};
  \node[circle,draw] (E) at (0,-1) {E};
  \node[circle,draw] (F) at (2,0) {F};
  \node[circle,draw] (S) at (-0.5,1) {S};

  % Disegno degli archi con pesi
  \draw (A) -- node {$2$} (B);
  \draw (A) -- node {$2$} (S);
  \draw (S) to[out=90,in=120] node[above] {$3$} (B);
  \draw (S) -- node {$1$} (F);
  \draw (S) -- node {$4$} (E);
  \draw (E) -- node {$5$} (F);
  \draw (E) to[out=0,in=220] node[above] {$2$} (D);
  \draw (F) -- node {$3$} (D);
  \draw (C) -- node {$3$} (D);
  \draw (C) -- node {$3$} (B);
  \draw (B) -- node {$6$} (D);
  \draw[looseness=2,overlay] (B) to[out=110,in=130] node[above] {$1$} (E);
\end{tikzpicture}
\end{document}

答案2

通过使用bboxquotes库,图像代码变得更短、更清晰,并且生成的图像更加漂亮(周围没有多余的空白):

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{bbox,
                quotes}

\begin{document}
    \begin{tikzpicture}[bezier bounding box,
                C/.style = {circle, draw, inner sep=2pt},
every edge quotes/.style = {auto, font=\footnotesize, inner sep=2pt},
                        ]
% Posizionamento manuale dei vertici
    \begin{scope}[nodes=C]
\node (A) at (1, 2) {A};
\node (B) at (3, 2) {B};
\node (C) at (5, 2) {C};
\node (D) at (4, 0) {D};
\node (E) at (0,-1) {E};
\node (F) at (2, 0) {F};
\node (S) at (-.5,1){S};
    \end{scope}
% Disegno degli archi con pesi
\draw   (A) edge["$2$"] (S) 
        (A) edge["$2$"] (B)
        (S) edge[bend left=60,"$3$"] (B) 
        (S) edge["$1$"] (F) 
        (S) edge["$4$"] (E) 
        (E) edge["$5$"] (F)
        (E) edge[bend right,"$2$"] (D)
        (E) edge[bend left=90, looseness=2, "$1$"] (B) 
        (F) edge["$3$"] (D) 
        (C) edge["$3$"] (D) 
        (C) edge["$3$"] (B) 
        (B) edge["$6$"] (D);
\end{tikzpicture}
\end{document}

顺便说一句,在您的代码片段中,您定义了箭头,但图中未使用箭头。因此,有点不清楚,您的图是有向的(边缘有箭头)还是无向的。我的 MWE 考虑后一种情况:

在此处输入图片描述

相关内容