我需要用 LaTeX 排版数独表格,到目前为止,我已经能够使用该logicpuzzle
软件包完成大部分工作。缺少的少数功能之一是能够在单元格内写入提示,列出可以放入该单元格的所有可能数字。
看看包装文档,我没有看到单独使用包来执行此操作的选项,所以我想知道是否应该完全使用不同的包,或者是否有办法扩展logicpuzzle
以使其显示这一点。
到目前为止,我所拥有的如下:
\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{logicpuzzle}
\begin{document}
\tableofcontents
\newpage
\chapter{Sudoku 1}
\begin{logicpuzzle}[rows=9,columns=9,color=orange!50,scale=1.5]
\framepuzzle
\framearea{black}{(1,7)--(4,7)--(4,10)--(1,10)}
\framearea{black}{(4,7)--(7,7)--(7,10)--(4,10)}
\framearea{black}{(7,7)--(10,7)--(10,10)--(7,10)}
\framearea{black}{(1,4)--(4,4)--(4,7)--(1,7)}
\framearea{black}{(4,4)--(7,4)--(7,7)--(7,4)}
\framearea{black}{(7,4)--(10,4)--(10,7)--(10,7)}
\framearea{black}{(1,1)--(1,4)--(4,4)--(4,1)}
\framearea{black}{(4,1)--(7,1)--(7,4)--(4,4)}
\setrow{9}{{},{},{3},{4},{},{5},{6},{},{}}
\setrow{8}{{7},{},{},{},{},{},{},{},{}}
\setrow{7}{{8},{},{},{},{},{},{},{},{}}
\setrow{6}{{9},{},{},{},{},{},{},{},{7}}
\setrow{5}{{},{},{},{},{},{},{},{},{8}}
\setrow{4}{{},{},{},{},{},{},{},{},{9}}
\setrow{3}{{},{},{},{},{},{},{},{},{}}
\setrow{2}{{},{},{},{},{},{},{},{},{}}
\setrow{1}{{},{},{},{},{},{},{},{},{}}
\fillcell{1}{9}
\fillcell{9}{9}
\end{logicpuzzle}
\end{document}
这是结果数独:
有没有办法使用该logicpuzzle
包在数独单元内排版数字提示?
理想情况下,它们看起来应该类似于这个问题:
答案1
我为此定义了一些简单的宏。
\documentclass[tikz]{standalone}
\newcommand\inserthint[3]{%
\foreach \i in {#3} {
\ifnum\i<4
\draw ({(#1-0.5)+(\i*0.25)},#2+0.25) node[font=\scriptsize] {\i};
\else
\ifnum\i>6
\draw ({(#1-2)+(\i*0.25)},#2-0.25) node[font=\scriptsize] {\i};
\else
\draw ({(#1-1.25)+(\i*0.25)},#2) node[font=\scriptsize] {\i};
\fi
\fi
}
}
\begin{document}
\begin{tikzpicture}
\draw[very thick,step=3] (0,0) grid (9,9);
\draw (0,0) grid (9,9);
\begin{scope}[every node/.style={minimum size=1cm},xshift=-.5cm,yshift=-.5cm]
\renewcommand{\arraystretch}{1}
\node at (1,6) {9};
\node at (1,7) {8};
\node at (1,8) {7};
\node at (3,9) {3};
\node at (4,9) {4};
\node at (6,9) {5};
\node at (7,9) {6};
\node at (9,6) {7};
\node at (9,5) {8};
\node at (9,4) {9};
\end{scope}
\begin{scope}[xshift=-.5cm,yshift=-.5cm]
\inserthint{1}{9}{1,...,9}
\inserthint{4}{6}{2,5,6,3,7}
\end{scope}
\end{tikzpicture}
\end{document}
如果需要填充,我们应该稍微重新安排一下代码。对于“普通”节点,你只需要添加 option fill=...
。对于“提示”节点,我为宏添加了一个参数。
\documentclass[tikz]{standalone}
\newcommand\inserthint[4]{%
\fill[#4] (#1-.5,#2-.5) rectangle (#1+.5,#2+.5);
\foreach \i in {#3} {
\ifnum\i<4
\draw ({(#1-0.5)+(\i*0.25)},#2+0.25) node[font=\scriptsize] {\i};
\else
\ifnum\i>6
\draw ({(#1-2)+(\i*0.25)},#2-0.25) node[font=\scriptsize] {\i};
\else
\draw ({(#1-1.25)+(\i*0.25)},#2) node[font=\scriptsize] {\i};
\fi
\fi
}
}
\begin{document}
\begin{tikzpicture}
\begin{scope}[every node/.style={minimum size=1cm},xshift=-.5cm,yshift=-.5cm]
\renewcommand{\arraystretch}{1}
\node at (1,6) {9};
\node at (1,7) {8};
\node at (1,8) {7};
\node at (3,9) {3};
\node at (4,9) {4};
\node at (6,9) {5};
\node at (7,9) {6};
\node at (9,6) {7};
\node at (9,5) {8};
\node[fill=blue!30] at (9,4) {9};
\node[fill=red!30] at (2,3) {};
\end{scope}
\begin{scope}[xshift=-.5cm,yshift=-.5cm]
\inserthint{1}{9}{1,...,9}{yellow}
\inserthint{4}{6}{2,5,6,3,7}{green!50}
\end{scope}
\draw[very thick,step=3] (0,0) grid (9,9);
\draw (0,0) grid (9,9);
\end{tikzpicture}
\end{document}