I would have used a Perl script to generate LaTeX scripts. But that's me.
Edit:
Well, this is what I love about Stackoverflow: lots of interesting and fun problems to solve. My wife is kind of a Sudoku junkie, so I thought printing a couple of booklets to keep her happy wouldn't hurt.
Note: A sample result PDF file is available at Scribd.
First, I went to CTAN (in case you don't know, this is CPAN's big brother.) I found a module for LaTeX called (what else?) sudoku. I read the documentation and deemed it good enough.
Secondly, I wanted it in booklet format. This time, the module I found on CTAN was too obscure and difficult to use, so I kept searching and found an article named LaTeX: creating a5 booklets written by Sven Hartenstein. The first line on this article says "this is how I produce A5 booklets printed on A4 paper (which is then folded once) with LaTeX ..."
Last, I searched CPAN for a good Sudoku generator. There are lots of solvers, but I just wanted to print a puzzle, so I settled with Games::Sudoku::Component and I wrote a little Perl script for generate the TeX file, called sudoku.pl:
#!/usr/bin/perl
use strict;
use warnings;
use Games::Sudoku::Component;
my $sudoku = Games::Sudoku::Component->new(size => 9);
print '
\documentclass[a5paper,headsepline,titlepage,10pt,normalheadings,DIVcalc]{scrbook}
\usepackage{sudoku}
\begin{document}
\title{Sudoku}
\author{L. Herrera}
\maketitle
\renewcommand*\sudokuformat[1]{\Large\sffamily#1}
\setlength\sudokusize{6cm}
\setlength\sudokuthickline{1pt}
';
print "\\chapter*{Easy puzzles}\n";
foreach (1 .. 16) {
print "\\section*{Puzzle $_}\n";
$sudoku->generate(blanks => 50);
my $puzzle = $sudoku->as_string(
separator => '|',
linebreak => "|.\n|",
);
$puzzle =~ s/0/ /gs;
print "\\begin{sudoku}\n|", $puzzle, "|.\n";
print "\\end{sudoku}\n\n\n";
}
print "\\end{document}\n";
Then, I copied verbatim the script created by Sven and named it booklet.sh
:
#!/bin/sh
#
# This script takes an A5 latex file as input and produces an A4
# document (both ps and pdf) for printing an A5 booklet
#
# Usage:
# booklet [filename without extension]
latex $1
latex $1
# make an a5 ps from dvi:
dvips -t a5 -o $1.ps $1.dvi
# sort pages
psbook $1.ps tmp1.ps
# put two pages on one a4 sheet
psnup -Pa5 -pa4 -2 tmp1.ps tmp2.ps
# change a5 to a4 in ps file
sed 's/^%%DocumentPaperSizes: A5/%%DocumentPaperSizes: a4/g' < tmp2.ps > $1_booklet.ps
# produce pdf files, pdflatex ist used (twice) to get bookmarks
pdflatex $1.tex
pdflatex $1.tex
ps2pdf -sPAPERSIZE=a4 $1_booklet.ps
# delete tmp files
rm tmp1.ps tmp2.ps
Now, I regenerate a small booklet ready for print just by executing
perl sudoku.pl > puzzle1.tex && ./booklet.sh puzzle1