# Changing verbatim input in beamer slide

I am trying to produce a presentation where code for a pair of arrays appear in the same position of a beamer slide.

I tried something like this (with different combinations and braces) and did not work.

\begin{verbatim}\only<1>

V1 V2 V3 V4
[1,]  0  1  1  0
[2,]  1  0  1  0
[3,]  1  1  0  0
[4,]  0  0  0  0

\only<2>

V1 V2 V3 V4
[1,]  0  1  0  0
[2,]  1  0  1  0
[3,]  0  0  0  0
[4,]  0  0  0  0

\end{verbatim}


Appreciate any suggestions.

Thanks!

verbatim material cannot appear in the argument of another command, so the usual approach \only<1>{\begin{verbatim}...\end{verbatim}} won't work. The easiest solution is to use the onlyenv environment (thanks to daleif for his comment about this):

\documentclass{beamer}

\begin{document}

\begin{frame}[fragile]
\begin{onlyenv}<1>
\begin{verbatim}
V1 V2 V3 V4
[1,]  0  1  1  0
[2,]  1  0  1  0
[3,]  1  1  0  0
[4,]  0  0  0  0
\end{verbatim}
\end{onlyenv}
\begin{onlyenv}<2>
\begin{verbatim}
V1 V2 V3 V4
[1,]  0  1  0  0
[2,]  1  0  1  0
[3,]  0  0  0  0
[4,]  0  0  0  0
\end{verbatim}
\end{onlyenv}
\end{frame}

\end{document}


An animation of the result:

• Can't it be used inside the onlyenv environment? – daleif Jun 18 '15 at 14:08
• @daleif Of course! I updated my answer. Thanks. – Gonzalo Medina Jun 18 '15 at 14:20
• I think I got it from Josephs W.s blog texdev.net – daleif Jun 18 '15 at 14:21
• Thanks a lot Gonzalo and daleif. It works like a charm... – Toño Jun 19 '15 at 7:45

You can also use the verbatimbox package to save the verbatim content into a named LaTeX box prior to the \only and then apply \only to the box.

\documentclass{beamer}
\usepackage{verbatimbox}
\begin{document}
\begin{frame}[fragile]
\begin{myverbbox}{\onlyA}
V1 V2 V3 V4
[1,]  0  1  1  0
[2,]  1  0  1  0
[3,]  1  1  0  0
[4,]  0  0  0  0
\end{myverbbox}
\begin{myverbbox}{\onlyB}
V1 V2 V3 V4
[1,]  0  1  0  0
[2,]  1  0  1  0
[3,]  0  0  0  0
[4,]  0  0  0  0\end{myverbbox}
\only<1>\onlyA
\only<2>\onlyB
\end{frame}
\end{document}

• @Toño That is strange, since I run MikTeX 2.9 and have no problem. But the good news is that the sty file can be downloaded from ctan.org/pkg/verbatimbox. You can place it in your working directory, or else in MikTeX's default package directory: C:\Users\StevenSegletes\AppData\Roaming\MiKTeX\2.9\tex\latex, where my name is replaced by the appropriate name for your file structure. Of course, then, you have to update the filename database (FNDB). – Steven B. Segletes Jun 24 '15 at 11:36