\documentclass[handout]{beamer}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{csquotes, listings, manfnt}
\lstset{basicstyle=\scriptsize, breaklines=true, breakindent=0mm, frame=single}

\usetheme{Boadilla}
\setbeamertemplate{navigation symbols}{}

\begin{document}
\title{Multiple documents from one source}
\subtitle{\LaTeX\ for lecturers and teachers}
\author{Leo Arnold}
\institute{latex@arney.de}
\date{09. October 2012} 
 
\frame{\titlepage} 

\begin{frame}
\frametitle{Typical approaches and roads to ruin}

\begin{itemize}[<+->]
\item typesetting the problems in \texttt{Sheet01.tex} and the solutions in \texttt{Sheet01-solutions.tex}
\begin{itemize}[<+->]
\item tedious to maintain congruence once you start changing the order of problems
\item changes in notation have to be updated in two separate files
\item students require problem to also be provided right above their solution
\end{itemize}
\item typesetting the problems in \texttt{Sheet01.tex}, then copying this file to \texttt{Sheet01-solutions.tex} and filling in the solutions
\begin{itemize}[<+->]
\item Now students nag about incongruencies, and all the above other disadvantages remain
\item even worse: Now the problem is typeset twice and both versions have to be kept in sync too
\end{itemize}
\item Typesetting problems and solutions in \texttt{Sheet01.tex}
\begin{itemize}[<+->]
\item needs some kind of switch to in/exclude solutions
\item pay close attention not to distribute the version with solutions to students first
\end{itemize}
\end{itemize}
\end{frame}

\section{Designing a unified workflow}

\begin{frame}
\frametitle{Roadmap}

\tableofcontents[sectionstyle=show/hide, hideothersubsections, pausesubsections]
\end{frame}

\AtBeginSubsection[]
{
  \begin{frame}{Roadmap}
  \tableofcontents[sectionstyle=show/hide, subsectionstyle=show/shaded/hide]
  \end{frame}
}

\subsection{Requirements}

\begin{frame}
\frametitle{Requirements}

\begin{itemize}[<+->]
\item Only \textbf{one} source document
\begin{itemize}[<+->]
\item this is natural for typesetting the solution sheet 
\item no duplicate texts, no forgotten copy \& paste updates
\item maintain congruence of notation (Replace-all function)
\end{itemize}
\item Easy reuse of parts in future courses
\begin{itemize}[<+->]
\item allow for divide \& conquer using  \texttt{\textbackslash{}include}
\item automate the generation of the solution of some math problems
\end{itemize}
\end{itemize}

\end{frame}

\subsection{Implement a switch to in/exclude specific content}

\begin{frame}
\frametitle{Clearifing the use cases}

Our problem sheet needs to come in three different versions
\begin{description}[<+->]
\item[student] just the problems
\item[teacher] the problems and their solutions
\item[corrector] problems, solutions and notes on grading
\end{description}

\pause\medskip

\begin{tabbing}
\textbf{Solution:} \= \quad \= Use \texttt{comment.sty} by Victor Eijkhout\\[1ex]
\pause\textbf{Credits:} \>\> pointed out to me by Rolf Niepraschk
\end{tabbing}

\end{frame}

\begin{frame}[fragile]
\frametitle{What \texttt{comment.sty} does}

provides two simple commands
\begin{itemize}[<+->]
\item \verb+\includecomment{foobar}+\\
Defines the environment \texttt{foobar} whose content will be included
\item \verb+\excludecomment{foobar}+\\
Defines the environment \texttt{foobar} whose content will be ignored
\end{itemize}

\pause\smallskip

\hfill
\begin{minipage}{0.45\linewidth}
\begin{lstlisting}[backgroundcolor=\color{gray!20}, language={[LaTeX]TeX}]
\includecomment{wisdom}
\excludecomment{nonsense}

Confucius says:

\begin{wisdom}
Man who chases two rabbits catches none.
\end{wisdom}

\begin{nonsense}
Donald Knuth invented WinWord.
\end{nonsense}
\end{lstlisting}
\end{minipage}
\pause\hfill
\begin{minipage}{0.45\linewidth}
\begin{lstlisting}
Confucius says:
Man who chases two rabbits catches none.
\end{lstlisting}
\end{minipage}
\hfill\strut

\end{frame}

\begin{frame}[fragile]
\frametitle{Applying what we just learned}

\begin{description}[<+->]
\item[*]  \verb+\includecomment{problem}+\\[1em]
\item[student] \verb+\excludecomment{solution}+
\verb+\excludecomment{howtograde}+
\item[teacher] \verb+\includecomment{solution}+
\verb+\excludecomment{howtograde}+
\item[corrector] \verb+\includecomment{solution}+
\verb+\includecomment{howtograde}+
\end{description}

\end{frame}

\begin{frame}[fragile]
\frametitle{Let's start our own little uniflow.cls}

\begin{center}
\begin{minipage}{.75\linewidth}
\begin{lstlisting}[backgroundcolor=\color{gray!20}]
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{uniflow}

\RequirePackage{comment}
  \includecomment{problem}

\DeclareOption{student}{%
  \excludecomment{solution}
  \excludecomment{howtograde}}
\DeclareOption{teacher}{%
  \includecomment{solution}
  \excludecomment{howtograde}}
\DeclareOption{corrector}{%
  \includecomment{solution}
  \includecomment{howtograde}}
\DeclareOption*{%
  \PassOptionsToClass{\CurrentOption}{article}}

\ExecuteOptions{corrector} % set default view
\ProcessOptions\relax

\LoadClass{article}
\end{lstlisting}
\end{minipage}
\end{center}

\end{frame}

\subsection{Produce all versions in one go}

\begin{frame}
\frametitle{Batch document production}

One call of \LaTeX\ will only produce a single document file, so what can we do?
\begin{itemize}[<+->]
\item shell script
\item MakeFile
\item pseudo-executables
\item Something with LUA in it.
\end{itemize}
\pause\medskip but then again
\begin{itemize}[<+->]
\item requires extra script file
\item requires non-\TeX\ programs
\item may not be platform independent
\item requires beyond-\TeX\ knowledge
\end{itemize}
\pause so ain't there a genuine pdf\LaTeX\ approach?
\end{frame}

\begin{frame}[fragile]
\frametitle{Escaping to the Shell}

pdf\LaTeX\ can actually execute commands on the shell 
\begin{center}
\verb+\write18{echo "Hello World!"}+
\end{center}
\pause which also means pdf\LaTeX\ can call pdf\LaTeX\ ... way to go!  

\pause\medskip
\begin{center}
\dbend
\end{center}
Giving \LaTeX\ access to the shell is a gateway for exploits. Hence \verb+\write18+ is disabled by default. Only the switch
\begin{center}
\verb+pdflatex --shell-escape+
\end{center}
will enable shell access.

\end{frame}

\begin{frame}[fragile]
\frametitle{Rethinking command line calls}

Normal usage
\begin{center}
\verb+pdflatex Sheet01.tex+
\end{center}

\pause

is the same as
\begin{center}
\verb+pdflatex "\input{Sheet01.tex}"+
\end{center}

\pause

so we can insert a little code preceding the actual document
\begin{center}
\verb+pdflatex "\gdef\conditionmacro{student} \input{Sheet01.tex}"+
\end{center}

\pause hence we need to adapt our \texttt{uniflow.cls} to react to \verb+\conditionmacro+ (later).

\end{frame}

\begin{frame}[fragile]
\frametitle{A neat trick by Ulrike Fischer (TeX.SX \#5265)}

\begin{lstlisting}[backgroundcolor=\color{gray!20}, title={Sheet01.tex}]
\ifx\conditionmacro\undefined
  \immediate\write18{%
    pdflatex --jobname=\jobname-student
    "\gdef\string\conditionmacro{student}
     \string\input\space\jobname"
  }
  \expandafter\stop
\fi
...
\end{lstlisting}

\pause\medskip

\begin{itemize}[<+->]
\item \verb+\immediate+: execute command immediately upon parse
\item \verb+\expandafter+: postpone expanding \verb+\stop+, read \verb+\fi+ first
\item \verb+\stop+: Don't read any further 
\end{itemize}

\end{frame}

\begin{frame}[fragile]
\frametitle{A neat trick by Ulrike Fischer (TeX.SX \#5265)}

\begin{lstlisting}[backgroundcolor=\color{gray!20}, title={Sheet01.tex}]
\ifx\conditionmacro\undefined
  \immediate\write18{%
    pdflatex --jobname=\jobname-student
    "\gdef\string\conditionmacro{student}
     \string\input\space\jobname"
  }
  \expandafter\stop
\fi
...
\end{lstlisting}

\medskip

So what happens here?
\begin{itemize}[<+->]
\item \verb+pdflatex --shell-escape Sheet01.tex+ reads until \verb+\fi+ and stops
\item So far no output
\item \verb+\write18+ has called \verb+pdflatex+ on the shell
\item The \verb+\ifx+-block is ignored because \verb+\conditionmacro+ is now defined
\item This will produce \verb+Sheet01-students.pdf+
\end{itemize}

\end{frame}

\begin{frame}[fragile]
\frametitle{Adapting the class file}

\begin{lstlisting}[backgroundcolor=\color{gray!20}]
[...]
\newif\ifuniFirstOrder\uniFirstOrdertrue

\DeclareOption{batch}{%
  [\immediate\write18-Trick]
}

\DeclareOption{student}{%
  \ifuniFirstOrder
    \excludecomment{solution}
    \excludecomment{howtograde}
  \fi
}
[...]
\ifx\conditionmacro\undefined
  \ExecuteOptions{corrector} % set default view
\else
  \ExecuteOptions{\conditionmacro}
  \uniFirstOrderfalse
\fi
\ProcessOptions\relax
\end{lstlisting}

\end{frame}

\subsection{Harness the power of freeware math software}

\begin{frame}
\frametitle{For simplicity of comprehension ...}

\begin{itemize}[<+->]
\item we will not pursue adapting \texttt{uniflow.sty}
\item we will focus on implementing Ulrike's trick
\item references to survey articles are provided
\item if you can't understand the math -- feel it
\item this section is Linux-only -- get used to it...
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{Algebra using Sage}

About Sage and Sage\TeX\

\begin{itemize}[<+->]
\item \url{http://www.sagemath.org}
\item Sage is developed primarily for Ubuntu
\item The Windows version is a VirtualBox containing Ubuntu with Sage
\item Hence Sage\TeX\ will not work using a Windows-\LaTeX\
\item Be sure to use \texttt{sagetex.sty} that came with your Sage installation
\end{itemize}

\pause\medskip How to compile:

\begin{verbatim}
pdflatex SageExample.tex
sage SageExample.sagetex.sage
pdflatex SageExample.tex
\end{verbatim}

\pause
\begin{tabbing}
\textbf{Credits:} \= \quad \= Günter Rau\\
\>\> \enquote{Sage\TeX{}}, \href{http://archiv.dante.de/DTK/PDF/komoedie_2011_1.pdf}{DTK 2011-1}, p. 17ff
\end{tabbing}
\end{frame}

\begin{frame}[fragile]
\frametitle{Statistics using R}

About R and Sweave

\begin{itemize}[<+->]
\item R is the freeware alternative to commercial SPlus
\item Highly used in psychological and educational research
\item \LaTeX{}-Plugin Sweave: \url{http://www.statistik.lmu.de/~leisch/Sweave/}
\item Used NoWeb markup, hence the file extension .Rnw
\item Hence bash completion is a pitfall when using Ulrike's trick 
\end{itemize}

\pause\medskip How to compile:

\begin{verbatim}
R CMD Sweave SweaveExample.Rnw
pdflatex SweaveExample.tex
\end{verbatim}

\pause
\begin{tabbing}
\textbf{Credits:} \= \quad \= Uwe Ziegenhagen\\ \>\> \enquote{Datenanalyse mit Sweave, \LaTeX und R}, \href{http://www.dante.de/DTK/Ausgaben/dtk104.pdf}{DTK 2010-4}, p. 35ff
\end{tabbing}
\end{frame}

\AtBeginSubsection[]{}


\end{document}

