# How to create step by step annotated programming tutorial in LaTeX?

(question inspired by How to create a crossword puzzle in LaTeX? and provided solutions)

How to make in most comfortable way in LaTeX (I use pdfLaTeX, but I am open for LuaTeX solutions) annotated, step by step, programming tutorials? Preferably from annotated sourcecode? Or in other way, by less effort than typing each slide by slide.

I means questions and answers tutorial in form of presentation (I don't know if beamer is the only solution for presentations, but I've heard it's popular one).

Here is an example how such presentation would look like:

*-------------------------------------------*
|      How to implement Main function?      |
|                                           |
|                                           |
| (1)?                                      |
|                                           |
|                                           |
|                                           |
|                                           |
|    (2)?                                   |
| (1)                                       |
|                                           |
| (1) ... ?                                 |
| (2) ... ?                                 |
*-------------------------------------------*

*-------------------------------------------*
|              Main function                |
|                                           |
|                                           |
| int main(){ (1)                           |
|                                           |
|                                           |
|                                           |
|                                           |
|    return 0; (2)                          |
| } (1)                                     |
|                                           |
| (1) ...                                   |
| (2) ...                                   |
*-------------------------------------------*

*-------------------------------------------*
|        How to perform Computation?        |
|                                           |
|                                           |
| int main(){                               |
|    (3)?                                   |
|                                           |
|    (4)?                                   |
|                                           |
|    return 0;                              |
| }                                         |
|                                           |
| (3) ...?                                  |
| (4) ...?                                  |
*-------------------------------------------*

*-------------------------------------------*
|              Computation                  |
|                                           |
|                                           |
| int main(){                               |
|    long a,b,c; (3)                        |
|                                           |
|    c = a * b; (4)                         |
|                                           |
|    return 0;                              |
| }                                         |
|                                           |
| (3) ...                                   |
| (4) ...                                   |
*-------------------------------------------*

*-------------------------------------------*
|            Input & Output  ?              |
|                                           |
| (5) ?                                     |
| int main(){                               |
|    long a,b,c;                            |
|    (6) ?                                  |
|    c = a * b;                             |
|    (6) ?                                  |
|    return 0;                              |
| }                                         |
|                                           |
| (5) ...?                                  |
| (6) ...?                                  |
*-------------------------------------------*

*-------------------------------------------*
|            Input & Output                 |
|                                           |
| #include <cstdio> (5)                     |
| int main(){                               |
|    long a,b,c;                            |
|    scanf("%ld%ld", &a, &b); (6)           |
|    c = a * b;                             |
|    printf("%ld\n", c); (6)                |
|    return 0;                              |
| }                                         |
|                                           |
| (5) ...                                   |
| (6) ...                                   |
*-------------------------------------------*

-
Although not exactly clear what you ask, I think you can do it with itemize environment with \pause. Also, you might like this question and its answers. – percusse Feb 18 '12 at 14:43

An interactive version with beamer may be constructed along the following lines:

\documentclass{beamer}

\begin{document}

\begin{frame}[fragile]%
\begin{semiverbatim}
\frametitle{Input \&\ Output}
\#include <cstdio>
\only<1>{(1)?}\only<2->{int main() \{}
\only<3>{(3)?}\only<4->{long a,b,c;}
\only<4>{(4)?}\only<5->{std::scanf("\%ld\%ld", \&a, \&b);}
\only<5>{(5)?}\only<6->{c = a * b;}
\only<6>{(6)?}\only<7->{std::printf("\%ld\textbackslash n", c);}
\only<2>{(2)?}\only<3->{return 0;}
\}
\end{semiverbatim}
\end{frame}

\end{document}


-
Great ! It will save a lot of my time life :). – Grzegorz Wierzowiecki Feb 18 '12 at 15:32
Very nice :). If you use such techniques for teaching purposes, I've found tool (however it's not TeX, but web interactive) to make similar visualisations automatically , nice to give a try (:)) : Online Python Tutor and related web article – Grzegorz Wierzowiecki Feb 18 '12 at 19:09
@Andrey-Vihrov Example uses cstdio in C++ because it's tailored to pitfalls of programming contests like spoj.pl - iostream can be to slow for such contests. On many contests return 0 is requirement to get solution accepted. – Grzegorz Wierzowiecki Feb 19 '12 at 9:20
@GrzegorzWierzowiecki: My comment was not very polite :-S The speed of iostreams varies per platform (in my tests I found that iostreams is even faster on reading ints with GCC 4.4 on 64-bit Linux; but slower on MSVC) and isn't important even in programming contests, unless you are inputting a very large set of data. In my opinion, this isn't enough to sacrifice the simplicity and type safety of iostreams in examples. Now, in C++, reaching the closing brace } of main() is equivalent to writing return 0; at the end. So you don't need it after all. That's all I have to say. Cheers! – Andrey Vihrov Feb 19 '12 at 10:04
I know all what you've wrote. However, some contests use specific versions of compilers and standard libraries, tailored to contests needs and it happens a lot, that solutions using iostreams a slower. That same applies to return 0. On the other hand - there are application where you are not in such comfort of using modern compiler -> like specific embedded micro-controlers. To sup it up: I appreciate your corrections and understand motivations, while in same time, just clarity that there are applications where rules are slightly different. All best :). – Grzegorz Wierzowiecki Feb 19 '12 at 11:46