如何在 Latex 中绘制模拟函数调用的代码片段

如何在 Latex 中绘制模拟函数调用的代码片段

该图片来自斯坦福 CS106B,展示了递归函数调用的方式。

这个怎么用latex画出来啊?

在此处输入图片描述

答案1

快速的第一种方法(但没有包listings):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shadows, shapes.callouts}

\begin{document}

\begin{tikzpicture}[font=\ttfamily]

\foreach \x in {1,...,3} {

    \node[fill=white, draw, drop shadow, align=left] at ({0.5*\x},{-0.5*\x}) (box \x) {
    string reverseOf(string s) \{ \\
    \quad\textcolor{magenta}{if} (s == "") \{ \\
    \quad\quad\textcolor{magenta}{return} ""; \\
    \quad\} \textcolor{magenta}{else} \{ \\ 
    \quad\quad\textcolor{magenta}{return} reverseOf(s.substr(1)) + s[0]; \\
    \quad\} \\
    \}
    };
    
    \node[shape=rectangle callout, fill=black!10, draw, minimum width=3em, callout relative pointer={(-0.25,-0.25)}] at ([xshift=5pt, yshift=7.5pt]box \x.center) { "" };
    
    \node[fill=black!10, draw=blue, anchor=south east, minimum width=3em, label={180:{string s}}] at ([xshift=-2pt, yshift=2pt]box \x.south east) { "p" };

}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

对...上瘾@Jasper Habicht,我们可以使用\usepackage{fontspec, inconsolata}字体(需要用LuaTeX或编译XeLaTeX)使其更像代码,因为我们不能\verb| |\foreach循环中使用(至少我不能)。因此,使其尽可能接近图像的输入和输出是:

\documentclass[border = 1cm, 11pt]{standalone}

\usepackage{tikz}
\usepackage{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{fontspec}
\usetikzlibrary{shapes.callouts, shadows}

\setmonofont{inconsolata}
    
\begin{document}

    \begin{tikzpicture}[font = {\ttfamily}]
        \foreach \i in {1,...,4} {
            \node[
                black, 
                align=left, 
                minimum width = 10.5 cm,
                minimum height = 4 cm,
                draw, 
                drop shadow={fill=black!100!white,shadow xshift=+2.4mm, shadow yshift=-2.4mm},
                line width = 3pt, 
                fill=white,
                inner xsep = 12pt,
                ] at ({0.7*\i},{-0.8*\i}) (thenode \i){%
                    string reverseOf(string s) \{ \\
                    \quad \textcolor{violet}{\textbf{if}} (s == "") \{ \\
                    \quad\quad\quad \textcolor{violet}{\textbf{return}} ""; \\
                    \quad \} \textcolor{violet}{\textbf{else}} \{ \\
                    \quad\quad\quad \textcolor{violet}{\textbf{return}} reverseOf(s.substr(1)) + s[0]; \\
                    \quad \} \\
                    \}
                };
            \node[
                fill=blue!5!white, 
                draw=black, 
                anchor=south east, 
                minimum width=4em, 
                label={180:{string s}}
                ] at ([xshift=-7pt, yshift=7pt]thenode \i.south east) { "p" };
            \node[%
                shape=rectangle callout, 
                fill=black!10!white, 
                draw= black, 
                minimum width=5em,
                rounded corners,
                callout relative pointer={(-0.7,-0.45)}
                ] at ([xshift=27pt, yshift=9.7pt]thenode \i.center) {""};
        }
    \end{tikzpicture}
    
    
\end{document}

在此处输入图片描述

相关内容