3

I need to draw a network topology diagram showing subnets, servers inside the subnets, and databases etc. I have been looking at various examples of using TikZ to draw diagrams, but I am uncertain how to accomplish my goal.

I saw an example that used \tikzstyle to define the various shapes for my diagram, but I have been unable to find an example of how to position those nodes.

My code so far:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\tikzstyle{vm} = [rectangle, rounded corners, minimum width=2.0cm, minimum height=1cm, text centered, textwidth=2.0cm, draw=black fill=green!20]
\tikzstyle{db} =  [rectangle, rounded corners, minimum width=6cm, minimum height=2cm, text centered, text width=6cm, draw=black, fill=red!20]
\tikzstyle {fs} = [rectangle, rounded corners, minimum width=1, minimum height=2, text width=1cm, draw=black]
\begin{document}
    \begin{figure}[h]
        \begin{center}
            \begin{tikzpicture}
                \node (mydb) {db} (4,2) {myDB};
                \node (fs) {fs} (4,4.5) {FSStorage};
                \node (ws1) {vm} (1,4.0) {WebServer1};
                \node (dev) {vm} (1,5.255) {Dev};
                \node (ws2) {vm} (5,4.0) {WebServer2};
                \node (ws3) {vm} (5,5.25) {WebServer3};
            \end{tikzpicture}
            \caption{my Network Diagram}
        \end{center}
    \end{figure}

I realize I don't have any of the edges connecting the nodes, but I wanted to get the nodes positioned before I do anything else.

5

The syntax used for nodes should be

\node [<style options>] (<node name>) at (<coordinate>) {<node text>};

but you're using

\node (<node name>) {<style options>} (<coordinate>) {<node text>};

So the style options are in curly braces instead of square brackets, and the at keyword is missing.

Note also that \tikzstyle{foo}=[<options>] is considered deprecated in favor of \tikzset{foo/.style={<options>}}. You also had two typos in the first style, textwidth instead of text width, and a missing comma after draw=black. See example below.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\tikzset{
  vm/.style={rectangle, rounded corners, minimum width=2.0cm, minimum height=1cm, text centered, text width=2.0cm, draw=black, fill=green!20},
  db/.style={rectangle, rounded corners, minimum width=6cm, minimum height=2cm, text centered, text width=6cm, draw=black, fill=red!20},
  fs/.style={rectangle, rounded corners, minimum width=1, minimum height=2, text width=1cm, draw=black}
  }
\begin{document}
            \begin{tikzpicture}
                \node [db] (mydb) at (4,2) {myDB};
                \node [fs] (fs) at (4,4.5) {FSStorage};
                \node [vm] (ws1) at (1,4.0) {WebServer1};
                \node [vm] (dev) at (1,5.255) {Dev};
                \node [vm] (ws2) at (5,4.0) {WebServer2};
                \node [vm] (ws3) at (5,5.25) {WebServer3};
            \end{tikzpicture}
\end{document}
5

In addition to the @torbjørn-t answer, I find the library positioning, which allow you to place a node relative to another without bothering with coordinates, very useful:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, positioning}

\tikzset{
  vm/.style={rectangle, rounded corners, minimum width=2.0cm, minimum height=1cm, text centered, text width=2.0cm, draw=black, fill=green!20},
  db/.style={rectangle, rounded corners, minimum width=6cm, minimum height=2cm, text centered, text width=6cm, draw=black, fill=red!20},
  fs/.style={rectangle, rounded corners, minimum width=1, minimum height=2, text width=1cm, draw=black}
}
\begin{document}
\begin{tikzpicture}[node distance=0.5cm]
  \node [db                               ] (mydb)  {myDB};
  \node [fs, above       = 1.25cm of mydb ] (fs)    {FSStorage};
  \node [vm, below left  = of fs          ] (ws1)   {WebServer1};
  \node [vm, above left  = of fs          ] (dev)   {Dev};
  \node [vm, below right = of fs          ] (ws2)   {WebServer2};
  \node [vm, above right = of fs          ] (ws3)   {WebServer3};
\end{tikzpicture}
\end{document}

enter image description here

In the code above, the above = of nodename, below = of nodename,... allow you position a node at the distance node distance from the node nodename. You can also specify a distance between = and of. You can read the details at section "17.5.3 Advanced Placement Options" of the PGF/TizZ manual.

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.