Overlays and verbatim

Another weird LaTeX problem. I have a piece of code on my slide and the result it gives. I want to change the code slightly and visualize the change in the result. Normally in LaTeX beamer slides, I would use overlays like this:

Query:
\begin{verbatim}
some code
\alt<2>{slightly changed code on slide 2}{original code on slide 1}
some more code
\end{verbatim}

Result:
this item is the same in both
\visible<1>{this one is only there for the original code}
\visible<2>{this one is only there for the changed code}

So far, so good. The code is in a verbatim environment, so I have cannot put the overlay around the line I want to change, but that’s fine, let’s make it an alternative around the whole verbatim part. But, unfortunately, the problem is that you cannot put a verbatim environment inside of overlays (learn why). So you have to hack it. This is the code I want, the line with FILTER is the one I only want to have on the second slide:

\begin{verbatim}
SELECT ?book ?author ?releasedate
WHERE {
   ?book dbo:author ?author .
   {
      ?book dbp:releaseDate ?releasedate . 
   } UNION {
      ?book dbp:pubDate ?releasedate . 
   }
   FILTER (?releasedate > 1950)
}
\end{verbatim}

Like in my post on using verbatim inside of verbatim, I have to end the verbatim environment prematurely, skip back over the space and then I can include the overlay inside of verb.

\begin{verbatim}
SELECT ?book ?author ?releasedate
WHERE {
   ?book dbo:author ?author .
   {
      ?book dbp:releaseDate ?releasedate . 
   } UNION {
      ?book dbp:pubDate ?releasedate . 
   }
\end{verbatim}
\vspace{-0.5\baselineskip}
\verb|  |\visible<2>{\texttt{FILTER (?releasedate > 1950 )}}\\
\verb|}|
\\

Not the most elegant way, but it works…

Writing verbatim inside verbatim

More strange problems, still teaching LaTeX. So I wanted to teach people how to get text or code formatted exactly as they type it with the verbatim environment. This is the part of code I wanted to use as an example:

\begin{verbatim}
text inside a      verbatim environment
is printed \emph{exactly} as     you\\
type      it ! % no comment!
\end{verbatim}

What I usually do is I have a block that shows the code they have to write and then the result. And of course for showing the code they need to write, I use verbatim. So can you include verbatim inside verbatim?

Well, yes you can, but of course you cannot include \end{verbatim}, as this would be interpreted as ending the verbatim environment. So I had to add this separately afterwards, using \verb (inline verbatim). Voila:

\begin{block}{What you write}
\begin{verbatim}
\begin{verbatim}
text inside a      verbatim environment
is printed \emph{exactly} as     you\\
type      it ! % no comment!
\end{verbatim}
\verb|\end{verbatim}|
\end{block}

Now there’s only a small problem, namely that there is some spacing after the environment, so it seems like there is an empty line between the end of the verbatim text and the \end{verbatim}. For this example it doesn’t matter, I use it to show them that empty lines are also printed exactly as they are given.

But next example is how to include verbatim inside a beamer frame. The example code I wanted to show inside the usual verbatim environment:

\begin{frame}[fragile]
   \begin{verbatim}
      test
   \end{verbatim}
\end{frame}

So you see that here I don’t really want an empty line between \end{verbatim} and \end{frame}. So my final hacky solution jumped back up over that space after the environment:

\begin{block}{What you write}
\begin{verbatim}
\begin{frame}[fragile]
   \begin{verbatim}
      test
\end{verbatim}
\vspace{-0.9\baselineskip}
\verb|   \end{verbatim}|\\
\verb|\end{frame}|
\end{block}

Which works fine, but the more easy solution is probably this one (or use lstlisting or any other code-environment):

\begin{block}{What you write}
\verb|\begin{frame}[fragile]|\\
\verb|   \begin{verbatim}|\\
\verb|      test|\\
\verb|   \end{verbatim}|\\
\verb|\end{frame}|
\end{block}