我正在尝试想出一个使用 tikz 进行递归调用的良好示意图,但结果并不像我所期望的那样,因为一个标签无缘无故地被翻转了:
MWE 是:
\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}
答案1
太棒了,这个列表(图是 Beamer 中的一个 trivlist)确实有一些有趣的副作用。你可以通过使用以下方法避免翻转\begin{figure}\leavevmode ...