8

I have to make the following block diagram in TikZ. As I am new to it, I can make a simple horizontal block diagram however, I am unable to connect blocks as shown in the picture below. Kindly can someone provide a TikZ code for this. I have a complicated block diagram, I have just added a part of it. Once I understand this, I can draw the complete diagram.

Required Block Diagram using TikZ

That's the code I have so far:

\documentclass{article}

\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,shadows,shapes}

\begin{document}
\pagestyle{empty}

\tikzstyle{line} = [draw, -latex']

\tikzset{label/.style={draw=gray, ultra thin, rounded corners=.25ex, fill=gray!20,text width=4cm, text badly centered,  inner sep=2ex, anchor=east, minimum height=4em}}

\begin{figure}[!htb]
\centering
\resizebox{\textwidth}{!}{%
\begin{tikzpicture}[node distance=5cm,auto,>=latex']
% Place nodes
\node [label] (init) {One};
\node [label, right of=init] (node) {One A1};
\node [label, right of=node] (node1) {One A2};
\node [label, right of=node1] (node2) {One B1};
\node [label, right of=node2] (node3) {One B2};
\node [label, right of=node3] (node4) {Two};

% Draw edges
\path [line] (init) -- (node);
\path [line] (node) -- (node1);
\path [line] (node1) -- (node2);
\path [line] (node2) -- (node3);
\path [line] (node3) -- (node4);

\end{tikzpicture}
}
\caption{Figure caption}
\label{fig:dummy}
\end {figure}

\end{document} 

The output for the code is

Code Output

  • 1
    Welcome to TeX.SE. It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem and shows how far you have gotten. While solving problems can be fun, setting them up is not. Then, those trying to help can simply cut and paste your MWE and get started on solving the problem, and provide you help with the specific problem you are having. – Peter Grill Oct 27 '14 at 20:12
  • If you search on TeX.SX you will many examples of these. See for example a-block-diagram-in-tikz, block-diagrams-using-tikz,... – Andrew Oct 27 '14 at 20:26
  • I have added the code I have so far. – Anas Oct 27 '14 at 20:30
  • Andrew, thanks for providing the links. I have understood the solution partially but still not sure how to connect the last node as mentioned in my question. – Anas Oct 27 '14 at 20:32
12

I am so basic in latex and this is my first time drawing such diagram, but the functions seem to be easy to understand. I searched and read this answer. I used its code as template to my answer.

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes.geometric}
\begin{document}
    \begin{tikzpicture}[>=latex']
        \tikzset{block/.style= {draw, rectangle, align=center,minimum width=2cm,minimum height=1cm},
        }
        \node [block]  (start) {One};

        \node [coordinate, right = 0.5cm of start] (ADL){};
        \node [coordinate, above = 1cm of ADL] (AUL){};
        \node [coordinate, right = 0.5cm of start] (BUL){};
        \node [coordinate, below = 1cm of BUL] (BDL){};

        \node [block, right = 0.5cm of AUL] (A1){One A1};
        \node [block, right = 0.5cm of BDL] (B1){One B1};

        \node [block, right = 1cm of A1] (A2){One A2};
        \node [block, right = 1cm of B1] (B2){One B2};

        \node [coordinate, right = 0.5cm of A2] (AUR){};
        \node [coordinate, below = 0.75cm of AUR] (ADR){};
        \node [coordinate, right = 0.5cm of B2] (BDR){};
        \node [coordinate, above = 0.75cm of BDR] (BUR){};
        \node [coordinate, right = 0.5cm of BUR] (BEnd){};
        \node [coordinate, right = 0.5cm of ADR] (AEnd){};

        \node [block, above right = 0cm and 1cm of B2] (end){Two};

        \path[draw, ->]
            (start) -- (ADL)
            (ADL) -- (AUL)
            (AUL) edge (A1)
            (A1) edge (A2)
            (A2) -- (AUR)
            (AUR) -- (ADR)
            (ADR) edge (AEnd)

            (start) -- (BUL)
            (BUL) -- (BDL)
            (BDL) edge (B1)
            (B1) edge (B2)
            (B2) -- (BDR)
            (BDR) -- (BUR)
            (BUR) -- (BEnd)
                    ;
    \end{tikzpicture}
\end{document}
  • 1
    many thanks for your efforts and providing the solution. – Anas Oct 27 '14 at 21:06
  • @Anas You are welcome. I am so basic in latex. But the code above is so easy to understand. First you define the styles you need, then you introduce the nodes and in the end, you connect them. – Enthusiastic Engineer Oct 27 '14 at 21:07
  • 1
    Many thanks for explaining the code. Although you are a beginner in Latex, I am sure you will be an expert soon. – Anas Oct 27 '14 at 21:11
5

ok, one solution with use of tikz is already anounced ... but anyway:

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows,calc,positioning,shadows,shapes}
    \begin{document}
    \begin{tikzpicture}[
            > = latex',
node distance = 0mm and 3mm,
mynode/.style = {name=n#1,
                 draw, minimum height=7mm, minimum width=22mm,
                 inner sep=1mm, outer sep = 0mm}
                        ]
%---
\node[mynode=1]                                    {One};
    \node[mynode=2,above right=0mm and 6mm of n1]  {one A1};
    \node[mynode=3,right= of n2]                   {one A2};
\node[mynode=4,below right=0mm and 6mm of n3]      {Two};
    \node[mynode=5,below right=0mm and 6mm of n1]  {one B1};
    \node[mynode=6,right= of n5]                   {one B2};
\coordinate[right=3mm of n1]    (a);
\coordinate[above left=2mm and 3mm of n4.west]    (b1);
\coordinate[below left=2mm and 3mm of n4.west]    (b2);
\draw       (n1) -- (a);
\draw[->]   (a) |- (n2);
\draw[->]   (a) |- (n5);
    \draw[->]   (n2) -- (n3);
    \draw[->]   (n3) -| (b1) -- (b1 -| n4.west);
    \draw[->]   (n5) -- (n6);
    \draw[->]   (n6) -| (b2) -- (b2 -| n4.west);
%-------
    \end{tikzpicture}
\end{document}

in above code I omit all unnecessary package. the result is equivalent as you show in question.

  • Many thanks @Zakro for providing the solution. – Anas Oct 27 '14 at 21:04
4

A PSTricks solution with help from the powerful xfp package:

\documentclass{article}

\usepackage[margin = 4cm]{geometry} % avoids `overfull \hbox' (adjust the margins according to the values of `\sizeW` and `\spreadW`)
\usepackage{pstricks}
\usepackage{xfp}

\newcommand*\adjustW[2]{\fpeval{#1*\sizeW+#2*\spreadW}}
\newcommand*\adjustH[2]{\fpeval{#1*\sizeH+#2*\spreadH}}

\begin{document}

\begin{center}
% horizontal adjustment
\def\sizeW{2.3}
\def\spreadW{0.5}
% vertical adjustment
\def\sizeH{1.2}
\def\spreadH{2.4}
% drawing
\begin{pspicture}(\adjustW{4}{5},\adjustH{1}{1})
\psset{dimen = m, arrows = ->}
  \psframe(0,\adjustH{0}{0.5})(\sizeW,\adjustH{1}{0.5})
  \rput(\adjustW{0.5}{0},\adjustH{0.5}{0.5}){One}
  \psline(\sizeW,\adjustH{0.5}{0.5})(\adjustW{1}{1},\adjustH{0.5}{0.5})
  \psline{<->}(\adjustW{1}{2},\adjustH{0.5}{1})(\adjustW{1}{1},\adjustH{0.5}{1})%
              (\adjustW{1}{1},\adjustH{0.5}{0})(\adjustW{1}{2},\adjustH{0.5}{0})
  \psframe(\adjustW{1}{2},\spreadH)(\adjustW{2}{2},\adjustH{1}{1})
  \rput(\adjustW{1.5}{2},\adjustH{0.5}{1}){One A$1$}
  \psframe(\adjustW{1}{2},0)(\adjustW{2}{2},\sizeH)
  \rput(\adjustW{1.5}{2},\adjustH{0.5}{0}){One B$1$}
  \psline(\adjustW{2}{2},\adjustH{0.5}{1})(\adjustW{2}{3},\adjustH{0.5}{1})
  \psline(\adjustW{2}{2},\adjustH{0.5}{0})(\adjustW{2}{3},\adjustH{0.5}{0})
  \psframe(\adjustW{2}{3},\spreadH)(\adjustW{3}{3},\adjustH{1}{1})
  \rput(\adjustW{2.5}{3},\adjustH{0.5}{1}){One A$2$}
  \psframe(\adjustW{2}{3},0)(\adjustW{3}{3},\sizeH)
  \rput(\adjustW{2.5}{3},\adjustH{0.5}{0}){One B$2$}
  \psline(\adjustW{3}{3},\adjustH{0.5}{1})(\adjustW{3}{4},\adjustH{0.5}{1})%
         (\adjustW{3}{4},\adjustH{0.75}{0.5})(\adjustW{3}{5},\adjustH{0.75}{0.5})
  \psline(\adjustW{3}{3},\adjustH{0.5}{0})(\adjustW{3}{4},\adjustH{0.5}{0})%
         (\adjustW{3}{4},\adjustH{0.25}{0.5})(\adjustW{3}{5},\adjustH{0.25}{0.5})
  \psframe(\adjustW{3}{5},\adjustH{0}{0.5})(\adjustW{4}{5},\adjustH{1}{0.5})
  \rput(\adjustW{3.5}{5},\adjustH{0.5}{0.5}){Two}
\end{pspicture}
\end{center}

\end{document}

output

All you have to do is choose the horizontal and vertical distances and the drawing will be adjusted accordingly.

  • 1
    Thanks Svend for providing a possible solution. Although it solves the problem, but its quite complicated for me as I haven't used PSTricks before. Since I don't understand it, its hard for me to extend the block diagram to illustrate the full block diagram I require. I will wait if some one else provides a Tikz solution; in the mean time I will try and understand this. Many thanks again for being kind to write the code. – Anas Oct 27 '14 at 20:47
3

Here another shot. Using the positioning-library for setting up the nodes and a \foreach-loop for creating the connections. The position of the arrows can be set with the connecting-angle of the nodes.

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc, positioning}
\tikzset{
 every node/.style={
  rectangle,
  draw,
  minimum width=2cm,
  minimum height=1cm
 },
 >=latex,
}

\begin{document}
 \begin{tikzpicture}[node distance=.25cm and 1cm]
  % Nodes
  \node (one) {One};
  \node (a1) [above right=of one] {One A1};
  \node (a2) [right=of a1] {One A2};
  \node (b1) [below right=of one] {One B1};
  \node (b2) [right=of b1] {One B2};
  \node (two) [below right=of a2] {Two};

  % Connectors
  \begin{scope}[->]
   \foreach \x\r in {a/165,b/195} {
    \draw (one) -| ($(one)!.5!(\x1)$) |- (\x1);
    \draw (\x1) -- (\x2);
    \draw (\x2) -| ($(\x2)!.5!(two)$) |- (two.\r);
   }
  \end{scope}
 \end{tikzpicture}
\end{document}
  • @moonspit: This is the required solution with minimum amount of coding. Many thanks for sharing this. – Anas Oct 27 '14 at 21:09
  • @Anas: Just thought it could be useful to post it as you said this is just a part of your diagram. Perhaps you can use some methods described for completing your diagram with saving you some work. – moospit Oct 27 '14 at 21:10
  • @moonspit: Yes, this will be extremely useful as the for loop will simplify the code where numerous node connections are required. Thanks for your effort. – Anas Oct 27 '14 at 21:13

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.