Is there a way to draw Sudoku (and other) grids?

Can we use it to draw grids, like those needed for Sudoku and other puzzles?

The following MathJax code leads to the grid below it.

$$\begin{array}{|c|c|c|} \hline \\ 1 & \; & \fbox{3} \\ \hline \\ 4 & 5 & 6 \\ \hline \\ 7 & \; & \tiny{\begin{array}{ccc} & 2 & \\ & \fbox{5} & \\ & 8 & 9 \end{array}} \\ \hline \end{array}$$


$$\begin{array}{|c|c|c|} \hline \\ 1 & \; & \fbox{3} \\ \hline \\ 4 & 5 & 6 \\ \hline \\ 7 & \; & \tiny{\begin{array}{ccc} & 2 & \\ & \fbox{5} & \\ & 8 & 9 \end{array}} \\ \hline \end{array}$$

Here we see a single Sudoku block, with several cells filled in. There is one given, 3; the other values are a partial solution. There are two empty cells and there's one cell with a number of hints, one of which is highlighted.

Of course, this is far from perfect and even just barely usable. It needs equally spaced cells, minimal padding (especially in the cell with hints there's way too much vertical padding above it), another way to separate givens from parts of the solution (or another way to highlight hints), thicker horizontal and vertical lines to define the blocks, and probably much more.

Also, the syntax isn't easy. Ideally, we should be able to do something like \begin{grid{sudoku}}{1,,(3), ...},{4,5,6, ...},{7,,{,2,,,[5],,,8,9}, ...}, ...}\end{grid} and have the grid drawn.

According to the TEX.SE there are several packages that can do that, but they are not part of MathJax, which is what we have here.

What can we do to draw better grids and how can we make drawing grids easier?

• Solutions might include a MathJax macro, defined once for this stack, or perhaps another plugin altogether.
– SQB
Commented May 22, 2014 at 9:52
• It wouldn't be terribly hard to write a MathJax macro because MathML makes equal cells relatively simple. Once there is a macro, the community could probably convince stackexchange to add it to the site. We'd be happy to help anyone who wants to give it a try. Commented May 30, 2014 at 15:04

Here is a slightly better version, but I think it needs work before being made into a macro for the site
- it has unnecessary spacing, both within cells and between blocks, so it spills over the sidebar (at least on my browser), maybe someone else can work out how to avoid that while still keeping everything aligned?

First a sudoku with relevant pencil marks (the small numbers showing what values an empty cell could take given only the direct information of what values are already taken in the three units they occupy [row, column, block]:

$\def\ph#1#2|{\tiny{\bbox[border:2px solid #2]{#1}}} \def\pn#1{\tiny{\bbox[border:2px]{#1}}} \def\pe{\tiny{\bbox[border:2px]{\phantom{0}}}} \def\vn#1{\begin{array}{}\pe&\pe&\pe\\&\rlap{\bbox[border:2px]{#1}}&\\\pe&\pe&\pe\end{array}} \def\vh#1#2|{\begin{array}{}\pe&\pe&\pe\\&\rlap{\bbox[border:2px solid #2]{#1}}&\\\pe&\pe&\pe\end{array}} \begin{array}{} \bbox[border:3px solid]{ \begin{array}{c|c|c} \vn1 & \begin{array}{}\pe&\pe&\pe\\\pn4&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pe\\\pn7&\pn8&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pn3\\\pe&\pe&\pe\\\pn7&\pn8&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pn7&\pe&\pn9\end{array} & \vn6 & \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pe\\\pn7&\pn8&\pn9\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pn8&\pe\end{array} & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pe\\\pn7&\pn8&\pn9\end{array} & \vn2 \\ \hline \vn1 & \vn4 & \begin{array}{}\pe&\pe&\pe\\\pe&\pe&\pn6\\\pe&\pn8&\pn9\end{array} \\ \hline \vn3 & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pe\\\pn7&\pn8&\pn9\end{array} & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pe\\\pe&\pn8&\pn9\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pn6\\\pn7&\pn8&\pn9\end{array} & \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pn6\\\pe&\pn8&\pn9\end{array} & \vn3 \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pn7&\pn8&\pn9\end{array} & \vn5 & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pn7&\pe&\pn9\end{array} \\ \hline \vn1 & \begin{array}{}\pe&\pn2&\pe\\\pn4&\pe&\pe\\\pe&\pn8&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pe\\\pn7&\pe&\pn9\end{array} \\ \end{array} } \\ \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pn2&\pn3\\\pe&\pe&\pn6\\\pe&\pe&\pe\end{array} & \begin{array}{}\pn1&\pn2&\pn3\\\pn4&\pe&\pe\\\pe&\pe&\pe\end{array} & \vn5 \\ \hline \vn8 & \begin{array}{}\pn1&\pn2&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pn1&\pe&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pe\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pn7&\pe&\pe\end{array} & \vn9 & \begin{array}{}\pn1&\pe&\pe\\\pn4&\pe&\pn6\\\pn7&\pe&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pn2&\pe\\\pn4&\pe&\pe\\\pe&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pn2&\pe\\\pe&\pe&\pe\\\pe&\pn8&\pn9\end{array} & \begin{array}{}\pn1&\pe&\pn3\\\pe&\pe&\pe\\\pe&\pn8&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \vn6 & \begin{array}{}\pn1&\pe&\pn3\\\pe&\pn5&\pe\\\pe&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pn4&\pn5&\pe\\\pn7&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pn2&\pe\\\pe&\pn5&\pe\\\pn7&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pe&\pe\\\pe&\pn5&\pe\\\pe&\pn8&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pe&\pn8&\pn9\end{array} & \vn7 & \begin{array}{}\pn1&\pn2&\pe\\\pe&\pe&\pn6\\\pe&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pe&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pe\\\pe&\pe&\pn9\end{array} & \vn4 \\ \hline \vn3 & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pe&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pn2&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pe\end{array} \\ \end{array} } \\ \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pn9\end{array} & \begin{array}{}\pn1&\pe&\pe\\\pe&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \vn2 \\ \hline \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pn9\end{array} & \vn8 & \begin{array}{}\pn1&\pe&\pe\\\pe&\pe&\pn6\\\pe&\pe&\pn9\end{array} \\ \hline \vn4 & \begin{array}{}\pe&\pe&\pn3\\\pe&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pn3\\\pe&\pe&\pn6\\\pn7&\pe&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pe&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pe&\pe\\\pe&\pn5&\pe\\\pe&\pn8&\pe\end{array} & \vn4 \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pe\end{array} & \vn3 & \vn7 \\ \hline \vn9 & \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pe&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pn9\end{array} & \vn3 & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pn4&\pn5&\pn6\\\pe&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pn4&\pe&\pn6\\\pe&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pe\end{array} & \vn1 & \vn8 \\ \end{array} } \end{array}$

Now we can demonstrate some of the features of the code...

Below we can highlight:

• $3$ hidden singles and the pencil marks they remove
- the large blue-boxed $1$s and red-boxed $8$ are the only ones in some unit that may be the respective value
- the pencil marks removed are also shown with boxes around them; and
• a pointing pair
- one of the green-boxed pencil marked $9$s must be a $9$ since they are the only ones left in that block
- so the cyan-boxed pencil marked $9$s may be removed

$\def\ph#1#2|{\tiny{\bbox[border:2px solid #2]{#1}}} \def\pn#1{\tiny{\bbox[border:2px]{#1}}} \def\pe{\tiny{\bbox[border:2px]{\phantom{0}}}} \def\vn#1{\begin{array}{}\pe&\pe&\pe\\&\rlap{\bbox[border:2px]{#1}}&\\\pe&\pe&\pe\end{array}} \def\vh#1#2|{\begin{array}{}\pe&\pe&\pe\\&\rlap{\bbox[border:2px solid #2]{#1}}&\\\pe&\pe&\pe\end{array}} \begin{array}{} \bbox[border:3px solid]{ \begin{array}{c|c|c} \vn1 & \begin{array}{}\pe&\pe&\pe\\\pn4&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pe\\\pn7&\pn8&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\ph9cyan|\end{array} & \begin{array}{}\pe&\pn2&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pn3\\\pe&\pe&\pe\\\pn7&\pn8&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pn7&\pe&\ph9cyan|\end{array} & \vn6 & \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pe\\\pn7&\pn8&\pn9\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\ph8red|&\pe\end{array} & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pe\\\pn7&\pn8&\pn9\end{array} & \vn2 \\ \hline \vn1 & \vn4 & \begin{array}{}\pe&\pe&\pe\\\pe&\pe&\pn6\\\pe&\pn8&\pn9\end{array} \\ \hline \vn3 & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pe\\\pn7&\pn8&\pn9\end{array} & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pe\\\pe&\pn8&\pn9\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pn6\\\pn7&\pn8&\pn9\end{array} & \begin{array}{}\pe&\pe&\pe\\\pn4&\pe&\pn6\\\pe&\pn8&\pn9\end{array} & \vn3 \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pn7&\pn8&\pn9\end{array} & \vn5 & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pn7&\pe&\pn9\end{array} \\ \hline \vn1 & \begin{array}{}\pe&\pn2&\pe\\\pn4&\pe&\pe\\\pe&\pn8&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pe\\\pn7&\pe&\pn9\end{array} \\ \end{array} } \\ \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pn2&\pn3\\\pe&\pe&\pn6\\\pe&\pe&\pe\end{array} & \begin{array}{}\pn1&\pn2&\pn3\\\pn4&\pe&\pe\\\pe&\pe&\pe\end{array} & \vn5 \\ \hline \vn8 & \begin{array}{}\pn1&\pn2&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\ph1blue|&\pe&\pn3\\\pe&\pe&\pe\\\pn7&\pe&\pe\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pn7&\pe&\pe\end{array} & \vn9 & \begin{array}{}\ph1blue|&\pe&\pe\\\pn4&\pe&\pn6\\\pn7&\pe&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pn2&\pe\\\pn4&\pe&\pe\\\pe&\ph8red|&\pe\end{array} & \begin{array}{}\ph1blue|&\pn2&\pe\\\pe&\pe&\pe\\\pe&\pn8&\pn9\end{array} & \begin{array}{}\pn1&\pe&\pn3\\\pe&\pe&\pe\\\pe&\pn8&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \vn6 & \begin{array}{}\pn1&\pe&\pn3\\\pe&\pn5&\pe\\\pe&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pn4&\pn5&\pe\\\pn7&\ph8red|&\pe\end{array} & \begin{array}{}\ph1blue|&\pn2&\pe\\\pe&\pn5&\pe\\\pn7&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pe&\pe\\\pe&\pn5&\pe\\\pe&\pn8&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pe&\pn8&\pn9\end{array} & \vn7 & \begin{array}{}\pn1&\pn2&\pe\\\pe&\pe&\pn6\\\pe&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pe&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pe\\\pe&\pe&\pn9\end{array} & \vn4 \\ \hline \vn3 & \begin{array}{}\pe&\pn2&\pe\\\pe&\pe&\pn6\\\pe&\pn8&\pe\end{array} & \begin{array}{}\pn1&\pn2&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pe\end{array} \\ \end{array} } \\ \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\ph9green|\end{array} & \begin{array}{}\ph1blue|&\pe&\pe\\\pe&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \vn2 \\ \hline \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\ph9green|\end{array} & \vn8 & \vh1blue| \\ \hline \vn4 & \begin{array}{}\pe&\pe&\pn3\\\pe&\pn5&\pe\\\pn7&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pn3\\\pe&\pe&\pn6\\\pn7&\pe&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \vh8red| & \vh1blue| & \vn4 \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pe\end{array} & \vn3 & \vn7 \\ \hline \vn9 & \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pe\\\pe&\pe&\pe\end{array} & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pe\end{array} \\ \end{array} } & \bbox[border:3px solid]{ \begin{array}{c|c|c} \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pn9\end{array} & \vn3 & \begin{array}{}\pe&\pe&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pn4&\pn5&\pn6\\\pe&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pn4&\pe&\pn6\\\pe&\pe&\pn9\end{array} & \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pn6\\\pe&\pe&\pn9\end{array} \\ \hline \begin{array}{}\pe&\pn2&\pe\\\pe&\pn5&\pn6\\\pn7&\pe&\pe\end{array} & \vn1 & \vn8 \\ \end{array} } \end{array}$

As an interim workaround, I use a screenshot tool to grab images quickly (there are a lot out there, but I use LightShot) off a Sudoku website.

I use this site to generate nice-looking Sudoku grids. This also allows for fast reduction of Sudoku possibilities and clues, which can also definitely be useful in an answer.

It's not as pretty as a custom module would be, but it's good enough for the time being until we get an official response.

• The link in your answer appears broken to me (the host is not resolvable). Commented May 23, 2014 at 20:19