TransWikia.com

Labeling an edge in TikZ

TeX - LaTeX Asked by Jason Miller on December 6, 2020

I’m new to TikZ (and LaTeX) and I’m trying to create a graph with nodes.

I’ve been reading the manual and I’ve created a graph using the same method in the picture. I’d like to now label my edges, but when I try to label them like they do in the manual, it does not work.

pgfmanual.pdf

My code is below. Please help!!

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}

2 Answers

One way is to use the quotes library. (If you use the babel package, you also need to load the babel library.)

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}

Answered by user228539 on December 6, 2020

With use of the TikZ libraries automata, positioning and quotes, nodes and paths are drawn separate, in positioning of nodes is used syntax determined in positioning library:

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}

enter image description here

Answered by Zarko on December 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP