[Tex/LaTex] Multiple outputs for beamer handouts

beamer

I'm currently generating some presentations with beamer for students of my course. Some like to print the material, so I created a handout. Some students prefer to have 2,3,4,6 etc. pages per sheet of A4 paper. How can I go about to automatize the following code, so that it outputs 4 different files in 1×2,1×3,2×2,2×3 format?

I'd like to avoid having to compile it again everytime for the different formats.

\documentclass[a4paper]{article}
\usepackage{pdfpages}

\begin{document}
\includepdf[pages=1-last,nup=1x2,landscape=false,frame=true,
            noautoscale=false,scale=1,delta=0mm 5mm]{Pres7Chap13-Handout.pdf}
\end{document}

Best Answer

Compile this with pdflatex option --shell-escape:

\documentclass[a4paper]{article}
\usepackage{pdfpages}
\usepackage{filecontents}
%
\begin{filecontents*}{1x2.tex}
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=1-last,nup=1x2,landscape=false,frame=true,
            noautoscale=false,scale=1,delta=0mm 5mm]{template.pdf}
\end{document}
\end{filecontents*}
%
\begin{filecontents*}{1x3.tex}
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=1-last,nup=1x3,landscape=false,frame=true,
            noautoscale=false,scale=1,delta=0mm 5mm]{template.pdf}
\end{document}
\end{filecontents*}
%
\begin{filecontents*}{2x2.tex}
\documentclass[a4paper]{article}
\usepackage{pdfpages}
\begin{document}
\includepdf[pages=1-last,nup=2x2,landscape=false,frame=true,
            noautoscale=false,scale=1,delta=0mm 5mm]{template.pdf}
\end{document}
\end{filecontents*}
%
\immediate\write18{pdflatex 1x2}
\immediate\write18{pdflatex 1x3}
\immediate\write18{pdflatex 2x2}
%
\begin{document}
\includepdf[pages=1-last,nup=2x3,landscape=false,frame=true,
            noautoscale=false,scale=1,delta=0mm 5mm]{template.pdf}
\end{document}

You will get 3 new .tex files - 1x2.tex, 1x3.tex and 2x2.tex and 4 pdf files correspondingly (3 + 1) in the same folder.