| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% sudoku.dtx |
| 4 |
% Copyright 2005,2006 Paul Abraham <[email protected]> |
| 5 |
% |
| 6 |
% This work may be distributed and/or modified under the |
| 7 |
% conditions of the LaTeX Project Public License, either |
| 8 |
% version 1.3 of this license or (at your option) any |
| 9 |
% later version. |
| 10 |
% The latest version of this license is in |
| 11 |
% http://www.latex-project.org/lppl.txt |
| 12 |
% and version 1.3 or later is part of all distributions of |
| 13 |
% LaTeX version 2003/12/01 or later. |
| 14 |
% |
| 15 |
% This work has the LPPL maintenance status "maintained". |
| 16 |
% |
| 17 |
% This Current Maintainer of this work is Paul Abraham. |
| 18 |
% |
| 19 |
% This work consists of the files sudoku.dtx, sudoku.ins |
| 20 |
% README, CHANGES and the derived file sudoku.sty. |
| 21 |
% |
| 22 |
% \fi |
| 23 |
% |
| 24 |
% \iffalse |
| 25 |
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01] |
| 26 |
%<package>\ProvidesPackage{sudoku} |
| 27 |
%<package> [2006/03/25 v1.0.1 Sudoku grid] |
| 28 |
% |
| 29 |
%<*driver> |
| 30 |
\documentclass{ltxdoc} |
| 31 |
\usepackage{sudoku} |
| 32 |
\EnableCrossrefs |
| 33 |
\CodelineIndex |
| 34 |
\RecordChanges |
| 35 |
\begin{document} |
| 36 |
\DocInput{sudoku.dtx} |
| 37 |
\PrintChanges |
| 38 |
\PrintIndex |
| 39 |
\end{document} |
| 40 |
%</driver> |
| 41 |
% \fi |
| 42 |
% |
| 43 |
% \CheckSum{104} |
| 44 |
% |
| 45 |
% \CharacterTable |
| 46 |
% {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 |
| 47 |
% 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 |
| 48 |
% Digits \0\1\2\3\4\5\6\7\8\9 |
| 49 |
% Exclamation \! Double quote \" Hash (number) \# |
| 50 |
% Dollar \$ Percent \% Ampersand \& |
| 51 |
% Acute accent \' Left paren \( Right paren \) |
| 52 |
% Asterisk \* Plus \+ Comma \, |
| 53 |
% Minus \- Point \. Solidus \/ |
| 54 |
% Colon \: Semicolon \; Less than \< |
| 55 |
% Equals \= Greater than \> Question mark \? |
| 56 |
% Commercial at \@ Left bracket \[ Backslash \\ |
| 57 |
% Right bracket \] Circumflex \^ Underscore \_ |
| 58 |
% Grave accent \` Left brace \{ Vertical bar \| |
| 59 |
% Right brace \} Tilde \~} |
| 60 |
% |
| 61 |
% \changes{v1.0}{2005/06/29}{Initial version} |
| 62 |
% \changes{v1.0.1}{2006/03/25}{Added ability to specify line thickness} |
| 63 |
% |
| 64 |
% \GetFileInfo{sudoku.sty} |
| 65 |
% |
| 66 |
% \DoNotIndex{\newcommand,\newenvironment} |
| 67 |
% \setlength{\parindent}{0pt} |
| 68 |
% \setlength{\parskip}{5pt plus 2pt minus 1pt} |
| 69 |
% |
| 70 |
% \title{The \textsf{sudoku} package\thanks{This document |
| 71 |
% corresponds to \textsf{sudoku}~\fileversion, dated \filedate.}} |
| 72 |
% \author{Paul Abraham \\ \texttt{[email protected]}} |
| 73 |
% |
| 74 |
% \maketitle |
| 75 |
% |
| 76 |
% \section{Introduction} |
| 77 |
% |
| 78 |
% The \textsf{sudoku} package allows the user to typeset |
| 79 |
% sudoku\footnote{I~have seen Sudoku written as Sudoku |
| 80 |
% (one word) and Su Doku (two words). I don't know which of them (if |
| 81 |
% either) is more correct than the other. I am just using one word.} |
| 82 |
% puzzles. |
| 83 |
% |
| 84 |
% A~sudoku puzzle is a 9$\times$9 grid where some of the squares in |
| 85 |
% the grid contain numbers. The rules are simple: |
| 86 |
% every column can only contain the digits 1 to 9, |
| 87 |
% every row can only contain the digits 1 to 9 and |
| 88 |
% every 3$\times$3 box can only contain the digits 1 to 9. |
| 89 |
% |
| 90 |
% The puzzle derives its name from the Japanese \textit{Su}, meaning |
| 91 |
% number, and \textit{Doku\/} meaning singular or solitary. |
| 92 |
% |
| 93 |
% More information, including help and example puzzles, can be found |
| 94 |
% at \texttt{www.sudoku.org.uk}. This site also has blank sudoku grids |
| 95 |
% (or worksheets), but you will not need to print them from there if |
| 96 |
% you have this package installed. |
| 97 |
% |
| 98 |
% \section{Acknowledgements} |
| 99 |
% |
| 100 |
% I~want to thank Morten H\o gholm who took my first attempt at |
| 101 |
% creating a sudoku environment in \LaTeX\ and corrected all my |
| 102 |
% mistakes. He then gave me the help and encouragement to publish |
| 103 |
% it as a package. |
| 104 |
% |
| 105 |
% \section{Usage} |
| 106 |
% |
| 107 |
% \DescribeMacro{\sudokuformat} |
| 108 |
% This macro contains the formatting information for each number |
| 109 |
% in the grid. The default value is \verb|\Huge\sffamily|, which |
| 110 |
% gives correctly sized numbers for the default grid size, but it |
| 111 |
% can be changed with \verb|\renewcommand*\sudokuformat[1]{\|\meta{font}|#1}|. |
| 112 |
% |
| 113 |
% \DescribeMacro{\sudokusize} |
| 114 |
% This length contains the size of the grid, defaulting to \verb|10cm|. |
| 115 |
% It can be changed with \verb|\setlength\sudokusize{|\meta{size}|}|. |
| 116 |
% |
| 117 |
% \DescribeMacro{\sudokuthickline} |
| 118 |
% This length determines the thickness of the thick lines and defaults |
| 119 |
% to \verb|2pt|. It can be changed with |
| 120 |
% \verb|\setlength\sudokuthickline{|\meta{size}|}|. |
| 121 |
% |
| 122 |
% \DescribeMacro{\sudokuthinline} |
| 123 |
% This length determines the thickness of the thin lines and defaults |
| 124 |
% to \verb|0.4pt|. It can be changed with |
| 125 |
% It can be changed with \verb|\setlength\sudokuthinline{|\meta{size}|}|. |
| 126 |
% |
| 127 |
% \DescribeEnv{sudoku-block} |
| 128 |
% This environment draws the sudoku grid. The contents of each cell |
| 129 |
% in the grid are delimited by the vertical bar (\verb+|+) character. |
| 130 |
% |
| 131 |
% \DescribeEnv{sudoku} |
| 132 |
% This environment starts a \verb|center| environment, then uses the |
| 133 |
% \verb|sudoku-block| environment to draw the grid. |
| 134 |
% |
| 135 |
% \section{Examples} |
| 136 |
% |
| 137 |
% This should be a relatively easy puzzle to solve. |
| 138 |
% |
| 139 |
% \begin{verbatim} |
| 140 |
% \begin{sudoku} |
| 141 |
% |2|5| | |3| |9| |1|. |
| 142 |
% | |1| | | |4| | | |. |
| 143 |
% |4| |7| | | |2| |8|. |
| 144 |
% | | |5|2| | | | | |. |
| 145 |
% | | | | |9|8|1| | |. |
| 146 |
% | |4| | | |3| | | |. |
| 147 |
% | | | |3|6| | |7|2|. |
| 148 |
% | |7| | | | | | |3|. |
| 149 |
% |9| |3| | | |6| |4|. |
| 150 |
% \end{sudoku} |
| 151 |
% \end{verbatim} |
| 152 |
% The output is shown in figure~\ref{easypuzzle}. The solution is shown in |
| 153 |
% figure~\ref{easysolution}. |
| 154 |
% |
| 155 |
% \begin{figure}[t] |
| 156 |
% \begin{sudoku} |
| 157 |
% |2|5| | |3| |9| |1|. |
| 158 |
% | |1| | | |4| | | |. |
| 159 |
% |4| |7| | | |2| |8|. |
| 160 |
% | | |5|2| | | | | |. |
| 161 |
% | | | | |9|8|1| | |. |
| 162 |
% | |4| | | |3| | | |. |
| 163 |
% | | | |3|6| | |7|2|. |
| 164 |
% | |7| | | | | | |3|. |
| 165 |
% |9| |3| | | |6| |4|. |
| 166 |
% \end{sudoku} |
| 167 |
% \caption{Easy puzzle\label{easypuzzle}} |
| 168 |
% \end{figure} |
| 169 |
% |
| 170 |
% This is a bit more difficult. |
| 171 |
% |
| 172 |
% \begin{verbatim} |
| 173 |
% \begin{sudoku} |
| 174 |
% | |2| | |3| |9| |7|. |
| 175 |
% | |1| | | | | | | |. |
| 176 |
% |4| |7| | | |2| |8|. |
| 177 |
% | | |5|2| | | |9| |. |
| 178 |
% | | | |1|8| |7| | |. |
| 179 |
% | |4| | | |3| | | |. |
| 180 |
% | | | | |6| | |7|1|. |
| 181 |
% | |7| | | | | | | |. |
| 182 |
% |9| |3| |2| |6| |5|. |
| 183 |
% \end{sudoku} |
| 184 |
% \end{verbatim} |
| 185 |
% The output is shown in figure~\ref{difficultpuzzle}. The solution is shown in |
| 186 |
% figure~\ref{difficultsolution}. |
| 187 |
% |
| 188 |
% \begin{figure}[t] |
| 189 |
% \begin{sudoku} |
| 190 |
% | |2| | |3| |9| |7|. |
| 191 |
% | |1| | | | | | | |. |
| 192 |
% |4| |7| | | |2| |8|. |
| 193 |
% | | |5|2| | | |9| |. |
| 194 |
% | | | |1|8| |7| | |. |
| 195 |
% | |4| | | |3| | | |. |
| 196 |
% | | | | |6| | |7|1|. |
| 197 |
% | |7| | | | | | | |. |
| 198 |
% |9| |3| |2| |6| |5|. |
| 199 |
% \end{sudoku} |
| 200 |
% \caption{Difficult puzzle\label{difficultpuzzle}} |
| 201 |
% \end{figure} |
| 202 |
% |
| 203 |
% \newpage |
| 204 |
% This code will create the same grid inline: |
| 205 |
% \begin{verbatim} |
| 206 |
% \renewcommand*\sudokuformat[1]{\sffamily#1} |
| 207 |
% \setlength\sudokusize{5cm} |
| 208 |
% Easy Sudoku Puzzle |
| 209 |
% \begin{sudoku-block} |
| 210 |
% |2|5| | |3| |9| |1|. |
| 211 |
% | |1| | | |4| | | |. |
| 212 |
% |4| |7| | | |2| |8|. |
| 213 |
% | | |5|2| | | | | |. |
| 214 |
% | | | | |9|8|1| | |. |
| 215 |
% | |4| | | |3| | | |. |
| 216 |
% | | | |3|6| | |7|2|. |
| 217 |
% | |7| | | | | | |3|. |
| 218 |
% |9| |3| | | |6| |4|. |
| 219 |
% \end{sudoku-block} |
| 220 |
% \end{verbatim} |
| 221 |
% |
| 222 |
% \StopEventually{} |
| 223 |
% |
| 224 |
%\iffalse |
| 225 |
% 2005/06/29 v1.0 Initial version |
| 226 |
% 2006/03/25 v1.0.1 Added ability to specify line thickness |
| 227 |
%\fi |
| 228 |
% |
| 229 |
% \section{Implementation} |
| 230 |
% |
| 231 |
% First initialise a couple of counters that keep track of where we are. |
| 232 |
% \begin{macrocode} |
| 233 |
\newcounter{@sudoku@row} |
| 234 |
\newcounter{@sudoku@col} |
| 235 |
% \end{macrocode} |
| 236 |
% |
| 237 |
% \begin{macrocode} |
| 238 |
\newcommand*\@sudoku@separator[1]{% |
| 239 |
\stepcounter{@sudoku@col}% |
| 240 |
\ifx#1\@sudoku@separator |
| 241 |
\expandafter#1% |
| 242 |
\else |
| 243 |
\ifx.#1% |
| 244 |
\setcounter{@sudoku@col}{-1}% |
| 245 |
\addtocounter{@sudoku@row}{-1}% |
| 246 |
\else |
| 247 |
\put(\value{@sudoku@col},\value{@sudoku@row})% |
| 248 |
{\makebox(1,1){\sudokuformat{#1}}}% |
| 249 |
\fi |
| 250 |
\fi |
| 251 |
} |
| 252 |
% \end{macrocode} |
| 253 |
% |
| 254 |
% \begin{macrocode} |
| 255 |
\begingroup |
| 256 |
\catcode`\|=\active |
| 257 |
\gdef\@sudoku@activate{\let|=\@sudoku@separator} |
| 258 |
\endgroup |
| 259 |
% \end{macrocode} |
| 260 |
% |
| 261 |
% Now we can draw the grid. Each 3$\times$3 box has thicker borders |
| 262 |
% than the rest of the grid. From version 1.0.1, the line thickness |
| 263 |
% can be determined by changing \verb|\sudokuthinline| and |
| 264 |
% \verb|\sudokuthickline|. |
| 265 |
% |
| 266 |
% \begin{macrocode} |
| 267 |
\newcommand*\@sudoku@grid{ |
| 268 |
% \end{macrocode} |
| 269 |
% |
| 270 |
% First draw the thin lines. |
| 271 |
% |
| 272 |
% \begin{macrocode} |
| 273 |
\linethickness{\sudokuthinline}% |
| 274 |
\multiput(0,0)(1,0){10}{\line(0,1){9}}% |
| 275 |
\multiput(0,0)(0,1){10}{\line(1,0){9}} |
| 276 |
% \end{macrocode} |
| 277 |
% |
| 278 |
% Now draw the thick lines. |
| 279 |
% |
| 280 |
% \begin{macrocode} |
| 281 |
\linethickness{\sudokuthickline}% |
| 282 |
\multiput(0,0)(3,0){4}{\line(0,1){9}}% |
| 283 |
\multiput(0,0)(0,3){4}{\line(1,0){9}} |
| 284 |
% \end{macrocode} |
| 285 |
% |
| 286 |
% Finally try and fill in the four corners of the grid. |
| 287 |
% |
| 288 |
% \begin{macrocode} |
| 289 |
\linethickness{0.5\sudokuthickline}% |
| 290 |
\put(0,0){\framebox(0,0){}}% |
| 291 |
\put(9,0){\framebox(0,0){}}% |
| 292 |
\put(0,9){\framebox(0,0){}}% |
| 293 |
\put(9,9){\framebox(0,0){}} |
| 294 |
} |
| 295 |
% \end{macrocode} |
| 296 |
% |
| 297 |
% The default font is a huge, sans serif font. |
| 298 |
% \begin{macrocode} |
| 299 |
\newcommand*\sudokuformat[1]{\Huge\sffamily#1} |
| 300 |
% \end{macrocode} |
| 301 |
% |
| 302 |
% The grid defaults to a 10 centimetre square. |
| 303 |
% \begin{macrocode} |
| 304 |
\newdimen\sudokusize |
| 305 |
\setlength\sudokusize{10cm} |
| 306 |
% \end{macrocode} |
| 307 |
% |
| 308 |
% Set line thicknesses to 0.4pt and 2pt for thin and thick lines. |
| 309 |
% \begin{macrocode} |
| 310 |
\newdimen\sudokuthinline |
| 311 |
\setlength\sudokuthinline{0.4pt} |
| 312 |
\newdimen\sudokuthickline |
| 313 |
\setlength\sudokuthickline{2pt} |
| 314 |
% \end{macrocode} |
| 315 |
% |
| 316 |
% Make the size of each square $1/9$ of the size of sudoku grid. |
| 317 |
% \begin{macrocode} |
| 318 |
\newenvironment{sudoku-block}{% |
| 319 |
\catcode`\|=\active |
| 320 |
\@sudoku@activate |
| 321 |
\setcounter{@sudoku@col}{-1}% |
| 322 |
\setcounter{@sudoku@row}{8}% |
| 323 |
\setlength\unitlength{.111111\sudokusize}% |
| 324 |
\begin{picture}(9,9)% |
| 325 |
\@sudoku@grid\@sudoku@grab@arguments |
| 326 |
}{\end{picture}} |
| 327 |
% \end{macrocode} |
| 328 |
% |
| 329 |
% As we change the \verb|catcode| of \verb+|+ inside the environment, |
| 330 |
% under normal \TeX\ we can't just use such an environment as an argument |
| 331 |
% of another command. This can be solved by using e\TeX\ as engine for |
| 332 |
% \LaTeX, which is what all the major distributions do currently. |
| 333 |
% \begin{macrocode} |
| 334 |
\begingroup |
| 335 |
\@ifundefined{eTeXversion}{\endgroup |
| 336 |
\def\@sudoku@grab@arguments#1{% |
| 337 |
\ifx#1\@sudoku@separator |
| 338 |
\expandafter#1% |
| 339 |
\else |
| 340 |
\PackageError{sudoku}{% |
| 341 |
Sudoku puzzles are not allowed in the arguments\MessageBreak |
| 342 |
of other commands}{% |
| 343 |
Either fix this in your source (possibly using the `lrbox' |
| 344 |
environment) or use an eTeX based LaTeX format} |
| 345 |
\fi |
| 346 |
}% |
| 347 |
}{\endgroup |
| 348 |
\def\@sudoku@grab@arguments#1.#2.#3.#4.#5.#6.#7.#8.#9.{% |
| 349 |
\scantokens{#1.#2.#3.#4.#5.#6.#7.#8.#9.}}% |
| 350 |
} |
| 351 |
% \end{macrocode} |
| 352 |
% |
| 353 |
% The |sudoku| environment simply calls the |sudoku-block| |
| 354 |
% environment inside a |center| environment. |
| 355 |
% \begin{macrocode} |
| 356 |
\newenvironment{sudoku}{% |
| 357 |
\begin{center}% |
| 358 |
\begin{sudoku-block}}{\end{sudoku-block}\end{center}} |
| 359 |
% \end{macrocode} |
| 360 |
% |
| 361 |
% \Finale |
| 362 |
% |
| 363 |
% \begin{figure}[b] |
| 364 |
% \begin{sudoku} |
| 365 |
% |2|5|8|7|3|6|9|4|1|. |
| 366 |
% |6|1|9|8|2|4|3|5|7|. |
| 367 |
% |4|3|7|9|1|5|2|6|8|. |
| 368 |
% |3|9|5|2|7|1|4|8|6|. |
| 369 |
% |7|6|2|4|9|8|1|3|5|. |
| 370 |
% |8|4|1|6|5|3|7|2|9|. |
| 371 |
% |1|8|4|3|6|9|5|7|2|. |
| 372 |
% |5|7|6|1|4|2|8|9|3|. |
| 373 |
% |9|2|3|5|8|7|6|1|4|. |
| 374 |
% \end{sudoku} |
| 375 |
% \caption{Solution for easy puzzle\label{easysolution}} |
| 376 |
% \end{figure} |
| 377 |
% |
| 378 |
% \begin{figure}[b] |
| 379 |
% \begin{sudoku} |
| 380 |
% |6|2|8|5|3|4|9|1|7|. |
| 381 |
% |5|1|9|8|7|2|4|3|6|. |
| 382 |
% |4|3|7|9|1|6|2|5|8|. |
| 383 |
% |8|6|5|2|4|7|1|9|3|. |
| 384 |
% |3|9|2|1|8|5|7|6|4|. |
| 385 |
% |7|4|1|6|9|3|5|8|2|. |
| 386 |
% |2|5|4|3|6|9|8|7|1|. |
| 387 |
% |1|7|6|4|5|8|3|2|9|. |
| 388 |
% |9|8|3|7|2|1|6|4|5|. |
| 389 |
% \end{sudoku} |
| 390 |
% \caption{Solution for difficult puzzle\label{difficultsolution}} |
| 391 |
% \end{figure} |
| 392 |
% |
| 393 |
\endinput |