# Drawing Loops in TikZ

I need your kind help to convert this image in TikZ/PGF, if possible:

FYI , I want to use the image on a beamerslide and a standard article type document (A4 paper).

I used Inkscape (.svg > .png), which does the job OK. But it doesn't provide any TikZ code - instead give PSTricks type code, which is even more confusing to me. Is there any GUI for TikZ/PGF?

-
Please show some attempt you've made in achieving this and specify what you're struggling with. The point of this Q&A site is to solve specific problems, not to have other people do your work. If your actual question is Is there any GUI for TikZ/PGF?, then clearly state so. If you're looking for someone to transfer your image to TikZ, you've come to the wrong place, sorry. –  doncherry Feb 15 '12 at 1:44
You can use a tree structure such as this one. You can test your code by using QTikZ on the fly for more convenience. –  percusse Feb 15 '12 at 1:45
Is there any particular reason why you want to do this in TikZ? For example, there are dedicated packages for drawing pseudocode algorithms. For example, algorithm2e. –  Marc van Dongen Feb 15 '12 at 5:57

This is fairly easy, see code below. Instead of writing the loop letters below each other, I rotated the node.

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{scopes}

\begin{document}

\begin{tikzpicture}
% putting the nodes

\node[red,right] (1) at (0,0) {first guess};
\node[black,right] (2) at (2,-2) {second guess};
\node[blue,right] (3) at (4,-4) {third guess};

% drawing arrows; the scopes library is used for putting the arrow tip option [-latex} only once; the |- notation means: from first point go straight down and then right to the second point, try -| to see the difference

{[-latex]
\draw[red] (1.south) |- (2.west);
\draw[black] (2.south) |- (3.west);
\draw[red] (1.220) -- node[rotate=-90, below] {loop} ++(0,-12) -- ++(6,0) node[right] {End};
\draw[black] (2.220) -- node[rotate=-90, below] {loop} ++(0,-8) -- ++(4,0) node[right] {End};
\draw[blue] (3.220) -- node[rotate=-90, below] {loop} node[right] {Max} ++(0,-4) -- ++(2,0) node[right] {End};
}
\end{tikzpicture}

\end{document}


Edit 1: If I get you right, you want

\draw[red] (1.south) -- (0,-2) -- (2.west);
\draw[black] (2.south) -- (2,-4) -- (3.west);


which yields the same result as before.

Edit 2: If one uses text width=3cm to break text inside the nodes, one alters the height of the node and for that also the coordinates of the 220 degree nodes. So I used the calc library to compute the coordinates relative to the node's centers. That way the arrows remain equally spaced and terminate at the same coordinates. The following picture illustrates the problem:

And the cleaned up code solving the problem with the calc library:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{scopes,calc}

\begin{document}

\begin{tikzpicture}

% putting the nodes

\node[red] (1) at (0,0) {first guess};
\node[black] (2) at (2,-2) {second guess};
\node[blue,text width=3cm] (3) at (4,-4) {Take a guess on $l(n)$ while Shanananana};

% drawing arrows; the scopes library is used for putting the arrow tip option [-latex} only once;
% the |- notation means: from first point go straight down and then right to the second point,
% try -| to see the difference

{[-latex,very thick]
\draw[red] (1.south) -- (0,-2) -- (2.west);
\draw[black] (2.south) -- (2,-4) -- (3.west);
\draw[red] ($(1.south) + (-0.4,0)$) --  node[rotate=-90, below] {loop} ($(1.center) + (-0.4,-12)$)  -- ++(6,0) node[right] {End};
\draw[black] ($(2.south) + (-0.4,0)$) -- node[rotate=-90, below] {loop} ($(2.center) + (-0.4,-8)$) -- ++(4,0) node[right] {End};
\draw[blue] ($(3.south) + (-0.4,0)$) -- node[rotate=-90, below] {loop} node[right] {Max} ($(3.center) + (-0.4,-4)$) -- +(2,0) node[right] {End};
}

\end{tikzpicture}

\end{document}


And the final result. Note that you can add a text width to the other nodes as well, the arrows still remain correct:

-
Thanks a lot. After adding all the usual texts, I'm getting two arrows in the opposite direction. Could you please tell me which I should tweak. Also, how can I break those "take a guess ..." line into two smaller lines as in the image (of the 1st post). I tried using  \\  before while, which gives error. –  bakul Feb 15 '12 at 12:51
Could you please post the code that's making trouble? –  Tom Bombadil Feb 15 '12 at 16:47
You can break lines by specifying a text width=Xcm but this influences the position of the 220 degree anchor. I'm working on a work around. Although it still works, the ''End'' nodes are then no longer aligned, which I don't like ;) –  Tom Bombadil Feb 15 '12 at 17:22
Thanks a lot for your help, Tom. Solved. –  bakul Feb 15 '12 at 19:16

You have already drawn this diagram in Inkscape right?. Then visit this site and install inkscape2tikz. Using this you can export tikz code without any trouble.

If you are in search of any Gui, then try QTikz or TikzEdt.

-
Thanks. I tried that before, but the script is probably not meant to be used when we're using Glyphs (in Incscape) for those math and Greek symbols. –  bakul Feb 15 '12 at 12:58
For adding math and things like that you can install TexText extension from the same website and then you can type re-editable LaTeX commands. –  Harish Kumar Feb 15 '12 at 16:01