Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am a fan of sudoku and I was wondering if it is possible to randomly generate sudoku tables with their solutions in LaTeX. I see a few problems in the generation of numbers in the tables, but, sincerely, I wouldn't know where to start.

share|improve this question
5  
The first step is to have an algorithm to produce Sudoku tables. If you have that, in pseudo-code, or any language you like if that's easier, then someone will probably step forward an implement it. –  Bruno Le Floch Feb 2 '12 at 19:53
    
I suggest to use the sudoku package. –  Thorsten Donig Feb 2 '12 at 20:03
add comment

3 Answers

up vote 19 down vote accepted

a package, sudokubundle, by peter wilson is on ctan. this appears to do what you're asking. it requires the random package to generate puzzle data.

share|improve this answer
    
I will look into it. –  azetina Feb 2 '12 at 20:05
    
I'm glad to see some answers get votes even if they don't include screenshots. (The default seems to be one has to include them in order to get votes....) –  Marc van Dongen Feb 3 '12 at 1:02
add comment

For typesetting Sudokus (well, LaTeX is mainly for typesetting, while calculations can be done for example with Lua, or pgf which provides a random number generator), Roberto Bonvallet has contributed a Sudoku example to the TikZ gallery.

Sudoku example

\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage{tikz}
\usepackage{mathpazo}
\PreviewEnvironment{tikzpicture}
\newcounter{row}
\newcounter{col}

\newcommand\setrow[9]{
  \setcounter{col}{1}
  \foreach \n in {#1, #2, #3, #4, #5, #6, #7, #8, #9} {
    \edef\x{\value{col} - 0.5}
    \edef\y{9.5 - \value{row}}
    \node[anchor=center] at (\x, \y) {\n};
    \stepcounter{col}
  }
  \stepcounter{row}
}

\begin{document}
\begin{tikzpicture}[scale=.5]

  \begin{scope}
    \draw (0, 0) grid (9, 9);
    \draw[very thick, scale=3] (0, 0) grid (3, 3);

    \setcounter{row}{1}
    \setrow { }{2}{ }  {5}{ }{1}  { }{9}{ }
    \setrow {8}{ }{ }  {2}{ }{3}  { }{ }{6}
    \setrow { }{3}{ }  { }{6}{ }  { }{7}{ }

    \setrow { }{ }{1}  { }{ }{ }  {6}{ }{ }
    \setrow {5}{4}{ }  { }{ }{ }  { }{1}{9}
    \setrow { }{ }{2}  { }{ }{ }  {7}{ }{ }

    \setrow { }{9}{ }  { }{3}{ }  { }{8}{ }
    \setrow {2}{ }{ }  {8}{ }{4}  { }{ }{7}
    \setrow { }{1}{ }  {9}{ }{7}  { }{6}{ }

    \node[anchor=center] at (4.5, -0.5) {Unsolved Sudoku};
  \end{scope}

  \begin{scope}[xshift=12cm]
    \draw (0, 0) grid (9, 9);
    \draw[very thick, scale=3] (0, 0) grid (3, 3);

    \setcounter{row}{1}
    \setrow { }{2}{ }  {5}{ }{1}  { }{9}{ }
    \setrow {8}{ }{ }  {2}{ }{3}  { }{ }{6}
    \setrow { }{3}{ }  { }{6}{ }  { }{7}{ }

    \setrow { }{ }{1}  { }{ }{ }  {6}{ }{ }
    \setrow {5}{4}{ }  { }{ }{ }  { }{1}{9}
    \setrow { }{ }{2}  { }{ }{ }  {7}{ }{ }

    \setrow { }{9}{ }  { }{3}{ }  { }{8}{ }
    \setrow {2}{ }{ }  {8}{ }{4}  { }{ }{7}
    \setrow { }{1}{ }  {9}{ }{7}  { }{6}{ }

    \node[anchor=center] at (4.5, -0.5) {Solved Sudoku};

    \begin{scope}[blue, font=\sffamily\slshape]
      \setcounter{row}{1}
      \setrow {4}{ }{6}  { }{7}{ }  {3}{ }{8}
      \setrow { }{5}{7}  { }{9}{ }  {1}{4}{ }
      \setrow {1}{ }{9}  {4}{ }{8}  {2}{ }{5}

      \setrow {9}{7}{ }  {3}{8}{5}  { }{2}{4}
      \setrow { }{ }{3}  {7}{2}{6}  {8}{ }{ }
      \setrow {6}{8}{ }  {1}{4}{9}  { }{5}{3}

      \setrow {7}{ }{4}  {6}{ }{2}  {5}{ }{1}
      \setrow { }{6}{5}  { }{1}{ }  {9}{3}{ }
      \setrow {3}{ }{8}  { }{5}{ }  {4}{ }{2}
    \end{scope}

  \end{scope}

\end{tikzpicture}
\end{document}
share|improve this answer
    
I guess this only displays the sudoku but does not generate it. Its a good idea though. Thanks for the input. –  azetina Feb 2 '12 at 22:00
add comment

For typesetting Sudokus, you can also use the logicpuzzle bundle [CTAN].

\begin{center}
  \begin{lpsudoku}
    \setrow{9}{{},2,6,{},{},{},{},{},{}}
    \setrow{8}{{},{},{},{},{},{},1,7,{}}
    \setrow{7}{{},{},3,1,{},6,{},{},{}}
    \setrow{6}{{},6,{},{},5,{},8,{},3}
    \setrow{5}{{},{},9,2,6,1,7,{},{}}
    \setrow{4}{5,{},4,{},8,{},{},6,{}}
    \setrow{3}{{},{},{},8,{},4,3,{},{}}
    \setrow{2}{{},4,8,{},{},{},{},{},{}}
    \setrow{1}{{},{},{},{},{},{},9,4,{}}
    \end{lpsudoku}
    \hspace{1.5cm}
    \begin{lpsudoku}
    \setrow{9}{1,2,6,5,7,8,4,3,9}
    \setrow{8}{4,8,5,9,3,2,1,7,6}
    \setrow{7}{7,9,3,1,4,6,5,8,2}
    \setrow{6}{2,6,1,4,5,7,8,9,3}
    \setrow{5}{8,3,9,2,6,1,7,5,4}
    \setrow{4}{5,7,4,3,8,9,2,6,1}
    \setrow{3}{6,5,2,8,9,4,3,1,7}
    \setrow{2}{9,4,8,7,1,3,6,2,5}
    \setrow{1}{3,1,7,6,2,5,9,4,8}
  \end{lpsudoku}
\end{center}

Sudoku

You can use the createlpsudoku bash script to transfer exported Sudokus from e.g. HoDoKu or QQwing into lpsudoku environments. See: How to create a Sudoku magazine

With the lpsmag bash script, you can even produce your own Sudoku magazines with solutions! Whenever you run the script, you have a new magazine.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.