I'm trying to come up with a nice schematic of recursive calls using tikz, but the result is not what I expect as one label is flipped without a reason:
The MWE is:
\documentclass[xcolor=dvipsnames]{beamer}
\mode<presentation> {
\usetheme{default}
}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\tikzstyle{node} = [rectangle, rounded corners, minimum width=2cm, minimum height=0.5cm,text centered, draw=black]
\tikzstyle{arrow} = [thick,->,>=stealth]
\addtobeamertemplate{block begin}{}{\justifying} %new code
\usepackage{listings}
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12} % for normal
% Python style for highlighting
\newcommand\pythonstyle{\lstset{language=Python,
basicstyle=\ttm\scriptsize,
showstringspaces=false,
}}
% Python environment
\lstnewenvironment{python}[1][mathescape]
{
\pythonstyle
\lstset{#1}
}
{}
\begin{document}
\begin{frame}[fragile]
\begin{figure}
\begin{tikzpicture}[node distance=2cm, scale=0.8, every node/.style={scale=0.8}]
\node (fact3) [node] {
\begin{python}
fact(3):
n = 3
return 3*fact(2)
\end{python}
};
\node (fact2) [node, below of=fact3] {
\begin{python}
fact(2):
n = 2
return 2*fact(1)
\end{python}
};
\node (fact1) [node, below of=fact2] {
\begin{python}
fact(1):
n = 1
return 1*fact(0)
\end{python}
};
\node (fact0) [node, below of=fact1] {
\begin{python}
fact(0):
n = 0
return 1
\end{python}
};
\path[->] (fact3) edge[bend left=30] node[swap] {} (fact2);
\path[->] (fact2) edge[bend left=30] node[swap] {} (fact1);
\path[->] (fact1) edge[bend left=30] node[swap] {} (fact0);
\path[->, dashed] (fact0) edge[bend left=30] node[pos=0.5, sloped, above] {1} (fact1);
\path[->, dashed] (fact1) edge[bend left=30] node[pos=0.5, sloped, above] {1} (fact2);
\path[->, dashed] (fact2) edge[bend left=30] node[pos=0.5, sloped, above] {2} (fact3);
\end{tikzpicture}
\caption{Recursive factorial calls}
\end{figure}
\end{frame}
\end{document}
\begin{figure}\leavevmode
...... node[pos=0.5,sloped,rotate=180, above] {2}...
. Aside,tikzstyle
is deprecated, usepositioning
library and usebelow=of
syntax.