11

I'm drawing a stack in beamer using drawstack package. I want to show the evolution of the stack by using pause. In tikzpicture, pause moves forward, but a stack is drawn in the reverse order. How do I get the pauses right to show the evolution of the stack. I can think of workarounds using multiple frames each showing the evolution, but is there a cleaner way of doing this?

\documentclass{beamer}
\usepackage{tikz}
\usepackage[nocolor]{drawstack}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\stacktop{} \cellptr{top of stack}
\separator
\cell{c} \cellcomL{2};
\separator
\cell{b} \cellcomL{1};
\separator
\cell{a} \cellcomL{0};
\separator
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

11
+50

Instead of using \pause I'd recommend using a key visible on as defined in the preamble of the following MWE. The trick is to enclose each level of stack in a scope environment, and pass the key visible on=<num> to the environment to set the overlay effects. I also had to redefine the \cellptr command in the drawstack package to prevent the top right arrow to wiggle between frames.

MWE

\documentclass{beamer}
\usepackage{tikz}
\tikzset{
  invisible/.style={opacity=0},
  visible on/.style={alt={#1{}{invisible}}},
  alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
  },
}

\usepackage[nocolor]{drawstack}
\renewcommand{\cellptr}[1]{
  \draw[<-,line width=0.7pt] (0,0) +(2,0) -- +(2.5,0) node[anchor=west] {#1};
}

\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{scope}[visible on=<4->]
  \stacktop{} \cellptr{top of stack}
  \separator
\end{scope}
\begin{scope}[visible on=<3->]
  \cell{c} \cellcomL{2};
  \separator
\end{scope}
\begin{scope}[visible on=<2->]
  \cell{b} \cellcomL{1};
  \separator
\end{scope}
\begin{scope}[visible on=<1->]
  \cell{a} \cellcomL{0};
  \separator
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}

Output

enter image description here

  • Thanks. For the top of stack indicator, can we have it on each frame as shown in the question? – arunmoezhi Dec 29 '15 at 22:54
  • @arunmoezhi: Yes. Just replace visible on=<4-> with visible on=<1-> in the scope associated with drawing the top stack. – Herr K. Dec 29 '15 at 23:17
  • thanks. But this will create a hole between a and top of stack in frame1 – arunmoezhi Jan 4 '16 at 18:39
8

A solution with a simple tabular:

\documentclass[table]{beamer}
\usepackage{fontspec}
\usepackage{libertine}
\usepackage{array}
\usetheme{Malmoe}

\begin{document}
\begin{frame}{Stack with a tabular}\arrayrulewidth=1pt
 \begin{tabular}{@{} l | 
    >{\columncolor{black!10}\centering\rule[-2ex]{0pt}{5ex}}p{3cm} |@{} r}\cline{2-2}
    & \ldots & $\longleftarrow$ top of stack \\\cline{2-2} \onslide<3->
  2 &   c    &                   \\\cline{2-2} \onslide<2->
  1 &   b    &                   \\\cline{2-2} \onslide<1->
  0 &   a    &                   \\\cline{2-2}  
 \end{tabular}
\end{frame}
\end{document}

enter image description here

Another solution with a simple makebox and without holes:

\documentclass[table]{beamer}
\usepackage{fontspec}
\usepackage{libertine}
\usepackage{array}
\usetheme{Malmoe}
\newcommand\MBox[2]{\makebox[1em]{#1}~\fbox{\makebox[3cm]{\rule[-2ex]{0pt}{5ex}#2}}}

\begin{document}
\begin{frame}{Stack with Boxes}\offinterlineskip
\onslide+<1->{\MBox{}{\ldots}$\longleftarrow$ top of stack\\}
\only<3->   {\MBox2c\\}
\only<2->   {\MBox1b\\}
\only<1->   {\MBox0a}
\end{frame}
\end{document}

enter image description here

  • thanks. Can the top of stack be dynamically moved. For instance, in frame1, top of stack has to be just above a with no holes in between – arunmoezhi Jan 4 '16 at 18:41
  • see my edited answer, the second variant. – user2478 Jan 4 '16 at 23:05
  • I get a compilation error at line 14 "! LaTeX Error: There's no line here to end." I'm using LuaLatex to compile – arunmoezhi Jan 4 '16 at 23:15
  • I used xelatex and had no problem.will have a look tomorrow – user2478 Jan 4 '16 at 23:21
  • Using xelatex/LuaLatex gives the same compilation error. But surprisingly the output pdf file is generated. The output file looks just fine. Thanks – arunmoezhi Jan 4 '16 at 23:29

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.