# Simplest way to draw a control flow graph similar like this?

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.

• 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

Now with texts.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[auto,
node distance = 12mm,
start chain = going below,
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}


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.

• 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

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}

\begin{document}

\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$)}}