14

How to use beamer overlay inside minted code? The obvious solution does not work:

\documentclass{beamer}
\usepackage{minted}

\begin{document}

\begin{frame}[fragile]

\frametitle{Foo}

\begin{minted}{lua}
\uncover<1>{print("foo")}
\uncover<2>{print("bar")}
\uncover<3>{print("baz")}
\end{minted}

\end{frame}

\end{document}

7 Answers7

12

After some trial and error the following is the only solution that works for me:

\begin{frame}

\begin{overprint}
\onslide<1>
\begin{minted}{c++}
   ...
\end{minted}

\onslide<2>
\begin{minted}{c++}
   ...
\end{minted}
\end{overprint}

\end{frame}
VZ.
  • 221
12

It seems that there's been some progress and this now works as expected:

\documentclass{beamer}
\usepackage{minted}

\begin{document}

\begin{frame}[fragile]

\frametitle{Foo}

\begin{minted}[escapeinside=||]{lua} |\pause|print("foo") |\pause|print("bar") |\pause|print("baz") \end{minted}

\end{frame}

\end{document}

Michaël
  • 1,384
  • 11
  • 22
7

Unfortunately, there is no obvious way to make this work due to the way minted works internally. In fact, it is vital that the code is not parsed by TeX in the usual way.

There might be some trickery possible to circumvent this but for now I suggest that the easiest way to approximate the desired behaviour is to use multiple sequential minted environments:

\uncover<1>{\begin{minted}{lua}
print("foo")
\end{minted}}
\uncover<2>{\begin{minted}{lua}
print("bar")
\end{minted}}
\uncover<3>{\begin{minted}{lua}
print("baz")
\end{minted}}

Though to be honest I’m not certain if this even works.

jthulhu
  • 123
Konrad Rudolph
  • 39,394
  • 22
  • 107
  • 160
6

I can add that @VZ.'s solution also works if you want to "single-step" some code (in my case, C code with pointers) together with illustrations using tikzpicture. The "obvious" solution did not work, neither did using escapeinside due to a bug in pygments (see https://github.com/gpoore/minted/issues/70). In the solution below, highlightlines is used to highlight the current instruction. Too bad the actual C code need to be duplicate - but hey, it works!

\documentclass{beamer}
\usepackage{minted}
\usepackage{tikz}
\begin{document}
\begin{frame}[fragile]
\frametitle{Foo}
\begin{overprint}
\onslide<1>
\begin{minted}[linenos,highlightlines={3}]{c}
  int main(void) {
    char *p;
    p=(char *)malloc(5);
    /* do stuff */
    p=(char *)malloc(7);
    free(p);
    return 0;
  }
\end{minted}
\onslide<2>
\begin{minted}[linenos,highlightlines={5}]{c}
  int main(void) {
    char *p;
    p=(char *)malloc(5);
    /* do stuff */
    p=(char *)malloc(7);
    free(p);
    return 0;
  }
\end{minted}
\end{overprint}
\begin{tikzpicture}
  \node<1->[rectangle,draw] (p) {p};
  \node<1->[rectangle,draw,right of=p] (q1) {q1};
  \draw<1>[->] (p) -- (q1);
  \uncover<2->{\node[rectangle,draw,below right of=p] (q2) {q2};};
  \draw<2->[->] (p) -- (q2);
\end{tikzpicture}
\end{frame}
\end{document}
Johannes_B
  • 24,235
  • 10
  • 93
  • 248
Niclas Börlin
  • 301
  • 3
  • 9
4

You cannot put a minted environment inside \only or \uncover, but you can use it inside minted environments. As usual, you still need the fragile option for frame. Here's an example (where code is a minted environment).

\newminted[code]{scala}{}
\begin{frame}[fragile]
\begin{code}
snippet1
\end{code}
\begin{onlyenv}<2>
\begin{code}
snippet2
\end{code}
\end{onlyenv}
\note[item]{M}
\end{frame}
  • 1
    Thanks a lot, this is exactly what I needed and, since I'm a beamer noob, I didn't know about onlyenvs – ceres-c Aug 31 '21 at 14:20
2

I used something similar to what Konrad Rudolph suggested, I used \onslide<n> before the minted block (actually #+begin_src in emacs' orgmode). It does not require curly brackets around the block.

mlt
  • 589
-2

The following worked for me: (I'm using Pygments 1.5 which is pre-release at the moment.)

\begin{frame}[fragile]
  \frametitle{...}

  \begin{minted}{haskell}
    data C a = ...
  \end{minted}

  \begin{minted}{c++}
    template<typename T> class C {...};
  \end{minted}

  \begin{minted}{scala}
    class C[T] {...};
  \end{minted}
\end{frame}

The fragile keyword is in fact what does the trick for me. (Note its use after \begin{frame}.)

Hossein
  • 13