Graphics, Figures & TablesGraph with nodes.

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Novermars
Posts: 2
Joined: Tue Jan 07, 2014 5:58 pm

Graph with nodes.

Post by Novermars »

Hello everybody!

For an assignment, I have to make a graph with 8 nodes and connect them. In the end, it should look something like this:
network_zps8648c5e4.png
network_zps8648c5e4.png (183.71 KiB) Viewed 7125 times
I found the TikZ-package to be useful, but still have some questions.
Currently, I have the following figure:
graph_zps0e33b37c.png
graph_zps0e33b37c.png (92.63 KiB) Viewed 7125 times
with the following code:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2cm,
  thin,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}]

  \node[main node] (1) {1};
  \node[main node] (2) [right of=1] {2};
  \node[main node] (3) [right of=2] {3};
  \node[main node] (4) [right of=3] {4};
  \node[main node] (5) [right of=4] {5};
  \node[main node] (6) [right of=5] {6};
  \node[main node] (7) [right of=6] {7};
  \node[main node] (8) [right of=7] {8};

  \path[every node/.style={font=\sffamily\small}]
    (1) edge node [right] {100} (2)
        edge [bend left] node[right] {180} (3)
        edge [bend left] node[right] {300} (4)
        edge [bend left] node[right] {380} (5)
        edge [bend left] node[right] {620} (6)
        edge [bend left] node[right] {950} (7)
        edge [bend left] node[right] {1140}(8)

    (2) edge node [right] {100} (3)
        edge [bend left] node[right] {180} (4)
        edge [bend left] node[right] {220} (5)
        edge [bend left] node[right] {400} (6)
        edge [bend left] node[right] {720} (7)
        edge [bend left] node[right] {820} (8)

    (3) edge node [right] {100} (4)
        edge [bend left] node[right] {140} (5)
        edge [bend left] node[right] {260} (6)
        edge [bend left] node[right] {480} (7)
        edge [bend left] node[right] {580} (8)
        
    (4) edge node [right] {100} (5)
        edge [bend left] node[right] {160} (6)
        edge [bend left] node[right] {300} (7)
        edge [bend left] node[right] {380} (8) 
     
    (5) edge node [right] {100} (6)  
        edge [bend left] node[right] {180} (7)
        edge [bend left] node[right] {220} (8)
        
    (6) edge node [right] {100} (7)   
        edge [bend left] node[right] {120} (8)
        
    (7) edge node [right] {100} (8);     
\end{tikzpicture}
\end{document}
And to make an understatement, it looks messy as hell. Is it possible to make it look more like the figure above, so more spacing between the lines and the numbers either above or below the line?

Thanks in advance!
Last edited by cgnieder on Tue Jan 07, 2014 8:37 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

tom
Posts: 73
Joined: Thu Apr 18, 2013 4:02 am

Graph with nodes.

Post by tom »

Replace

Code: Select all

edge [bend left] node[right] {180} (3)
with

Code: Select all

edge [bend left=60] node[above] {180} (3)
HTH, Tom
Novermars
Posts: 2
Joined: Tue Jan 07, 2014 5:58 pm

Re: Graph with nodes.

Post by Novermars »

That works wonderful, thank you!
Post Reply