3

I am creating a graph of connected nodes and I want to have numbers on all edges that connect the nodes. At the moment it looks as follows:

\begin{figure}[H]
\begin{tikzpicture}
\node[shape=ellipse, draw=black, fill=gray!50] (a) at (7,-4) {a};
\node[shape=ellipse, draw=black, fill=gray!50] (b) at (7,1) {b};
\draw[solid] (b) -- (a) ;
\end{tikzpicture}
\end{figure}

If I want the line between a and b have an assigned number, how shall I do?

3

Something like this? The key is use of node

\draw[solid] (b) --node[pos=xx,position](){3}(a) ;

where xx=0~1 and position= left,right,above,below

enter image description here

Code

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,}

\begin{document}
%\begin{figure}[H]
\begin{tikzpicture}
\node[shape=ellipse, draw=black, fill=gray!50] (a) at (7,-4) {a};
\node[shape=ellipse, draw=black, fill=gray!50] (b) at (7,1) {b};
\draw[solid] (b) --node[midway,right](){1} (a) ;
\draw[solid] (b) --node[pos=0.1,left](){2} (a) ;
\draw[solid] (b) --node[pos=0.9,right](){3}(a) ;
\end{tikzpicture}
%\end{figure}
\end{document}

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.