# Using \only on a block - beamer

Is there a way to make the command \only work on a block in beamer?

Or is there another way to make a block appear and then disappear?

Here is a part of my code:

\documentclass{beamer}
\usetheme{CambridgeUS}

\usepackage[french]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{moreverb}
\usepackage{listings}
\begin{document}

\begin{frame}[fragile]{Creation des outils principaux}
% - Le titre doit résumer le transparent dans un langage compréhensible
%   par tous ceux qui ne suivront rien de ce qu'il y a sur ce transparent.
\begin{itemize}
\item Recuperation des donnees entrées dans le terminal
\only<2>{
\begin{exampleblock}{Opt Parse}
\begin{lstlisting}[firstnumber=0]
import optparse
def parser():
parser = optparse.OptionParser(        description = "Dessinateur de fonctions")
default="out.ppm", dest="filename",
help='filename. Default : out.ppm')
return parser.parse_args()
if __name__ == '__main__':
(opts, args) = parser()
\end{lstlisting}
\end{exampleblock}
}
\end{itemize}
\end{frame}

\end{document}

• I had the same problem when using a tikZ \matrix command. All blocks afterwards need not start with \only but with \begin{block}{title}<5-> – Sebastian Apr 14 '13 at 20:39

I don't see your problem. I built this MWE:

\documentclass{beamer}
\usetheme{CambridgeUS}

\begin{document}

\begin{frame}{Title of this frame}
% first block appears at first
\only<1>{
\begin{block}{First block}
Hello
\end{block}
}
% first block disappears and the second appears
\only<2>{
\begin{block}{Second block}
Hello again
\end{block}
}

\end{frame}

\end{document}


getting these two frames:

As you can see, the frame number is always the same and the second block appears after the first one, that is not more visible.

Edit

After you posted your MWE I got your problem. Maybe my solution will not completely satisfy you, but it works.

First: the option fragile is needed anyway. Secondly, since I noticed that the code occupies more or less the whole frame (even setting the font size smaller), I decided in my solution to put it outside the itemize environment.

This is the new MWE:

\documentclass{beamer}
\usetheme{CambridgeUS}

\usepackage[french]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{moreverb}
\usepackage{listings}
\lstset{basicstyle={\footnotesize}}
\begin{document}

\begin{frame}[fragile]{Creation des outils principaux}
% - Le titre doit résumer le transparent dans un langage compréhensible
%   par tous ceux qui ne suivront rien de ce qu'il y a sur ce transparent.
\begin{itemize}
\item<1-> Recuperation des donnees entrées dans le terminal
\end{itemize}
\begin{exampleblock}{Opt Parse}<2>
\begin{lstlisting}[firstnumber=0]
import optparse
def parser():
parser = optparse.OptionParser(        description = "Dessinateur de fonctions")
default="out.ppm", dest="filename",
help='filename. Default : out.ppm')
return parser.parse_args()
if __name__ == '__main__':
(opts, args) = parser()
\end{lstlisting}
\end{exampleblock}

\end{frame}
\end{document}


The result is shown in the following two frames:

Notice that the key point is having inserted <1-> to the item and <2> for the exampleblock. In this way the sentence of the item is still visible when the exampleblock appears.

• When I do yours it's working but mine keep being stuck Maybe it's because the block are in a itemize? – lpostula Apr 20 '12 at 9:10
• @kasmanit: Please add a minimal working example (MWE) that illustrates your problem! – Daniel Apr 20 '12 at 9:16
• In fact it seems that it's the listing with the code that cause trhe problem, when I remove it it's working – lpostula Apr 20 '12 at 9:19
• Perhaps you forgot to use the frame option fragile when you inserted the code by means of listings. – Claudio Fiandrino Apr 20 '12 at 9:20
• When I add the fragile It gave me utf8x error (i gonna fix this later, it's not the main problem), beacause it says that there is missing } but I am sure tht everything is fine – lpostula Apr 20 '12 at 9:32

A variant of Claudio's answer can be obtained by modifying the action specifications of the block environment, as explained in the Beamer User Guide for version 3.50 in Section 9.6.3.:

\documentclass{beamer}
\usetheme{CambridgeUS}

\begin{document}

\begin{frame}{Title of this frame}
% first block appears at first
\begin{block}<only@1>{First block} % <-- note the "only@" here...
Hello
\end{block}
% first block disappears and the second appears
\begin{block}<only@2>{Second block} % <-- and here
Hello again
\end{block}

\end{frame}

\end{document}