使用\ draw语法向TikZ添加边缘权重

问题描述 投票:0回答:1

我使用TikZ得到以下图表:

\begin{tikzpicture}
        \tikzset{vertex/.style = {shape=circle,draw,minimum size=2em}}
        \tikzset{edge/.style = {->,> = latex'}}
        % Vertices
        \node[vertex] (s) at (0,0) {s};
        \node[vertex] (t) at (2,-2) {t};
        \node[vertex] (v) at (-2,-2) {v};
        \node[vertex] (w) at (2,-5) {w};
        \node[vertex] (u) at (-2,-5) {u};
        % Edges
        \draw[edge, ultra thick] (s) to (t);
        \draw[edge, ultra thick] (s) to (v);
        \draw[edge, ultra thick] (t) to (w);
        \draw[edge, ultra thick] (v) to (u);
        \draw[edge] (t) to (u);
        \draw[edge] (v) to (w);
\end{tikzpicture}

enter image description here

使用这种语法向该图添加边缘权重的最佳方法是什么?

latex tikz
1个回答
0
投票

您可以简单地将节点添加到路径。可以通过以下任一方式完成

\draw (x) -- (y) node [midway] {w} ;

如果您使用标准语法绘制边缘。或与

\draw (x) -- to node[] {w} (y)  ;

如果您喜欢'收件人'表格。

节点的正常位置是直线的正中,但是您可以添加任何参数来调整节点的位置(左,右,上方,下方,x / yshift)。

此节点也可以遵循边缘方向(倾斜)。在这种情况下,参数是相对于边缘方向定义的,您将主要在下面的上方使用。

这里是正常或倾斜权重的示例。我同时使用了“至”和“-”形式。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{tikzpicture}[
  vertex/.style = {shape=circle,draw,minimum size=2em},
  edge/.style = {->,-Latex},
  ]
  % Vertices
  \node[vertex] (s) at (0,0) {s};
  \node[vertex] (t) at (2,-2) {t};
  \node[vertex] (v) at (-2,-2) {v};
  \node[vertex] (w) at (2,-5) {w};
  \node[vertex] (u) at (-2,-5) {u};
  % Edges
  \draw[edge, ultra thick] (s) -- (t) node[midway,right] {$w_{st}$}  ;
  \draw[edge, ultra thick] (s) to node[left] {$w_{sv}$} (v);
  \draw[edge, ultra thick] (t) to node[right] {$w_{tw}$} (w);
  \draw[edge, ultra thick] (v) to  node[left] {$w_{vu}$} (u);
  \draw[edge] (t) to node[above, xshift=8mm] {$w_{tu}$} (u);
  \draw[edge] (v) to node[above, xshift=-8mm]  {$w_{vw}$} (w);
\end{tikzpicture}
\hfill%
\begin{tikzpicture}[
  vertex/.style = {shape=circle,draw,minimum size=2em},
  edge/.style = {->,-Latex},
  ]
  % Vertices
  \node[vertex] (s) at (0,0) {s};
  \node[vertex] (t) at (2,-2) {t};
  \node[vertex] (v) at (-2,-2) {v};
  \node[vertex] (w) at (2,-5) {w};
  \node[vertex] (u) at (-2,-5) {u};
  % Edges
  \draw[edge, ultra thick] (s) -- (t) node[midway,above,sloped] {$w_{st}$}  ;
  \draw[edge, ultra thick] (s) to node[above,sloped] {$w_{sv}$} (v);
  \draw[edge, ultra thick] (t) to node[above,sloped] {$w_{tw}$} (w);
  \draw[edge, ultra thick] (v) to  node[below,sloped] {$w_{vu}$} (u);
  \draw[edge] (t) to node[above left, sloped] {$w_{tu}$} (u);
  \draw[edge] (v) to node[above right, sloped]  {$w_{vw}$} (w);
\end{tikzpicture}
\end{document}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.