如何创建 Shidoku 网格?

如何创建 Shidoku 网格?

我不知道如何使用 LaTeX 创建 Shidoku 网格。换句话说,我试图创建一个 4x4 网格,并用粗线将 2x2 子方块分开。我有这个sudoku包,但当我尝试减少行数和列数时它不起作用。任何帮助都将不胜感激。

编辑:我正在使用以下文档类别:

\documentclass{ltxdoc}
\usepackage{sudoku}
\usepackage{natbib}
\usepackage{amsmath}

答案1

这是一个使用 的可能解决方案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}

在此处输入图片描述

使用 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}

在此处输入图片描述

这是我第一次使用 simpe 的方法tabular,但现在我添加了必要的字体调整,并定义了一个环境来简化书写;现在 Shidoku 的生成只需通过以下方式:

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

代码如下:

\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}

在此处输入图片描述

答案2

需要修改sudoku.sty,才能使用其基础结构:我们可以使用 Harish Kumar 修改的宏,但更改其名称。这样,您可以使用类似的语法输入数独和四独谜题。

\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}

在此处输入图片描述

答案3

我已经sudoku.sty为您修改了,以下是 4x4 文件的内容sudoku44.sty。将下面的内容保存在名称中sudoku44.sty,并将其保存在与您的文件相同的目录中.tex。(或者将文件放入C:\Program Files\MiKTeX 2.9\tex\latex\sudoku\并在 Windows 上运行refresh file name data baseif。)

%%
%% 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'.

现在你的主.tex文件(比如 mysudoku.tex)将会像这样:

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

编译后(使用 pdflatex,我偏爱),我们得到

在此处输入图片描述

笔记:不要删除或修改原始sudoku.sty文件。使用名称保存新文件sudoku44.sty。然后,您可以使用这两个文件,例如

\usepackage{sudoku}

或者

\usepackage{sudoku44}

但是您不能在同一个mysudoku.tex文件中使用这两个包,因为它们会发生冲突。

相关内容