So zeichnen Sie Codeausschnitte, die Funktionsaufrufe in Latex simulieren

So zeichnen Sie Codeausschnitte, die Funktionsaufrufe in Latex simulieren

Das Bild stammt von Stanford CS106B und zeigt rekursive Funktionsaufrufe.

Wie verwendet man Latex zum Zeichnen?

Bildbeschreibung hier eingeben

Antwort1

Ein erster kurzer Ansatz (aber ohne listingsPaket):

\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}

Bildbeschreibung hier eingeben

Antwort2

In Abhängigkeit von@Jasper Habicht, wir können font verwenden (das mit oder \usepackage{fontspec, inconsolata}kompiliert werden muss ), um es mehr wie einen Code aussehen zu lassen, da wir es nicht in der Schleife verwenden können (zumindest konnte ich das nicht). Die Eingabe und Ausgabe, um es so nah wie möglich an das Bild heranzubringen, sind also:LuaTeXXeLaTeX\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}

Bildbeschreibung hier eingeben

verwandte Informationen