Thanks to Marc van Dongen there is a template of a "step by step" programming tutorial (with a sample layout in his question): How to create step by step annotated programming tutorial in LaTeX? .
I wonder if it is possible to achieve similar functionality with source highlighting ?
Simply changing environment from semiverbatim
to lstlisting
, does not work (as is easy to guess).
%Generate with : `pdflatex filename`
\documentclass{beamer}
\usepackage{listings}
\lstset{
language=C++,
basicstyle=\small,
numbers=left,
numberstyle=\tiny\color{gray},
}
\begin{document}
\begin{frame}[fragile]%
%\begin{semiverbatim}
\begin{lstlisting}[language=C++,caption={C++ as C++ TexTests},label=cppascpptextests]
\only<1-2>{\frametitle{General program structure}}
\only<3>{\frametitle{Variables}}
\only<4>{\frametitle{Reading numbers from standard input}}
\only<5>{\frametitle{Printing results on standard output}}
\only<6>{\frametitle{Assigning results of computation}}
\only<7>{\frametitle{Working example :)}}
\only<8>{\frametitle{Enjoy programming :)}}
\only<4>{(4)How to include needed header file?}\only<5->{\#include <cstdio>}
\only<1>{(1) How to construct main function ?}\only<2->{int main() \{}
\only<3>{(3) How to declare variables?}\only<4->{long a,b,c;}
\only<4>{(4) How to read data?}\only<5->{scanf("\%ld\%ld", \&a, \&b);}
\only<6>{(6) How to compute result?}\only<7->{c = a * b;}
\only<5>{(5) How to print result?}\only<6->{printf("\%ld\textbackslash n", c);}
\only<2>{(2) How to exit program correctly?}\only<3->{return 0;}
\only<2->{\}}
\end{lstlisting}
%\end{semiverbatim}
\end{frame}
\end{document}
escapechar=
option in your listing settings to escape to LaTeX inside the listing. This can be used to bring in the\only
commands. The problem is, though, that the stuff inside the LaTeX code does not get syntax highlighting. A more elegant way is to usemoredelims=
to hide this stuff in a listings style, take tex.stackexchange.com/a/18266/3751 for an inspiration.long a,b,c; // (3) How to declare variables?
) with the comment being allowed to stay after slide 3.lstlistings
independently.