1

I am trying to draw an activation stack. I am really bat at TikZ, so I hope you can help me out.

Activation Stack Diagram

As you can see, there are four boxes stacked one on top of the other. Each box has a name, put on the right, and is made of an upper part, where variables and numbers are displayed, and a lower part which is divided in two halves. The left half has to contain an arrow pointing to one of the boxes placed under it, the right half contains only a few characters. The box at the bottom hdoesn't have a pointy arrow, because it doesn't have to reference anything.

I really hope you can help me out, I really appreciate it.

1
  • 5
    Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – LaRiFaRi
    Mar 25 '15 at 10:08
2

You can start with:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning, arrows.meta}
\usepackage{cancel}

\tikzset{box/.style={draw, minimum width=3cm, minimum height=2cm, outer sep=0pt, font=\ttfamily, align=left},
    minibox/.style={box, minimum width=1.5cm, minimum height=5mm, anchor=north west},
    pointer/.style={fill, circle, minimum size=2mm, inner sep=0pt},
    }

\begin{document}
\begin{tikzpicture}[>=Latex]

\node[box, label=right:$P_2$] (P2) {};
\node[minibox] at (P2.south west) (P2-1) {};
\node[minibox] at (P2.south) (P2-2) {};

\node[box, label=right:$P_4$, below=1cm of P2] (P4) {};
\node[minibox] at (P4.south west) (P4-1) {};
\node[minibox] at (P4.south) (P4-2) {};

\node[box, label=right:$P_3$, below=1cm of P4] (P3) {a/d(p1)=0\\b=\cancel{3} 2};
\node[minibox] at (P3.south west) (P3-1) {};
\node[minibox] at (P3.south) (P3-2) {};

\node[box, label=right:$P_4$, below=1cm of P3] (P1) {a=3\\b=2\\c=1\\d=0};
\node[minibox] at (P1.south west) (P1-1) {};
\node[minibox] at (P1.south) (P1-2) {\# 6};

\draw[->] (P2-1.center) coordinate[pointer]  -- ([xshift=-10mm]P2-1.west) |- ([yshift=-8mm]P3.north west);
\draw[->] (P4-1.center) coordinate[pointer]  -- ([xshift=-5mm]P4-1.west) |- ([yshift=-4mm]P3.north west);
\draw[->] (P3-1.center) coordinate[pointer]  -- ([xshift=-5mm]P3-1.west) |- ([yshift=-4mm]P1.north west);
\draw[-|] (P1-1.center) coordinate[pointer]  -- ([xshift=-5mm]P1-1.west);
\end{tikzpicture}
\end{document}

enter image description here

5

You may find this excerpt from a book I write useful. Please do not use as is.

\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{calc}
\usetikzlibrary{shapes}
\usetikzlibrary{shapes.multipart}

\newcommand\kk[1]{\textcolor{RoyalBlue}{\text{\textup{\textbf{\texttt{#1}}}}}}
\newcommand\cc[1]{\textcolor{Sepia}{\text{\textup{\textbf{\texttt{#1}}}}}}

\begin{document}

\begin{tikzpicture}
\begin{scope}[start chain=going left,node distance=0pt]
\def\width{9ex}

\tikzstyle{frame}=[
    font=\scriptsize,
    %text width=7ex,
    draw,
    on chain,
    rectangle split,
    rectangle split parts=20,
    rectangle split part align={center, left},
    rectangle split empty part height=0ex,
    rectangle split empty part depth=0ex,
    rectangle split empty part width=0ex,
]

\node (main) [frame,
    rectangle split parts=5,
    rectangle split part fill={Fuchsia!30,brown!30, brown!30, yellow!40} 
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of O/S 
  \end{minipage}
\nodepart{two} \kk{int} \cc{argc}
\nodepart{three} \kk{char} \cc{**argv}
\nodepart{four} 
  \begin{minipage}{14ex} \centering
   \kk{int} \cc{a[500]}\medskip\\
    \tikz{\node (a) [
    draw,
    font=\tiny,
    text width=12ex,
    rectangle split parts,
    rectangle split part fill={yellow},
    rectangle split part align={left,left,left,left}
  ]{%
    \nodepart{one} \cc{a[499]}
    \nodepart{two} \cc{a[498]}
    \nodepart{three} {\begin{minipage}{6ex}\quad\\\mbox{\quad}\large\qquad$\vdots$\quad\\\scriptsize\end{minipage}}
    \nodepart{four}\cc{a[1]}
    \nodepart{five}\cc{a[0]}
  };}
  \end{minipage}
\nodepart{five} 
  \begin{minipage}{14ex} \centering
   \kk{int} \cc{b[200]}\\
    \tikz{\node [
    draw,
    text width=12ex,
    font=\tiny,
    rectangle split parts=3,
    rectangle split part fill={yellow},
    rectangle split part align={left,left,left,left}
  ]{%
    \nodepart{one} \cc{b[199]}
    \nodepart{two} {\large $\vdots$}
    \nodepart{three}\cc{b[0]}
  };}
  \end{minipage}
};

\node (sort) [frame,
    rectangle split parts=3,
    text width=10ex,
    rectangle split part fill={Fuchsia!30,brown!30, brown!30} 
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of \cc{main} 
  \end{minipage}
\nodepart{two} \kk{int} \cc{*a}
\nodepart{three} \kk{int} \cc{n}
};

\node (qsort1) [frame,
    rectangle split parts=5,
    rectangle split part fill={Fuchsia!30,teal!30,brown!30, brown!30, yellow!40} 
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of \cc{sort} 
  \end{minipage}
\nodepart{two}   \kk{void} \cc{*bp} \quad
\nodepart{three}   \kk{int} \cc{from}
\nodepart{four} \kk{int} \cc{to}
\nodepart{five}  \kk{int} \cc{p}
};

\node (qsort2) [frame,  
    rectangle split parts=5,
    rectangle split part fill={Fuchsia!30,teal!30,brown!30, brown!30, yellow!40} 
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of \cc{qsort}$_1$ 
  \end{minipage}
\nodepart{two}    \kk{void} \cc{*bp}
\nodepart{three}    \kk{int} \cc{from}
\nodepart{four}  \kk{int} \cc{to}
\nodepart{five}   \kk{int} \cc{p}
};

\node (qsort3) [frame,
  text width=9ex,
    rectangle split parts=5,
    rectangle split part fill={Fuchsia!30,teal!30,brown!30, brown!30, yellow!40} 
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of \cc{qsort}$_2$
  \end{minipage}
\nodepart{two}   \kk{void} \cc{*bp}
\nodepart{three}   \kk{int} \cc{from}
\nodepart{four} \kk{int} \cc{to}
\nodepart{five}  \kk{int} \cc{p}
};

\node (pivot) [frame,
    rectangle split parts=6,
    rectangle split part fill={Fuchsia!30,teal!30,yellow!40}
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of \cc{qsort}$_3$
  \end{minipage}
\nodepart{two}\kk{void} \cc{*bp}
\nodepart{three}\kk{int} \cc{last}
\nodepart{four}\kk{int} \cc{pivot}
\nodepart{five}\kk{int} \cc{split}
\nodepart{six}\kk{int} \cc{i}
};

\node (swap) [frame,
    rectangle split parts=5,
    rectangle split part fill={Fuchsia!30,teal!30,brown!30, brown!30,yellow!40} 
] {%
\nodepart{one} 
  \begin{minipage}{\width}
    \centering saved state of \cc{pivot} 
  \end{minipage}
\nodepart{two} \kk{void} \cc{*bp}
\nodepart{three} \kk{int} \cc{i}
\nodepart{four} \kk{int} \cc{j}
\nodepart{five} \kk{int} \cc{t}
};

\node(arrow)[on chain]{\Huge$\Leftarrow$};

\node[above=0 pt of main.north,anchor=south,font=\itshape] {main};
\node[above=0 pt of sort.north,anchor=south,font=\itshape] {sort};
\node[above=0 pt of qsort1.north,anchor=south,font=\itshape] {qsort$_1$};
\node[above=0 pt of qsort2.north,anchor=south,font=\itshape] {qsort$_2$};
\node[above=0 pt of qsort3.north,anchor=south,font=\itshape] {qsort$_3$};
\node[above=0 pt of pivot.north,anchor=south,font=\itshape] {pivot};
\node[above=0 pt of swap.north,anchor=south,font=\itshape] {swap};

\tikzstyle{bp}=[draw=black,thin,->,opacity=0.5,>=latex,->] 
\draw[bp] let 
            \p1 = ($(qsort1.two east)!0.1!(qsort1.two) $),
         \p2 =  ($(main.text)+(0,0) $),
           \p3 = ($ (sort.text split)!0.75!(sort.text split east) +(1ex,0)$)
     in 
                (\x1,\y1) -> (\x1,\y2) --(\x3,\y2) -- (\x3,\y3);
;
\draw[bp] let 
            \p1 = ($(qsort2.two east)!0.1!(qsort2.two) $),
         \p2 =  ($(main.text)+(0,1ex) $),
           \p3 = ($ (sort.text split)!0.75!(sort.text split east) +(0ex,0)$)
     in 
                (\x1,\y1)-- (\x1,\y2) --(\x3,\y2) -- (\x3,\y3);

\draw[bp] let 
            \p1 = ($(qsort3.two east)!0.1!(qsort3.two) $),
         \p2 =  ($(main.text)+(0,2ex) $),
           \p3 = ($ (sort.text split)!0.75!(sort.text split east) +(-1ex,0)$)
     in 
                (\x1,\y1)-- (\x1,\y2) --(\x3,\y2) -- (\x3,\y3);
;

\draw[bp] let 
            \p1 = ($(swap.two east)!0.1!(swap.two) $),
            \p2 =  ($(main.text)+(0,3ex) $),
           \p3 = ($ (sort.text split)!0.75!(sort.text split east) +(-2ex,0)$)
     in 
                (\x1,\y1)-> (\x1,\y2) -> (\x3,\y2) -> (\x3,\y3);



\draw[bp] let 
            \p1 = ($(pivot.two east)!0.1!(pivot.two) $),
         \p2 =  ($(main.text)+(0,-2.5ex) $),
           \p3 = ($ (qsort3.text split)!0.75!(qsort3.text split east) +(-1ex,0)$)
     in 
                (\x1,\y1)-- (\x1,\y2) --(\x3,\y2) -- (\x3,\y3);
;

\draw[bp] 
                ($(sort.two east)!0.5!(sort.two) $) |- (a.south west);
;
\end{scope}

\node[fill=gray!10,label=above left:{\scriptsize Legend},font=\scriptsize] at ($(swap)+(0,-20ex)$) {%
  \begin{minipage}{37ex}
    \begin{tikzpicture}
        \matrix[row sep=1mm, column sep=2mm,column 1/.style={anchor=base west},column 2/.style={anchor=base west},font=\scriptsize]{%
          \node[fill=Fuchsia!30,draw] {Caller's saved state}; & \node[draw,,fill=teal!30] {Back pointer};\\
          \node[draw,fill=brown!30] {Arguments}; &  \node[draw,fill=yellow!40] {Local variables};\\
        }; 
    \end{tikzpicture}
\end{minipage}
};

\begin{scope}[
every node/.style={ellipse,draw,font=\scriptsize\ttfamily,inner sep=1pt,fill=blue!20}
]
\node (root)
at ($(arrow)+(-24ex,10ex)$)
{$\top$}
[]
child {node {main}}
child {node {sort}
    child {node {swap}}
    child {node {qsort}
        child {node {pivot}}
    }
};
\end{scope}
\node[above=2pt of root,font=\small\bfseries]{Nesting structure};
\node[above=22pt of main,font=\small,xshift=-30ex]{\textbf{Frames on the hardware stac}};
\node[above=10pt of main,font=\footnotesize,xshift=-30ex]{\textit{(grows from high memory to low memory, depicted right to left)}};
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.