% \iffalse meta-comment
%
% sudoku.dtx
% Copyright 2005,2006 Paul Abraham <paul.abraham@linuxdirect.org>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3 of this license or (at your option) any
% later version.
% The latest version of this license is in
%     http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of
% LaTeX version 2003/12/01 or later.
%
% This work has the LPPL maintenance status "maintained".
% 
% This Current Maintainer of this work is Paul Abraham.
%
% This work consists of the files sudoku.dtx, sudoku.ins
% README, CHANGES and the derived file sudoku.sty.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{sudoku}
%<package>	[2006/03/25 v1.0.1 Sudoku grid]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{sudoku}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{sudoku.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{104}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \changes{v1.0}{2005/06/29}{Initial version}
% \changes{v1.0.1}{2006/03/25}{Added ability to specify line thickness}
%
% \GetFileInfo{sudoku.sty}
%
% \DoNotIndex{\newcommand,\newenvironment}
% \setlength{\parindent}{0pt}
% \setlength{\parskip}{5pt plus 2pt minus 1pt}
%
% \title{The \textsf{sudoku} package\thanks{This document
%   corresponds to \textsf{sudoku}~\fileversion, dated \filedate.}}
% \author{Paul Abraham \\ \texttt{paul.abraham@linuxdirect.org}}
%
% \maketitle
%
% \section{Introduction}
%
% The \textsf{sudoku} package allows the user to typeset
% sudoku\footnote{I~have seen Sudoku written as Sudoku
% (one word) and Su Doku (two words).  I don't know which of them (if
% either) is more correct than the other.  I am just using one word.}
%  puzzles.
%
% A~sudoku puzzle is a 9$\times$9 grid where some of the squares in
% the grid contain numbers.  The rules are simple:
% every column can only contain the digits 1 to 9,
% every row can only contain the digits 1 to 9 and
% every 3$\times$3 box can only contain the digits 1 to 9.
%
% The puzzle derives its name from the Japanese \textit{Su}, meaning
% number, and \textit{Doku\/} meaning singular or solitary.
%
% More information, including help and example puzzles, can be found
% at \texttt{www.sudoku.org.uk}.  This site also has blank sudoku grids
% (or worksheets), but you will not need to print them from there if
% you have this package installed.
%
% \section{Acknowledgements}
%
% I~want to thank Morten H\o gholm who took my first attempt at
% creating a sudoku environment in \LaTeX\ and corrected all my
% mistakes.  He then gave me the help and encouragement to publish
% it as a package.
%
% \section{Usage}
%
% \DescribeMacro{\sudokuformat}
% This macro contains the formatting information for each number
% in the grid.  The default value is \verb|\Huge\sffamily|, which
% gives correctly sized numbers for the default grid size, but it
% can be changed with \verb|\renewcommand*\sudokuformat[1]{\|\meta{font}|#1}|.
%
% \DescribeMacro{\sudokusize}
% This length contains the size of the grid, defaulting to \verb|10cm|.
% It can be changed with \verb|\setlength\sudokusize{|\meta{size}|}|.
%
% \DescribeMacro{\sudokuthickline}
% This length determines the thickness of the thick lines and defaults
% to \verb|2pt|.  It can be changed with
% \verb|\setlength\sudokuthickline{|\meta{size}|}|.
%
% \DescribeMacro{\sudokuthinline}
% This length determines the thickness of the thin lines and defaults
% to \verb|0.4pt|.  It can be changed with
% It can be changed with \verb|\setlength\sudokuthinline{|\meta{size}|}|.
%
% \DescribeEnv{sudoku-block}
% This environment draws the sudoku grid.  The contents of each cell
% in the grid are delimited by the vertical bar (\verb+|+) character.
%
% \DescribeEnv{sudoku}
% This environment starts a \verb|center| environment, then uses the
% \verb|sudoku-block| environment to draw the grid.
%
% \section{Examples}
%
% This should be a relatively easy puzzle to solve.
%
% \begin{verbatim}
% \begin{sudoku}
% |2|5| | |3| |9| |1|.
% | |1| | | |4| | | |.
% |4| |7| | | |2| |8|.
% | | |5|2| | | | | |.
% | | | | |9|8|1| | |.
% | |4| | | |3| | | |.
% | | | |3|6| | |7|2|.
% | |7| | | | | | |3|.
% |9| |3| | | |6| |4|.
% \end{sudoku}
% \end{verbatim}
% The output is shown in figure~\ref{easypuzzle}.  The solution is shown in
% figure~\ref{easysolution}.
%
% \begin{figure}[t]
% \begin{sudoku}
% |2|5| | |3| |9| |1|.
% | |1| | | |4| | | |.
% |4| |7| | | |2| |8|.
% | | |5|2| | | | | |.
% | | | | |9|8|1| | |.
% | |4| | | |3| | | |.
% | | | |3|6| | |7|2|.
% | |7| | | | | | |3|.
% |9| |3| | | |6| |4|.
% \end{sudoku}
% \caption{Easy puzzle\label{easypuzzle}}
% \end{figure}
%
% This is a bit more difficult.
%
% \begin{verbatim}
% \begin{sudoku}
% | |2| | |3| |9| |7|.
% | |1| | | | | | | |.
% |4| |7| | | |2| |8|.
% | | |5|2| | | |9| |.
% | | | |1|8| |7| | |.
% | |4| | | |3| | | |.
% | | | | |6| | |7|1|.
% | |7| | | | | | | |.
% |9| |3| |2| |6| |5|.
% \end{sudoku}
% \end{verbatim}
% The output is shown in figure~\ref{difficultpuzzle}.  The solution is shown in
% figure~\ref{difficultsolution}.
%
% \begin{figure}[t]
% \begin{sudoku}
% | |2| | |3| |9| |7|.
% | |1| | | | | | | |.
% |4| |7| | | |2| |8|.
% | | |5|2| | | |9| |.
% | | | |1|8| |7| | |.
% | |4| | | |3| | | |.
% | | | | |6| | |7|1|.
% | |7| | | | | | | |.
% |9| |3| |2| |6| |5|.
% \end{sudoku}
% \caption{Difficult puzzle\label{difficultpuzzle}}
% \end{figure}
%
% \newpage
% This code will create the same grid inline:
% \begin{verbatim}
% \renewcommand*\sudokuformat[1]{\sffamily#1}
% \setlength\sudokusize{5cm} 
% Easy Sudoku Puzzle
% \begin{sudoku-block}
% |2|5| | |3| |9| |1|.
% | |1| | | |4| | | |.
% |4| |7| | | |2| |8|.
% | | |5|2| | | | | |.
% | | | | |9|8|1| | |.
% | |4| | | |3| | | |.
% | | | |3|6| | |7|2|.
% | |7| | | | | | |3|.
% |9| |3| | | |6| |4|.
% \end{sudoku-block}
% \end{verbatim}
%
% \StopEventually{}
%
%\iffalse
% 2005/06/29 v1.0 Initial version
% 2006/03/25 v1.0.1 Added ability to specify line thickness
%\fi
%
% \section{Implementation}
%
% First initialise a couple of counters that keep track of where we are.
%    \begin{macrocode}
\newcounter{@sudoku@row}
\newcounter{@sudoku@col}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand*\@sudoku@separator[1]{%
        \stepcounter{@sudoku@col}%
        \ifx#1\@sudoku@separator
                \expandafter#1%
        \else
                \ifx.#1%
                        \setcounter{@sudoku@col}{-1}%
                        \addtocounter{@sudoku@row}{-1}%
                \else
                        \put(\value{@sudoku@col},\value{@sudoku@row})%
                        {\makebox(1,1){\sudokuformat{#1}}}%
                \fi
        \fi
}
%    \end{macrocode}
%
%    \begin{macrocode}
\begingroup
        \catcode`\|=\active
        \gdef\@sudoku@activate{\let|=\@sudoku@separator}
\endgroup
%    \end{macrocode}
%
% Now we can draw the grid.  Each 3$\times$3 box has thicker borders
% than the rest of the grid.  From version 1.0.1, the line thickness
% can be determined by changing \verb|\sudokuthinline| and
% \verb|\sudokuthickline|.
%
%    \begin{macrocode}
\newcommand*\@sudoku@grid{
%    \end{macrocode}
%
% First draw the thin lines.
%
%    \begin{macrocode}
        \linethickness{\sudokuthinline}%
        \multiput(0,0)(1,0){10}{\line(0,1){9}}%
        \multiput(0,0)(0,1){10}{\line(1,0){9}}
%    \end{macrocode}
%
% Now draw the thick lines.
%
%    \begin{macrocode}
        \linethickness{\sudokuthickline}%
        \multiput(0,0)(3,0){4}{\line(0,1){9}}%
        \multiput(0,0)(0,3){4}{\line(1,0){9}}
%    \end{macrocode}
%
% Finally try and fill in the four corners of the grid.
%
%    \begin{macrocode}
        \linethickness{0.5\sudokuthickline}%
        \put(0,0){\framebox(0,0){}}%
        \put(9,0){\framebox(0,0){}}%
        \put(0,9){\framebox(0,0){}}%
        \put(9,9){\framebox(0,0){}}
}
%    \end{macrocode}
%
% The default font is a huge, sans serif font.
%    \begin{macrocode}
\newcommand*\sudokuformat[1]{\Huge\sffamily#1}
%    \end{macrocode}
%
% The grid defaults to a 10 centimetre square.
%    \begin{macrocode}
\newdimen\sudokusize
\setlength\sudokusize{10cm}
%    \end{macrocode}
%
% Set line thicknesses to 0.4pt and 2pt for thin and thick lines.
%    \begin{macrocode}
\newdimen\sudokuthinline
\setlength\sudokuthinline{0.4pt}
\newdimen\sudokuthickline
\setlength\sudokuthickline{2pt}
%    \end{macrocode}
%
% Make the size of each square $1/9$ of the size of sudoku grid.
%    \begin{macrocode}
\newenvironment{sudoku-block}{%
        \catcode`\|=\active
        \@sudoku@activate
        \setcounter{@sudoku@col}{-1}%
        \setcounter{@sudoku@row}{8}%
        \setlength\unitlength{.111111\sudokusize}%
        \begin{picture}(9,9)%
        \@sudoku@grid\@sudoku@grab@arguments
        }{\end{picture}}
%    \end{macrocode}
%
% As we change the \verb|catcode| of \verb+|+ inside  the environment,
% under normal \TeX\ we can't just use such an environment as an argument
% of another command. This can be solved by using e\TeX\ as engine for
% \LaTeX, which is what all the major distributions do currently.
%    \begin{macrocode}
 \begingroup
   \@ifundefined{eTeXversion}{\endgroup
     \def\@sudoku@grab@arguments#1{%
       \ifx#1\@sudoku@separator
         \expandafter#1%   
       \else
          \PackageError{sudoku}{%
            Sudoku puzzles are not allowed in the arguments\MessageBreak
            of other commands}{%
            Either fix this in your source (possibly using the `lrbox'
            environment) or use an eTeX based LaTeX format}
        \fi
     }%
   }{\endgroup
     \def\@sudoku@grab@arguments#1.#2.#3.#4.#5.#6.#7.#8.#9.{%
       \scantokens{#1.#2.#3.#4.#5.#6.#7.#8.#9.}}%
   }
%    \end{macrocode}
%
% The |sudoku| environment simply calls the |sudoku-block|
% environment inside a |center| environment.
%    \begin{macrocode}
\newenvironment{sudoku}{%
        \begin{center}%
        \begin{sudoku-block}}{\end{sudoku-block}\end{center}}
%    \end{macrocode}
%
% \Finale
%
% \begin{figure}[b]
% \begin{sudoku}
% |2|5|8|7|3|6|9|4|1|.
% |6|1|9|8|2|4|3|5|7|.
% |4|3|7|9|1|5|2|6|8|.
% |3|9|5|2|7|1|4|8|6|.
% |7|6|2|4|9|8|1|3|5|.
% |8|4|1|6|5|3|7|2|9|.
% |1|8|4|3|6|9|5|7|2|.
% |5|7|6|1|4|2|8|9|3|.
% |9|2|3|5|8|7|6|1|4|.
% \end{sudoku}
% \caption{Solution for easy puzzle\label{easysolution}}
% \end{figure}
%
% \begin{figure}[b]
% \begin{sudoku}
% |6|2|8|5|3|4|9|1|7|.
% |5|1|9|8|7|2|4|3|6|.
% |4|3|7|9|1|6|2|5|8|.
% |8|6|5|2|4|7|1|9|3|.
% |3|9|2|1|8|5|7|6|4|.
% |7|4|1|6|9|3|5|8|2|.
% |2|5|4|3|6|9|8|7|1|.
% |1|7|6|4|5|8|3|2|9|.
% |9|8|3|7|2|1|6|4|5|.
% \end{sudoku}
% \caption{Solution for difficult puzzle\label{difficultsolution}}
% \end{figure}
%
\endinput
