# Creating a diagram in LaTeX, possibly with TikZ?

I am interested in drawing some flow style diagram with arrows, as sketched in the picture to represent the flow of recursion. I was hoping someone could point me in the right direction as to how I would go around creating a diagram like this. I am familiar with circuitikz so far, so maybe an approach with tikz?

• Yes, this is definitely doable with tikz. Please make an initial attempt and we'd be happy to help you complete it. – Peter Grill Oct 21 '14 at 5:41

Concision at the expense of clarity and/or generalisability (if that is actually a word):

\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth,every node/.style={text depth=0cm},
every label/.style={label distance=0.75cm, text=red, inner sep=1cm/16}]
\foreach \i [count=\j, count=\k from 0] in {5,7,9,11}{
\node [label={[name=p-\j]180:$+$}, label={[name=q-\j]0:$\i$}]
(n-\j) at (\j*1.5,-\j/1.5) {Stuff (\i)};
\draw [<-] (n-\j.base west) -| ++(-.5,.5);
\ifnum\j>1
\draw [blue, ->] (p-\k) to [out=250, in=200] (p-\j);
\draw [blue, ->] (q-\j) to [out=110, in=20]  (q-\k);
\fi
}
\end{tikzpicture}
\end{document}

I would go with a matrix.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzset{
table/.style={
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={rectangle,text width=4.5em,align=center,inner sep=0pt},
text depth=1.25ex,
text height=2.5ex,
nodes in empty cells
},
}

\begin{document}

\begin{tikzpicture}
% the matrix entries
\matrix (mat) [table]
{
&  &  &  &  &  \\
|[text=red]|$+$ & Stuff (5)  & |[text=red]|5 & & &  \\
&  |[text=red]|$+$  & Stuff (7) & |[text=red]|7 & &  \\
&    & |[text=red]|$+$ & Stuff (9) & |[text=red]|9 &  \\
&    &  &|[text=red]|$+$ & Stuff (11) & |[text=red]| 11   \\
};
% the matrix rules

% the arrows
\begin{scope}
\draw[->]  ([xshift=8pt]mat-1-1.south) |- ([yshift=4pt]mat-2-2.south west);
\draw[->]  ([xshift=8pt]mat-2-2.south) |- ([yshift=4pt]mat-3-3.south west);
\draw[->]  ([xshift=8pt]mat-3-3.south) |- ([yshift=4pt]mat-4-4.south west);
\draw[->]  ([xshift=8pt]mat-4-4.south) |- ([yshift=4pt]mat-5-5.south west);
\draw[->,shorten >= 3pt]  ([shift={(-3pt,-3pt)}]mat-2-1.center) to[out=240, in=190]  ([shift={(-3pt,-3pt)}]mat-3-2.center);
\draw[->,shorten >= 3pt]  ([shift={(-3pt,-3pt)}]mat-3-2.center) to[out=240, in=190]  ([shift={(-3pt,-3pt)}]mat-4-3.center);
\draw[->,shorten >= 3pt]  ([shift={(-3pt,-3pt)}]mat-4-3.center) to[out=240, in=190]  ([shift={(-3pt,-3pt)}]mat-5-4.center);
\draw[->,shorten >= 7pt]  ([shift={(0pt,8pt)}]mat-5-6.center) to[out=90, in=65]  (mat-4-5.center);
\draw[->,shorten >= 7pt]  ([shift={(0pt,8pt)}]mat-4-5.center) to[out=90, in=65]  (mat-3-4.center);
\draw[->,shorten >= 7pt]  ([shift={(0pt,8pt)}]mat-3-4.center) to[out=90, in=65]  (mat-2-3.center);
\end{scope}
\end{tikzpicture}

\end{document}

• Would :-) is the right word – azetina Oct 21 '14 at 4:50
• Wow, great. I will have to spend some time figuring out how that all goes together. thanks – JS60 Oct 21 '14 at 5:06
• No \foreach ? – percusse Oct 21 '14 at 7:09
• @percusse It would have been ;). Newbies some times find it difficult to follow the compactness. Hence avoided. :) – user11232 Oct 21 '14 at 7:11
• There is no newbies in TeX or all of us are. – percusse Oct 21 '14 at 7:11

This one goes by macro that takes 5 arguments, especially the L shape can have different vertical and horizontal lengths, which are controlled by #2 (negiative for going down) and #3. The text is defined by a stuff style that has a fixed length.

#1=starting point, #2=vertically down  length,  #3=length of L, #4=end point, #5=stuff label.

Code

\documentclass[border=10pt]{standalone}%[11pt]{article}

\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\tikzset{stuff/.style={rectangle, text width=2cm, inner sep=0pt, outer sep=0pt}
}
\newcommand\myL[5]{
% draw L shape line
\draw[line width=1pt,->] ($(#1)+(1cm,0)$) node[below left=0.3cm and 0.2cm](a){\color{red}$+$} |- +(0,#2)
node[](b){} -- +(#3,#2)node[](#4){};
\node[stuff,anchor=south west] at (#4){#5};
% curve arrow on the left
\draw[blue,->] (a) to[out=-90,in=-180, looseness=2] ([shift={(0,-0.5)}]#4);
% curve line on the right, 3cm and 2cm can be changed
\draw[green,->] ($(#4)+(2cm,0.5cm)$) to[out=90,in=0] ($(b.east)+(1cm,-#2)$);
}
\begin{document}

\begin{tikzpicture}
\node[inner sep=0pt, outer sep=0pt] at (0,0) (A){};
\myL{A}{-1}{1}{B}{Long Stuff(5)\hfill \color{red}5}
\draw[line width=5pt,white,->] ($(B)+(2cm,0.5cm)$)to[out=90,in=0] ($(b.east)+(1cm,1cm)$);   % to erase the unneeded curve arrow that was drawn automatically.
\myL{B}{-1}{1}{C}{Stuff(7)\hfill \color{red}  7}
\myL{C}{-1}{1}{D}{Stuff(9)\hfill \color{red}  9}
\myL{D}{-1}{1}{E}{Stuff(11)\hfill \color{red} 11}
\draw[line width=5pt,white,->, looseness=2] (a) to[out=-90,in=-180] ([shift={(0,-0.5)}]E); % to erase the unneeded curve arrow that was dran automatically.
\end{tikzpicture}
\end{document}