3

I just started using TikZ, I want to draw exactly like this picture,enter image description here

I tried, and this is what I getenter image description here, which is not exactly what I want, how can I achieve the wanted result? Thanks

My MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
            \tikzstyle{block} = [draw,text centered, minimum height=2.8em,minimum width=12em]
        \tikzstyle{block1} = [text centered, minimum height=2.8em,minimum width=12em]   
    \begin{tikzpicture}
    \node [block] at (0,0) (1) {\textbf{Abstract Specification ($R_{0}$)}};
    \node [block] at (1.7,-2) (2) {\textbf{Refined Specification($R_{1}$)}};
    \node [block1] at (3.5,-3.8) (3) {.........};
    \node [block] at (6,-6) (4) {\textbf{Refined Specification ($R_{N}$)}};
    \node [block] at (7.5,-8) (5) {\textbf{Executable Program}};
        % arroows
 \draw [-latex,thick] (1.south) --  node [right] {\small{$Refinement$}} (2.north) ;
 \draw [-latex,thick] (2.south) --  node [right] {\small{$Refinement$}} (3.north) ;
  \draw [-latex,thick] (3.south) --  node [right] {\small{$Refinement$}} (4.north) ;
  \draw [-latex,thick] (4.south) --  node [right] {\small{$Refinement$}} (5.north) ;
    \end{tikzpicture}
\end{document}
  • What is the next step you want to implement? That is, what aspect are you trying to change now and want help with? There are many ways in which the two images differ - you need to help us by telling us which ones you are bothered about and, preferably, which particular one you want answers to this particular question to tackle. – cfr Sep 20 '17 at 23:47
  • @cfr I think he want to make the arrows shifted left to point to the center of every "hidden rectangular" and not to the center of it's top. But let's wait for his answer – koleygr Sep 20 '17 at 23:52
  • @cfr I want to get exactly like the first picture, the second picture is my try, the problem is in the arrows, I don't know how to get them align like the first picture – Nousa Sep 21 '17 at 0:07
  • 'the problem is in the arrows, I don't know how to get them align like the first picture' please put this in your question - it is the arrows and not, say, the twiddly bit at the bottom right of the bottom right box, for example. – cfr Sep 21 '17 at 0:16
8

A first fix is to get rid of .north and .south, so that the arrows will point to the center. (tikz is clever enough to don't "show" the arrow that connects the paths inside them)

But to have really parallel arrows you need to fix the coordinates to exact steps:

\documentclass{article}
\usepackage{tikz}

\begin{document}
            \tikzstyle{block} = [draw,text centered, minimum height=2.8em,minimum width=12em]
        \tikzstyle{block1} = [text centered, minimum height=2.8em,minimum width=12em]   
    \begin{tikzpicture}
    \node [block] at (0,0) (1) {\textbf{Abstract Specification ($R_{0}$)}};
    \node [block] at (1.7,-2) (2) {\textbf{Refined Specification($R_{1}$)}};
    \node [block1] at (3.4,-4) (3) {.........};
    \node [block] at (5.1,-6) (4) {\textbf{Refined Specification ($R_{N}$)}};
    \node [block] at (6.8,-8) (5) {\textbf{Executable Program}};
        % arroows
 \draw [-latex,thick] (1) --  node [right] {\small{$Refinement$}} (2) ;
 \draw [-latex,thick] (2) --  node [right] {\small{$Refinement$}} (3) ;
  \draw [-latex,thick] (3) --  node [right] {\small{$Refinement$}} (4) ;
  \draw [-latex,thick] (4) --  node [right] {\small{$Refinement$}} (5) ;
    \end{tikzpicture}
\end{document}

Output:

enter image description here

A next step is to use bm package to make bold the math too...

\documentclass{article}
\usepackage{tikz}
\usepackage{bm}

\begin{document}
            \tikzstyle{block} = [draw,text centered, minimum height=2.8em,minimum width=12em]
        \tikzstyle{block1} = [text centered, minimum height=2.8em,minimum width=12em]   
    \begin{tikzpicture}
    \node [block] at (0,0) (1) {\textbf{Abstract Specification ($\bm{R_{0}}$)}};
    \node [block] at (1.7,-2) (2) {\textbf{Refined Specification ($\bm{R_{1}}$)}};
    \node [block1] at (3.4,-4) (3) {.........};
    \node [block] at (5.1,-6) (4) {\textbf{Refined Specification (\bm{$R_{N}}$)}};
    \node [block] at (6.8,-8) (5) {\textbf{Executable Program}};
        % arroows
 \draw [-latex,thick] (1) --  node [right] {\small{$Refinement$}} (2) ;
 \draw [-latex,thick] (2) --  node [right] {\small{$Refinement$}} (3) ;
  \draw [-latex,thick] (3) --  node [right] {\small{$Refinement$}} (4) ;
  \draw [-latex,thick] (4) --  node [right] {\small{$Refinement$}} (5) ;
    \end{tikzpicture}
\end{document}

Output: The same with bold math.

Something last is if you want to make ultra thick the arrows and thick the rectangles (So that it will be more close to a flow diagram by emphasize the flow and the separate steps)

\documentclass{article}
\usepackage{tikz}

\begin{document}
            \tikzstyle{block} = [draw,thick,text centered, minimum height=2.8em,minimum width=12em]
        \tikzstyle{block1} = [text centered, minimum height=2.8em,minimum width=12em]   
    \begin{tikzpicture}
    \node [block] at (0,0) (1) {\textbf{Abstract Specification ($\bm{R_{0}}$)}};
    \node [block] at (1.7,-2) (2) {\textbf{Refined Specification ($\bm{R_{1}}$)}};
    \node [block1] at (3.4,-4) (3) {.........};
    \node [block] at (5.1,-6) (4) {\textbf{Refined Specification (\bm{$R_{N}}$)}};
    \node [block] at (6.8,-8) (5) {\textbf{Executable Program}};
        % arroows
 \draw [-latex,ultra thick] (1) --  node [right] {\small{$Refinement$}} (2) ;
 \draw [-latex,ultra thick] (2) --  node [right] {\small{$Refinement$}} (3) ;
  \draw [-latex,ultra thick] (3) --  node [right] {\small{$Refinement$}} (4) ;
  \draw [-latex,ultra thick] (4) --  node [right] {\small{$Refinement$}} (5) ;
    \end{tikzpicture}
\end{document}

Output:

enter image description here

It is a matter of taste after that (and may be before :P )

  • 1
    Also it is not a good latex practice to put text in math mode... you could just emphasize or something. But I left it as it was – koleygr Sep 21 '17 at 0:11
6

This answer doesn't add anything to the previous answers regarding the positioning of nodes and arrows. But, just for fun, I added a shape with a folded edge for the last node. Although, I have to admit, it's a bit much for just one bend edge.

The new shape is named document. It's based on an example from the PGF manual. The folded edge can be on any edge of the node (option document corner). It has the same anchors as a rectangular node and additionally the anchors north west corner, north east corner, south east corner, and south east corner to get lines and arrows all the way to the bend corner. (Option blocklast see complete example below.)

\node [blocklast,shape=document,document corner=south east] (6) at (0,4) {\textbf{Executable Program}};
\node [blocklast,shape=document,document corner=south west] (7) at (5,4) {\textbf{Executable Program}};
\node [blocklast,shape=document,document corner=north west] (8) at (5,2) {\textbf{Executable Program}};
\node [blocklast,shape=document,document corner=north east] (9) at (0,2) {\textbf{Executable Program}};
\draw [latex-latex,thick] (6.south east corner) -- (8.north west corner);
\draw [latex-latex,thick] (7.south west corner) -- (9.north east corner);

enter image description here

The folded edge itself can also be configured (options in picture):

enter image description here

With this, the diagram looks like this:

enter image description here

The code:

\documentclass{article}
\usepackage{tikz}

% the new shape, based on example in
% 101.5.3 Command for Declaring New Shapes
% on page 1039, pgf manual for version 3.0.1a
\makeatletter
% options
\tikzset{%
  document corner width/.store in=\dc@width,
  document corner height/.store in=\dc@height,
  document corner shift/.store in=\dc@shift,
  document corner bend/.store in=\dc@bend,
  document corner width=18pt,
  document corner height=6pt,
  document corner shift=3pt,
  document corner bend=2pt,
  document corner/.is choice,
  document corner/north west/.code={\def\dc@corner{0}},
  document corner/north east/.code={\def\dc@corner{1}},
  document corner/south east/.code={\def\dc@corner{2}},
  document corner/south west/.code={\def\dc@corner{3}},
  document corner=south east
}
\newdimen\pgf@xd
%\newdimen\pgf@yd
\newdimen\pgf@xe
\newdimen\pgf@ye
\pgfdeclareshape{document}{
  \inheritsavedanchors[from=rectangle]
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{south east}
  % anchors for corners
  \anchor{north west corner}{
    \southwest
    \pgf@xa=\pgf@x
    \northeast
    \pgf@x=\pgf@xa
    \pgf@xa=\dc@width\pgf@xa=0.5\pgf@xa
    \pgf@ya=\dc@height\pgf@ya=0.5\pgf@ya
    \advance\pgf@x by \pgf@xa
    \advance\pgf@y by-\pgf@ya
   }
  \anchor{north east corner}{
    \northeast
    \pgf@xa=\dc@width\pgf@xa=0.5\pgf@xa
    \pgf@ya=\dc@height\pgf@ya=0.5\pgf@ya
    \advance\pgf@x by-\pgf@xa
    \advance\pgf@y by-\pgf@ya
  }
  \anchor{south east corner}{
    \northeast
    \pgf@xa=\pgf@x
    \southwest
    \pgf@x=\pgf@xa
    \pgf@xa=\dc@width\pgf@xa=0.5\pgf@xa
    \pgf@ya=\dc@height\pgf@ya=0.5\pgf@ya
    \advance\pgf@x by-\pgf@xa
    \advance\pgf@y by \pgf@ya
  }
  \anchor{south west corner}{
    \southwest
    \pgf@xa=\dc@width\pgf@xa=0.5\pgf@xa
    \pgf@ya=\dc@height\pgf@ya=0.5\pgf@ya
    \advance\pgf@x by \pgf@xa
    \advance\pgf@y by \pgf@ya
  }

  \backgroundpath{%
    % store lower right in xa/ya and upper right in xb/yb
    \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
    \ifcase\dc@corner % north west
      % compute corner of ‘‘flipped page’’
      \pgf@xc=\pgf@xa \advance\pgf@xc by \dc@width
      \pgf@yc=\pgf@yb \advance\pgf@yc by-\dc@height
      \pgf@xd=\pgf@xc \advance\pgf@xd by-\dc@shift
      \pgf@xe=\dc@width \advance\pgf@xe by-\dc@shift \pgf@xe=0.5\pgf@xe % = (\dc@width-\dc@shift)/2
      \advance\pgf@xe by \pgf@xa
      \pgf@ye=\pgf@yc \advance\pgf@ye by \dc@bend
      % construct main path
      \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yc}}
      \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
      \pgfpathclose
      % add little corner
      \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xd}{\pgf@yc}}
      \pgfpathquadraticcurveto{\pgfpoint{\pgf@xe}{\pgf@ye}}{\pgfpoint{\pgf@xa}{\pgf@yc}}
    \or % north east
      % compute corner of ‘‘flipped page’’
      \pgf@xc=\pgf@xb \advance\pgf@xc by-\dc@width
      \pgf@yc=\pgf@yb \advance\pgf@yc by-\dc@height
      \pgf@xd=\pgf@xc \advance\pgf@xd by \dc@shift
      \pgf@xe=\dc@width \advance\pgf@xe by-\dc@shift \pgf@xe=0.5\pgf@xe % = (\dc@width-\dc@shift)/2
      \advance\pgf@xe by \pgf@xd
      \pgf@ye=\pgf@yc \advance\pgf@ye by \dc@bend
      % construct main path
      \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
      \pgfpathclose
      % add little corner
      \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xd}{\pgf@yc}}
      \pgfpathquadraticcurveto{\pgfpoint{\pgf@xe}{\pgf@ye}}{\pgfpoint{\pgf@xb}{\pgf@yc}}
    \or % south east
      % compute corner of ‘‘flipped page’’
      \pgf@xc=\pgf@xb \advance\pgf@xc by-\dc@width
      \pgf@yc=\pgf@ya \advance\pgf@yc by \dc@height
      \pgf@xd=\pgf@xc \advance\pgf@xd by \dc@shift
      \pgf@xe=\dc@width \advance\pgf@xe by-\dc@shift \pgf@xe=0.5\pgf@xe % = (\dc@width-\dc@shift)/2
      \advance\pgf@xe by \pgf@xd
      \pgf@ye=\pgf@yc \advance\pgf@ye by-\dc@bend
      % construct main path
      \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
      \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@ya}}
      \pgfpathclose
      % add little corner
      \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xd}{\pgf@yc}}
      \pgfpathquadraticcurveto{\pgfpoint{\pgf@xe}{\pgf@ye}}{\pgfpoint{\pgf@xb}{\pgf@yc}}
    \or % south west
      % compute corner of ‘‘flipped page’’
      \pgf@xc=\pgf@xa \advance\pgf@xc by \dc@width
      \pgf@yc=\pgf@ya \advance\pgf@yc by \dc@height
      \pgf@xd=\pgf@xc \advance\pgf@xd by-\dc@shift
      \pgf@xe=\dc@width \advance\pgf@xe by-\dc@shift \pgf@xe=0.5\pgf@xe % = (\dc@width-\dc@shift)/2
      \advance\pgf@xe by \pgf@xa
      \pgf@ye=\pgf@yc \advance\pgf@ye by-\dc@bend
      % construct main path
      \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@yc}}
      \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@ya}}
      \pgfpathclose
      % add little corner
      \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xd}{\pgf@yc}}
      \pgfpathquadraticcurveto{\pgfpoint{\pgf@xe}{\pgf@ye}}{\pgfpoint{\pgf@xa}{\pgf@yc}}
    \fi
  }
}
\makeatother

\begin{document}
  \begin{tikzpicture}[%
    blocksize/.style={minimum height=2.8em,minimum width=12em},
    block/.style={blocksize,draw},
    blocklast/.style={blocksize,draw,thick},
    blockdots/.style={blocksize}
  ]
    \node [block]     at (0,0)      (1) {\textbf{Abstract Specification ($R_{0}$)}};
    \node [block]     at (2,-2)     (2) {\textbf{Refined Specification($R_{1}$)}};
    \node [blockdots] at (3.7,-3.7) (3) {\dots\dots\dots};
    \node [block]     at (5.4,-5.4) (4) {\textbf{Refined Specification ($R_{N}$)}};
    \node [blocklast,shape=document] at (7.4,-7.4) (5) {\textbf{Executable Program}};
    % arroows
    \draw [-latex,thick] (1) --  node [right] {\textit{\small Refinement}} (2) ;
    \draw [-latex,thick] (2) --  node [right] {\textit{\small Refinement}} (3) ;
    \draw [-latex,thick] (3) --  node [right] {\textit{\small Refinement}} (4) ;
    \draw [-latex,thick] (4) --  node [right] {\textit{\small Refinement}} (5) ;
  \end{tikzpicture}
\end{document}
  • Wow, this is more than I was looking for, thanks a lot for making it @Mike – Nousa Sep 28 '17 at 10:08
4

a variation of koleygr answer. with employed libraries arrows.meta, chains, positioning and quotes:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
node distance = 8mm and -42mm,
  start chain = A going below right,
   box/.style = {draw, text width=48mm, align=center, minimum height=6mm,
                 inner sep=1mm, font=\sffamily\bfseries,
                 on chain=A},
every edge quotes/.style = {inner sep=2mm, font=\small\itshape, anchor=west}
                        ]
% nodes
\node   [box]   {Abstract Specification $(R_{0})$}; % A-1
\node   [box]   {Refined Specification  $(R_{1})$};
\node   [box, draw=none]    {\dots\dots};
\node   [box]   {Refined Specification  $(R_{N})$};
\node   [box]   {Executable Program};               % A-5
% arrows
\draw[-Stealth, semithick]
    (A-1) edge ["Refinement"] (A-2)
    (A-2) edge ["Refinement"] (A-3)
    (A-3) edge ["Refinement"] (A-4)
    (A-4)  to  ["Refinement"] (A-5);
    \end{tikzpicture}
\end{document}

as can be seen, all local styles definition is moved to definition of styles in tikzpicture options. text "Refinement" is in italic shape instead wrongly used math node (label is not collection of variables). with node distance is possible simple determine the slope of nodes positioning. with this and use of chains, edges code become more clear and concise.

addendum: arrow between nodes can be drawn in a loop (and make code slightly shorter):

% arrows
\foreach \i [remember=\i as \j (initially 1)] in {2,...,5}
\draw[-Stealth, semithick]
    (A-\j) to ["Refinement"] (A-\i);

enter image description here

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.