1

enter image description here

I'm been using Inkscape for the entire time, and it was just too much time consuming to resize the box, creating the arrow and placing the block etc... I've noticed about tikz in latex, and wonder if I could create diagrams like this without so much hassle. (e.g manual positioning). The ideal scenario is to just define a block, place some code in, and define the links.

  • 2
    Welcome to TeX S.E. You can do this with TikZ (or PStricks I believe). Don't expect miracles, it will be (very) time consuming at the beginning, but once you're done you can do it quite quickly. I'm quite impressed by the few minutes some members here need to produce figures from scratch. As a starter, if you stick with TikZ, the manual has very nice introducing examples. – sztruks Jan 5 at 17:35
  • The figure is straightforward, punching in the texts is cumbersome, and a burden on anyone considering writing an answer. As for the figure, a chain of nodes will do the trick and then you can add the arrows. – user121799 Jan 5 at 17:40
  • The "Diagrams as Simple Graphs" tutorial in version 3.0.1a of the TikZ manual produces a very similar graph starting on page 55 – AndréC Jan 5 at 17:41
  • Not a direct Tex answer but maybe you should have a look at packages like plantuml or dot (graphviz) and their export posibilities. – albert Jan 5 at 17:44
  • Please show some of your effort in trying. – zyy Jan 5 at 17:50
3

Now with texts.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{chains,shadows.blur}
\begin{document}
\begin{tikzpicture}[auto,
  node distance = 12mm,
  start chain = going below,
  box/.style = {draw,rounded corners,blur shadow,fill=white,
        on chain,align=center}]
 \node[box] (b1)    {$x_1\leftarrow0$\\ $y_1\leftarrow0$};      
 \node[box] (b2)    {$x_2\leftarrow\phi(x_1,x_3)$\\
 $y_2\leftarrow\phi(y_1,y_3)$\\
 $(x_2<10)$?};      
 \node[box] (b3)    {$y_3\leftarrow y_2+x_2$\\ $x_3\leftarrow x_2+1$};  
 \node[box] (b4)    {print($y_2$)};     
 \begin{scope}[rounded corners,-latex]
  \path (b2.-40) edge[bend left=50] (b4.40)
  (b1) edge (b2) (b2) edge (b3);
  \draw (b3.230) -- ++(0,-0.3) -| ([xshift=-5mm]b2.west) |-
  ([yshift=3mm]b2.130) -- (b2.130);
 \end{scope}
\end{tikzpicture}
\end{document}      

enter image description here

I think that it is fair to say that the basics are not too difficult to learn. The problem everyone has is that the pgfmanual has more than 1000 pages, so any answer can be repeated in countless different flavors. Here you could choose another way to bend the path, could use a matrix of nodes and many other things. Your question is about the "Simplest way to draw a control flow graph similar like this?". I do not claim that this is the simplest way, but it is a possible way, which is IMHO rather straightforward.

  • 2
    Really appreciate you taking the time to answer this. Looking at the snippet, it's not overly complicated as I imagined. – Guo Xian HO Jan 5 at 18:16
0

A simple pstricks solution:

\documentclass[svgnames, border=12pt]{standalone}

\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{pst-node, pst-blur}
\usepackage{auto-pst-pdf} 

\newcommand{\shadowframe}[1]{\psframebox[shadow, blur]{\enspace #1\enspace }}

\begin{document}

\psset{framearc=0.2, shadowcolor=DarkGray, shadowangle=-60}
\begin{psmatrix}[colsep =1cm, rowsep=1.2cm]
 &[name=A] \shadowframe{$\begin{aligned}
  x_1 & \leftarrow 0 \\ y_1 & \leftarrow 0
\end{aligned}$}\\
 & [name=B] \shadowframe{$\begin{gathered}
 \begin{aligned}
  x_2 & \leftarrow \phi(x_1,x_3)\\ y_2 & \leftarrow \phi(y_1,y_3)
\end{aligned}\\
x_2 < 10\,?
\end{gathered}$}\\
\pnode{I} & [name=C] \shadowframe{$\begin{aligned}
 y_3 & \leftarrow y_2 + x_2 \\ x_3 & \leftarrow x_2 + 1
\end{aligned}$}\\
 & [name=D] \shadowframe{\makebox[5.75em][l]{print($y_2$)}}
 %%% Links
 \psset{arrows=->, arrowinset=0.12, linejoin=1, linearc=0.25}%, nodesep=4pt
 \ncline{A}{B}
 \ncline{B}{C}
 \ncbar[arrows=-, angleA=-90,armA=1.2em, offsetA = -2em, angleB=90]{C}{I}%
 \ncbar[angleA=90, angleB=90 , offsetB = -2em, armB=1.5em]{I}{B}%
 \ncarc[arcangleA=50, arcangleB=60]{B}{D}% offsetB=2em,offset=1em,
\end{psmatrix}

\end{document} 

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.