在 TikZ 中标记边缘

在 TikZ 中标记边缘

我是 TikZ(和 LaTeX)的新手,我正在尝试创建一个带有节点的图形。

我一直在阅读手册,并使用图片中相同的方法创建了一个图表。我现在想标记我的边,但当我尝试像手册中那样标记它们时,它不起作用。

手册.pdf

我的代码如下。请帮忙!!

\begin{tikzpicture}
  [node/.style = {circle,draw}]
  \node[node] (B) {B};
  \node[node] (A) [below of=B, xshift = -20mm] {A}
    edge [-]  {1} (B); % would like to label this edge "1"
  \node[node] (C) [below of=B] {C}
    edge [-] {1} (A); % would like to label this edge "1"
  \node[node] (D) [below of=C] {D}
    edge [-] {1} (A); % would like to label this edge "1"
  \node[node] (E) [below of=B, xshift =  20mm] {E}
    edge [-] {1} (B) % would like to label this edge "1"
    edge [-] {1} (C) % would like to label this edge "1"
    edge [-] {1} (D); % would like to label this edge "1"
\end{tikzpicture}

答案1

一种方法是使用quotes库。(如果使用babel包,则还需要加载babel库。)

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[node/.style = {circle,draw}]
  \node[node] (B) {B};
  \node[node] (A) [below of=B, xshift = -20mm] {A}
    edge [-,"1"] (B); % would like to label this edge "1"
  \node[node] (C) [below of=B] {C}
    edge [-,"1"] (A); % would like to label this edge "1"
  \node[node] (D) [below of=C] {D}
    edge [-,"1"] (A); % would like to label this edge "1"
  \node[node] (E) [below of=B, xshift =  20mm] {E}
    edge [-,"1"] (B) % would like to label this edge "1"
    edge [-,"1"] (C) % would like to label this edge "1"
    edge [-,"1"] (D); % would like to label this edge "1"
\end{tikzpicture}
\end{document}

答案2

使用 TikZ 库automatapositioningquotes节点和路径是分开绘制的,在定位节点时使用positioning库中确定的语法:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{automata,
                positioning,
                quotes}
\begin{document}
    \begin{tikzpicture}[
node distance = 15mm, on grid,
every state/.style={semithick, minimum size=1em, inner sep=2pt},
every edge/.append style = {-},
every edge quotes/.append style = {auto, font=\footnotesize, inner sep=2pt}
                        ]
    \begin{scope}[nodes={state}]
\node (C)       {C};
\node (A) [ left=of C] {A};
\node (B) [above=of C] {B};
\node (E) [right=of C] {E};
\node (D) [below=of C] {D};
    \end{scope}
%
\path (A)   edge ["1"] (B)
      (B)   edge ["1"] (E)
      (E)   edge ["1"] (D) 
      (D)   edge ["1"] (A)
% 
      (A)   edge ["1"] (C) 
      (C)   edge ["1"] (E); 
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容