1

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!

  • Please make example code compilable, if you can, or as close to compilable as possible, if the problem you are asking about is that you can't get it to compile. – cfr May 6 '16 at 12:02
5

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, andbelow` 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}
  • Thanks so much, I've used this and managed to make it work! :) – Alice Roberts May 6 '16 at 11:44
  • 1
    @AliceRoberts Note that you can say 'thanks' by accepting an answer. To do this, you click on the greyed-out checkmark at the top left of the answer, below the upvote/downvote buttons. You can do this even if you don't have enough reputation to upvote. – cfr May 6 '16 at 12:00
2

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

  • Thanks alot! I actually used the previous response, but this looks like it will be easier for the next one! – Alice Roberts May 6 '16 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.