# Beamer overlays with tikz nodes break verbatim whitespace handling

I can't get any of the verbatim environments (alltt, semiverbatim) to respect white space as soon as I place it in an overlay macro:

\documentclass{beamer}
\begin{document}

\begin{frame}[fragile]

\only<1>{\begin{minipage}{0.9\textwidth}
\begin{semiverbatim}
top level
2 spaces indent
4 spaces indent
\end{semiverbatim}
\end{minipage}};

\end{frame}

\end{document}


The above example typesets as follows, on a single line:

top level 2 spaces 4 spaces


According to this answer https://tex.stackexchange.com/a/32488/63936 this has something to do with verbatim not working inside macro parameters. However verbatim indentation does work inside some macros, for instance in tikz's \node, but only if the node has no overlay spec. So there is some strange interaction between overlays and verbatim processing. Possibly tikz does some magic to overcome issues, but it does not go us far as nodes with overlays. The solution presented in the answer linked above only works with the listings package.

Is there anyway to make the other verbatim environments work inside overlayed contents?

A simple alternative is to fall back to the overlay environments that beamer offers as well. These are described in §9.4 of the beamer manual:

For each of the basic commands \only, \alt, \visible, \uncover, and \invisible there exists “environment versions” onlyenv, altenv, visibleenv, uncoverenv, and invisibleenv.

So in your case, switching to onlyenv gives the intended result:

\documentclass{beamer}
\begin{document}

\begin{frame}[fragile]

\begin{onlyenv}<1>
\begin{minipage}{0.9\textwidth}
\begin{semiverbatim}
top level
2 spaces indent
4 spaces indent
\end{semiverbatim}
\end{minipage}
\end{onlyenv}
;

\end{frame}

\end{document}


• Wow, this is pretty good. I should have asked here immediately, without wasting half a day on research this issue. It actually works for me, also if I mix in tikz nodes into this. I guess this is because the environment does not really place the verbatim thing in the parameter. Nice. I have forgotten about these environments. I never used them, and I remember wondering what they are for ... – Andrzej Wąsowski Jan 18 '15 at 22:54