12

I'm trying to draw some very basic Network topologies and thought that I'd try using Tikz for it.

So I thought that I'd make a start and then ask for help, as I've got to do 5 of these and there's no point doing them all backwards.

Here's my MWE:

\documentclass[11pt,a4paper,oneside]{book}
\usepackage{tikz}
\begin{document}
\begin{center}
\def\a{.4}
\begin{figure}

\begin{tikzpicture}
\draw (2,-1) circle (\a cm);
\draw (2,-1) -- (2,0);
\draw (4,-1) circle (\a cm);
\draw (4,-1) -- (4,0);
\draw (1,1) circle (\a cm);
\draw (1,1) -- (1,0);
\draw (3,1) circle (\a cm);
\draw (3,1) -- (3,0);
\draw (5,1) circle (\a cm);
\draw (5,1) -- (5,0);
\draw (0,0) -- (6,0);

\end{tikzpicture}
\caption{Bus Network Topology}

\end{figure}
\end{center}
\end{document}

So that comes out a bit wonky for starters, not the centering that I was hoping for

enter image description here

Also it puts the drawing in the next \section{}, because it's a float and LaTeX does what it likes with them. If there are any suggestions on how to combat this looseness that'd be ace. I guess I made it a float by using begin{figure}, but I wanted to caption it.

It would be nice to add some color as well to this, only flat 1 tone in the ellipses (would hide the connecting lines)

I'm going to draw Mesh, Tree, Star, Ring and Bus as there are depicted here.

enter image description here

Anyway, thanks!

  • Keeping the figure in the current section is easy: use the placeins package. Can't easily help with the rest of the question. – Mike Renfro Feb 22 '15 at 14:57
  • 1
    You can also use a package like capt-of so you can have a caption without a float. – cfr Feb 22 '15 at 15:00
17

bus network

Here's one way to draw the network:

\documentclass[11pt,a4paper,oneside]{book}
\usepackage{tikz}
\begin{document}
    \def\ab{.4}
    \tikzset{
      net node/.style = {circle, minimum width=2*\ab cm, inner sep=0pt, outer sep=0pt, ball color=blue!50!cyan},
      net connect/.style = {line width=1pt, draw=blue!50!cyan!25!black},
      net thick connect/.style = {net connect, line width=2.5pt},
    }
    \begin{figure}
      \centering
      \begin{tikzpicture}
        \path [net thick connect]
          (0,0) -- (6,0);
      \foreach \i/\j in {2/-1,4/-1,1/1,3/1,5/1}
      \path [net connect] (\i,0) -- (\i,\j) node [net node] {};
      \end{tikzpicture}
      \caption{Bus Network Topology}
    \end{figure}
\end{document}

Note that it makes no sense to put figure in a center environment. Instead, use \centering within the figure to centre the diagram. I've set up some styles as that makes it easier to keep things consistent and to change, say, the colour of all nodes if you need to. I've also used a loop to draw the nodes.

There are many ways to do this but this one should be fairly easy to adapt to the other network diagrams, whereas some of the other methods would not generalise as easily, I think.

Note that \a is an existing command. Use \newcommand rather than \def to check for this kind of problem. I've substituted \ab which is not already taken.

For some of the networks, it may be easier to use polar coordinates to specify the node locations. For example, consider the star which is basically nodes placed on a circle around a hub node:

star network

It would be a pain to calculate the node positions in the system used above, but polar coordinates make the diagram straightforward:

  \newcommand*\ab{.4}
  \tikzset{
    net node/.style = {circle, minimum width=2*\ab cm, inner sep=0pt, outer sep=0pt, ball color=blue!50!cyan},
    net root node/.style = {net node, minimum width=3*\ab cm},
    net connect/.style = {line width=1pt, draw=blue!50!cyan!25!black},
  }
  \begin{figure}
    \centering
    \begin{tikzpicture}
      \node (root) [net root node] {};
      \foreach \i in {0,...,4}
        \path [net connect] (root) -- (-90+\i*72:2) node [net node] {};
    \end{tikzpicture}
    \caption{Star Network Topology}
  \end{figure}

The ring and mesh are very similar:

ring network mesh network

  \begin{figure}
    \centering
    \begin{tikzpicture}
      \foreach \i in {0,...,4}
        \path (-90+\i*72:2) node (n\i) [net node] {};
      \path [net connect] (n0) -- (n1) -- (n2) -- (n3) -- (n4) -- (n0);
    \end{tikzpicture}
    \caption{Ring Network Topology}
  \end{figure}
  \begin{figure}
    \centering
    \begin{tikzpicture}
      \foreach \i in {0,...,4}
        \path (-90+\i*72:2) node (n\i) [net node] {};
      \foreach \i in {0,...,4}
        \foreach \j in {0,...,4}
          \path [net connect]
            (n\i) -- (n\j);;
    \end{tikzpicture}
    \caption{Mesh Network Topology}
  \end{figure}

For the tree, I would quite unnecessarily use forest but note that this is entirely egregious! I would do it because it is a tree and forest is fantastic - not because a diagram this simple needs the power of forest....

tree network

\documentclass[11pt,a4paper,oneside]{book}
\usepackage{tikz,forest}
\begin{document}
  \addtocounter{figure}{4}
  \newcommand*\ab{.4}
  \tikzset{
    net node/.style = {circle, minimum width=2*\ab cm, inner sep=0pt, outer sep=0pt, ball color=blue!50!cyan},
    net root node/.style = {net node, minimum width=3*\ab cm},
    net connect/.style = {line width=1pt, draw=blue!50!cyan!25!black},
  }
  \begin{figure}
    \centering
    \begin{forest}
      for tree={
        edge=net connect,
        if level=0{%
          net root node,
          before typesetting nodes={
            repeat=2{
              append={[,
                net node,
                repeat=3{
                  append={[, net node]},
                },
              ]},
            },
          },
        }{},
      }
      []
    \end{forest}
    \caption{Tree Network Topology}
  \end{figure}
\end{document}

What I like about this, of course, is that what actually draws the tree ends up being a single set of square brackets!

The hybrid is more fiddly since there is less of a pattern. I worked from the ring pattern and added the remaining nodes manually, with the help of the calc library for TikZ.

hybrid network

  \usetikzlibrary{calc}

  ...

  \begin{figure}
    \centering
    \begin{tikzpicture}
      \foreach \i in {0,...,4}
        \path (-90+\i*72:2) node (n\i) [net node] {};
      \path [net connect]
        (n0)
          edge node [net node, pos=1] {} +(0,-15mm)
          edge node [net node, pos=1] {} +(10mm,-15mm)
          edge node [net node, pos=1] {} +(-10mm,-15mm)
          -- (n1)
          edge (n4)
          edge (n3)
          -- (n2)
          -- (n3)
          -- (n4)
          -- (n0)
          ($(n2)!1/2!(n3)$) -- +(0,15mm) node [net node] {}
        ;
    \end{tikzpicture}
    \caption{Hybrid Network Topology}
  \end{figure}
  • that's ace cheers @cfr , hopefully I'll be able to make the rest from there. Just wondering - is it possible to scale a drawing after creating it? Say it was a bit small on the page or whatnot but all the ratios within it were fine? thanks – baxx Feb 22 '15 at 16:29
  • just seen the edits, thats great. I think I'd have been at this until wednesday looking at these :) – baxx Feb 22 '15 at 16:31
  • 1
    @user3130747 Yes. You can use [scale=2] or whatever as an argument to the tikzpicture environment. Note I've edited the above as I forgot the calc library is needed for the last one. – cfr Feb 22 '15 at 16:31
  • 1
    @user3130747 I'm supposed to be marking ;). – cfr Feb 22 '15 at 16:32
  • 1
    @user3130747 The TiKZ manual is very good: texdoc tikz or find it on CTAN. See Utilities > Repeating Things: The Foreach Statement. (Section 83 in the version of the manual I've got.) – cfr Feb 22 '15 at 18:02

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.