TransWikia.com

Labelling Tikz Directed Edges

TeX - LaTeX Asked by Alice Roberts on May 22, 2021

I am struggling to label my edges in my Tikz directed graph. I have tried using the tikz library quotes in my preamble by no success.

I currently have the code

begin{tikzpicture}
tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}
tikzset{edge/.style = {->,> = latex'}}
% vertices
node[vertex] (1) at  (0,0) {$1$};
node[vertex] (2) at  (2,0) {$2$};
node[vertex] (3) at  (0,-2) {$3$};
node[vertex] (4) at  (2,-2) {$4$};
node[vertex] (5) at  (-2, -2) {$5$};
%edges
draw[edge] ["1/3"] (1) to (2);
draw[edge] (1.260) to (3.100);
draw[edge] (1.325) to (4.125);
draw[edge] (2) to (3);
draw[edge] (2) to (4);
draw[edge] (3.80) to (1.280);
draw[edge] (4) to (3);
draw[edge] (4.145) to (1.305);
draw[edge] (3.170) to (5.10);
draw[edge] (5.350) to (3.190);
end{tikzpicture}

And I want to achieve some thing similar to this..
enter image description here

Any help appreciated!

2 Answers

I made some changes to your code, so you can use nodes along paths.

  • midway places the node at the middle. pos= indicates a more precise position, from 0 (at start) to 1 (at end). midway is equivalent to pos=.5.
  • sloped alignes the node to the angle of the path it's on. The rotation I applied is to make it orthogonal to it.
  • right, above, left, and below are the positions of the node in regards to the path.

Output

enter image description here

Code

documentclass{article}
usepackage{tikz}

usetikzlibrary{arrows}

tikzset{
    vertex/.style={circle,draw,minimum size=1.5em},
    edge/.style={->,> = latex'}
}

begin{document}
begin{tikzpicture}

% vertices
node[vertex] (1) at (0,0) {$1$};
node[vertex] (2) at (2,0) {$2$};
node[vertex] (3) at (0,-2) {$3$};
node[vertex] (4) at (2,-2) {$4$};
node[vertex] (5) at (-2,-2) {$5$};

%edges
draw[edge] (1) -- (2) node[midway, above] {$frac{1}{3}$};
draw[edge] (1.260) -- (3.100);
draw[edge] (1.325) -- (4.125) node[pos=.3, right, sloped, rotate=90] {$frac{1}{3}$};
draw[edge] (2) -- (3);
draw[edge] (2) -- (4) node[midway, right] {$frac{1}{2}$};
draw[edge] (3.80) -- (1.280);
draw[edge] (4) -- (3);
draw[edge] (4.145) -- (1.305);
draw[edge] (3.170) -- (5.10);
draw[edge] (5.350) -- (3.190);
end{tikzpicture}
end{document}

Answered by Alenanno on May 22, 2021

You've placed the quotes string on the wrong place I think, add it right after to and it works. With some adjustments for position and rotation.

documentclass[border=5mm]{standalone}
usepackage{tikz}
usetikzlibrary{quotes}
begin{document}
begin{tikzpicture}
tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}
tikzset{edge/.style = {->,> = latex}}
% vertices
node[vertex] (1) at  (0,0) {$1$};
node[vertex] (2) at  (2,0) {$2$};
node[vertex] (3) at  (0,-2) {$3$};
node[vertex] (4) at  (2,-2) {$4$};
node[vertex] (5) at  (-2, -2) {$5$};
%edges
draw[edge]  (1) to["$frac{1}{3}$"] (2);
draw[edge] (1.260) to["$frac{1}{2}$"] (3.100);
draw[edge] (1.325) to["$frac{1}{3}$" {sloped,rotate=90,anchor=west,pos=0.3}] (4.125);
draw[edge] (2) to["$frac{1}{3}$" {sloped,rotate=-90,anchor=east,pos=0.7}] (3);
draw[edge] (2) to["$frac{1}{2}$"] (4);
draw[edge] (3.80) to["$frac{1}{3}$"] (1.280);
draw[edge] (4) to["$frac{1}{2}$"] (3);
draw[edge] (4.145) to["$frac{1}{2}$" {sloped,rotate=90,anchor=east,pos=0.3}] (1.305);
draw[edge] (3.170) to["$1$"] (5.10);
draw[edge] (5.350) to["$frac{1}{2}$"] (3.190);
end{tikzpicture}
end{document}

enter image description here

Answered by Torbjørn T. on May 22, 2021

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