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 can't figure out how to create a Shidoku grid using LaTeX. In other words, I am trying to have a 4x4 grid with a bold line separating the 2x2 subsquares. I have the sudoku package but it won't work when I try to decrease the number of rows and columns. Any help is greatly appreciated.

Edit: I am using the following document classes:

\documentclass{ltxdoc}
\usepackage{sudoku}
\usepackage{natbib}
\usepackage{amsmath}
share|improve this question
add comment

3 Answers 3

up vote 13 down vote accepted

Here's a possible solution using a tabular:

\documentclass{article}
\usepackage{array}

\newcolumntype{P}{%
>{\rule[-0.6cm]{0pt}{1.5cm}\centering$}p{1cm}<{$}}

\begin{document}

\noindent\begin{tabular}{!{\vrule width 2pt}P|P!{\vrule width 2pt}P|P!{\vrule width 2pt}}
\noalign{\hrule height 2pt}
a & b & c & d \tabularnewline
\hline
a & b & c & d \tabularnewline
\noalign{\hrule height 2pt}
a & b & c & d \tabularnewline
\hline
a & b & c & d \tabularnewline
\noalign{\hrule height 2pt}
\end{tabular}

\end{document}

enter image description here

And a solution using TikZ:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}

\begin{document}

\begin{tikzpicture}
% the matrix
\matrix [matrix of math nodes, nodes in empty cells,
     nodes={align=center},
    text width=1cm,text depth=0.4cm,text height=0.6cm] 
(shi)
{
a & b & c & d \\
a & b & c & d \\
a & b & c & d \\
a & b & c & d \\
};

% vertical rules
\foreach \i in {1,3}
  \draw (shi-1-\i.north east) -- (shi-4-\i.south east);
\foreach \i in {1,3}
  \draw[line width=2pt] (shi-1-\i.north west) -- (shi-4-\i.south west);
\draw[line width=2pt] (shi-1-4.north east) -- (shi-4-4.south east);

% horizontal rules
\foreach \i in {2,4}
  \draw (shi-\i-1.north west) -- (shi-\i-4.north east);
\foreach \i in {2,4}
  \draw[line width=2pt] (shi-\i-1.south west) -- (shi-\i-4.south east);
\draw[line width=2pt] (shi-1-1.north west) -- (shi-1-4.north east);
\end{tikzpicture}

\end{document}

enter image description here

And here's my first approach using a simpe tabular but now I've added the necessary font adjustments and defined an environment to simplify the writing; now a Shidoku is produce simply by something like:

\begin{shidoku}
\Srow{2}{5}{}{}
\Srow{}{1}{}{}
\Srow{4}{}{7}{}
\Srow{}{}{5}{2}
\end{shidoku}

Here's the code:

\documentclass{article}
\usepackage{sudoku}
\usepackage{array}

\newcolumntype{P}{%
>{\rule[-6pt]{0pt}{30pt}\centering\Huge\sffamily}p{19pt}}

\newcommand\TPLine{\noalign{\hrule height 2pt}}

\newcounter{shrow}
\newenvironment{shidoku}
  {\setcounter{shrow}{0}\begin{center}\begin{tabular}{!{\vrule width 2pt}P|P!{\vrule width 2pt}P|P!{\vrule width 2pt}}}
  {\end{tabular}\end{center}}

\newcommand\Srow[4]{%
  \ifnum\value{shrow}=0 \TPLine\fi\stepcounter{shrow}%
  #1 & #2 & #3 & #4\tabularnewline
  \ifnum\value{shrow}=2 \TPLine\else\ifnum\value{shrow}=4 \TPLine\else\hline\fi\fi}

\begin{document}

\begin{shidoku}
\Srow{2}{5}{}{}
\Srow{}{1}{}{}
\Srow{4}{}{7}{}
\Srow{}{}{5}{2}
\end{shidoku}

\begin{sudoku}
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
\end{sudoku}

\end{document}

enter image description here

share|improve this answer
    
Thank you for your speedy reply! Unfortunately, when I use that formula, I get ! Use of \@array doesn't match its definition. –  Katherine M. Mar 30 '12 at 5:38
1  
@KatherineM.: when you use what formula? –  Gonzalo Medina Mar 30 '12 at 5:39
1  
@KatherineM. which document class are you using? You didn't include that information in your previous comment. It would be a good idea if you add to your original question a minimal complete example of the code that is not working for you. –  Gonzalo Medina Mar 30 '12 at 6:25
1  
@KatherineM. now that I see your edit, you are loading the array package (as in my example), aren't you? –  Gonzalo Medina Mar 30 '12 at 6:34
1  
@KatherineM. So you can use a different document class? If that is so, then change \documentclass{ltxdoc} to \documentclass{article} and process my example code. If you really need to use ltxdoc, then let me know and I will try to detect the problem. –  Gonzalo Medina Mar 30 '12 at 6:44
show 5 more comments

I have modified the sudoku.sty for you and here are the contents of the 4x4 sudoku44.sty file. Save the contents below in the name sudoku44.sty and keep it in the same directory as your .tex file. (Alternatively put the file in C:\Program Files\MiKTeX 2.9\tex\latex\sudoku\ and run a refresh file name data base if on windows.)

%%
%% This is file `sudoku44.sty'.
%%
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{sudoku}
 [2006/03/25 v1.0.1 Sudoku grid]
\newcounter{@sudoku@row}
\newcounter{@sudoku@col}
\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
}
\begingroup
        \catcode`\|=\active
        \gdef\@sudoku@activate{\let|=\@sudoku@separator}
\endgroup
\newcommand*\@sudoku@grid{
        \linethickness{\sudokuthinline}%
        \multiput(0,0)(1,0){5}{\line(0,1){4}}%
        \multiput(0,0)(0,1){5}{\line(1,0){4}}
        \linethickness{\sudokuthickline}%
        \multiput(0,0)(2,0){2}{\line(0,1){4}}%
        \multiput(0,0)(0,2){2}{\line(1,0){4}}
        \linethickness{0.5\sudokuthickline}%
        \put(0,0){\framebox(0,0){}}%
        \put(4,0){\framebox(0,0){}}%
        \put(0,4){\framebox(4,0){}}%
        \put(4,0){\framebox(0,4){}}
}
\newcommand*\sudokuformat[1]{\Huge\sffamily#1}
\newdimen\sudokusize
\setlength\sudokusize{10cm}
\newdimen\sudokuthinline
\setlength\sudokuthinline{0.4pt}
\newdimen\sudokuthickline
\setlength\sudokuthickline{2pt}
\newenvironment{sudoku-block}{%
        \catcode`\|=\active
        \@sudoku@activate
        \setcounter{@sudoku@col}{-1}%
        \setcounter{@sudoku@row}{3}%
        \setlength\unitlength{.111111\sudokusize}%
        \begin{picture}(4,4)%
        \@sudoku@grid\@sudoku@grab@arguments
        }{\end{picture}}
 \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.{%
       \scantokens{#1.#2.#3.#4.}}%
   }
\newenvironment{sudoku}{%
        \begin{center}%
        \begin{sudoku-block}}{\end{sudoku-block}\end{center}}
\endinput
%%
%% End of file `sudoku44.sty'.

Now your main .tex file (say mysudoku.tex) will be like this:

%-------------sudoku--------------------------------
\documentclass{article}
\usepackage{sudoku44}
%---------------------------------------------------
\begin{document}
\begin{sudoku}
|2|5| | |.
| |1| | |.
|4| |7| |.
| | |5|2|.
\end{sudoku}
%---------------------------------------------------
\end{document}
%---------------------------------------------------

After compiling (with pdflatex, my preference), we get

enter image description here

NOTE: Do not erase or modify the original sudoku.sty file. Save the new file with a name sudoku44.sty. Then you can use both the files like

\usepackage{sudoku}

or

\usepackage{sudoku44}

But you can not use both the packages in the same mysudoku.tex file as they clash.

share|improve this answer
    
Thanks for your help, do you know if modifying the sudoku.sty file means that I won't be able to input sudoku files? –  Katherine M. Mar 30 '12 at 6:54
1  
@KatherineM. You should not tamper the original sudoku.sty file. Leave it to itself. Copy the contents given above in a different file and name it sudoku44.sty (say). Then you can use the original 9x9 sudoku also. –  Harish Kumar Mar 30 '12 at 7:07
    
I am very new to LaTeX and though you have taken the time to modify the code, I have no idea what to do with that. –  Katherine M. Mar 30 '12 at 7:17
1  
@KatherineM. copy the contents of sudoku44 (the first code block) and paste in your editor. Save it as sudoku44.sty. Place this file in the same folder as your main mysudoku.tex (which I mentioned in the answer) that contains the second code block above. Compile mysudoku.tex. you are done -:) –  Harish Kumar Mar 30 '12 at 7:25
    
+1 from a sudoku fan –  karlkoeller Jun 18 '13 at 15:49
show 1 more comment

You don't need to modify sudoku.sty, just to use its infrastructure: we can use the macros as modified by Harish Kumar, but changing their names. In this way you can input both sudoku and shidoku puzzles using a similar syntax.

\documentclass{article}
\usepackage{sudoku}

\makeatletter
\newcommand*\@shidoku@grid{
  \linethickness{\sudokuthinline}%
  \multiput(0,0)(1,0){5}{\line(0,1){4}}%
  \multiput(0,0)(0,1){5}{\line(1,0){4}}
  \linethickness{\sudokuthickline}%
  \multiput(0,0)(2,0){2}{\line(0,1){4}}%
  \multiput(0,0)(0,2){2}{\line(1,0){4}}
  \linethickness{0.5\sudokuthickline}%
  \put(0,0){\framebox(0,0){}}%
  \put(4,0){\framebox(0,0){}}%
  \put(0,4){\framebox(4,0){}}%
  \put(4,0){\framebox(0,4){}}
}
\newenvironment{shidoku-block}{%
        \catcode`\|=\active
        \@sudoku@activate
        \setcounter{@sudoku@col}{-1}%
        \setcounter{@sudoku@row}{3}%
        \setlength\unitlength{.111111\sudokusize}%
        \begin{picture}(4,4)%
        \@shidoku@grid\@shidoku@grab@arguments
        }{\end{picture}}

\def\@shidoku@grab@arguments#1.#2.#3.#4.{\scantokens{#1.#2.#3.#4.}}
\newenvironment{shidoku}{%
        \begin{center}%
        \begin{shidoku-block}}{\end{shidoku-block}\end{center}}
\makeatother

\begin{document}
\begin{shidoku}
|2|5| | |.
| |1| | |.
|4| |7| |.
| | |5|2|.
\end{shidoku}

\begin{sudoku}
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
| | | | | | | | | |.
\end{sudoku}

\end{document}

enter image description here

share|improve this answer
1  
+1 Very nice trick. –  Harish Kumar Mar 30 '12 at 9:52
1  
@HarishKumar I've borrowed your nice code, of course. –  egreg Mar 30 '12 at 9:55
1  
Thank you. Any how, your idea is very good. I liked it. –  Harish Kumar Mar 30 '12 at 9:57
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.