3

I am a beginner in TikZ. I want to draw a simple network with 2 nodes and a network like this:

simplenetwork

My code is here:

\documentclass[border=1pt]{standalone} 
\usepackage{tikz}
\usetikzlibrary{arrows, shapes, trees, calc}

\begin{document}
% Define styles
\tikzstyle{bnode}=[draw, fill=blue!20, text width=5em, text centered, minimum height=2.5em]

    \begin{tikzpicture}

    % draw two nodes
    \node (node1) [bnode] at (0, 0) {Node 1};
    \node (node2) [bnode] at (4, 0) {Node 2};

    % draw network

    % name this line to line1
    \draw[line width=2pt] ($(node1.west |- node1.south)+(0, -.5)$) -- ($(node2.east |- node2.south)+(0, -.5)$);
    % and draw a line between node1 and line1 like this:
    % \draw[line width=2pt] (node1) -- (line1); 

    \draw[line width=2pt] (node1) -- (0, -.95);
    \draw[line width=2pt] (node2) -- (4, -.95);

    \end{tikzpicture}
\end{document}

I made the horizontal line drawn by the position of Node1 and Node2, but I don't know how to draw a relative vertical line. Can you help optimize the code?

1 Answer 1

2

Something like this? I'm not sure if it is optimised or not - it probably depends what you have in mind.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{% \tikzstyle is deprecated
  bnode/.style={draw, fill=blue!20, text width=5em, text centered, minimum height=2.5em, line width=.4pt, above=.95}}
\begin{tikzpicture}
  \path [draw, line width=2pt] (0,0) coordinate (o) node (node 1) [bnode, anchor=west] {Node 1} -- (6,0) node (node 2) [bnode, anchor=east] {Node 2} (o -| node 1.south) -- (node 1.south) (o -| node 2.south) -- (node 2.south);
\end{tikzpicture}
\end{document}

nodes

1
  • Thank you cfr, This is what I wanted. It is working well but I need to study TikZ more.. Nov 10, 2015 at 4:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .