summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/sudoku/sudoku.dtx
blob: 23c46369ace4b5f58fc82bcaf0424f9fc81e4b13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
% \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