增加 tikz 路径边缘的距离

增加 tikz 路径边缘的距离

因此,我在 Latex 中制作了一个图表,但我无法让节点的边缘以任何方式拉长。边缘的权重目前很难读取。我该如何解决这个问题?以下是我目前拥有的。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}

\begin{document}


\begin{tikzpicture}
\begin{scope}[every node/.style={circle,thick,draw}]
    \node (1) at (0,0) {1};
    \node (8) at (0,-1) {8};
    \node (5) at (1,-2) {5};
    \node (2) at (-1,-2) {2};
    \node (3) at (2,-3) {3};
    \node (4) at (0,-3) {4} ;
    \node (7) at (-1,-4) {7};
    \node (6) at (-2,-5) {6};
\end{scope}


\begin{scope}[>={Stealth[black]},
          every node/.style={fill=white, circle},
          every edge/.style={draw=black ,thick}]
    \path [->] (1) edge node {$120$} (8);
    \path [->] (8) edge node {$170$} (5);
    \path [->] (8) edge node {$155$} (2);
    \path [->] (5) edge node {$115$} (3);
    \path [->] (5) edge node {$160$} (4);
    \path [->] (4) edge node {$160$} (7);
    \path [->] (7) edge node {$175$} (6);
\end{scope}
\end{tikzpicture}


\end{document}

答案1

问题在于,您已经对节点的绝对位置进行了硬编码。这不是绘制此类图表的好方法。但是,如果您只想快速解决问题,则可以传递scale=<factor>给图片。例如:

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{scope}[every node/.style={circle,thick,draw},scale=3]
    \node (1) at (0,0) {1};
    \node (8) at (0,-1) {8};
    \node (5) at (1,-2) {5};
    \node (2) at (-1,-2) {2};
    \node (3) at (2,-3) {3};
    \node (4) at (0,-3) {4} ;
    \node (7) at (-1,-4) {7};
    \node (6) at (-2,-5) {6};
\end{scope}
\begin{scope}[>={Stealth[black]},
          every node/.style={fill=white, circle},
          every edge/.style={draw=black ,thick}]
    \path [->] (1) edge node {$120$} (8);
    \path [->] (8) edge node {$170$} (5);
    \path [->] (8) edge node {$155$} (2);
    \path [->] (5) edge node {$115$} (3);
    \path [->] (5) edge node {$160$} (4);
    \path [->] (4) edge node {$160$} (7);
    \path [->] (7) edge node {$175$} (6);
\end{scope}
\end{tikzpicture}
\end{document}

给出

比例图

我实际上可能会做的是使用 Forest 并编写如下内容:

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{arrows.meta}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={%
    circle,
    thick,
    draw,
    edge={thick,-{Stealth[]}},
    calign=fixed edge angles,
    l sep+=40pt
  },
  before typesetting nodes={%
    for tree={%
      edge label/.wrap value={node [fill=white, circle, midway] {$#1$}}
    }
  }
  [1
    [8, edge label=120
      [2, edge label=155]
      [5, edge label=170
        [4, edge label=160
          [7, edge label=160
            [6, edge label=175]
            [, phantom]
          ]
          [, phantom]
        ]
        [3, edge label=145]
      ]
    ]
  ]
\end{forest}
\end{document}

这里的巨大优势在于使用括号语法的简洁树规范以及更大的灵活性和自动化树的格式和内容的能力。

森林树

相关内容