如何为每个方块输入一个解决方案?

如何为每个方块输入一个解决方案?
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{enumitem}
\usepackage{graphicx}
\usepackage[left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm]{geometry}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (6,0) -- (6,-24);
\draw (6,-24) -- (12,-24);
\draw (12,-24) -- (12,0);
\draw (6,0) -- (12,0);
\draw (0,-6) -- (18,-6);
\draw (18,-6) -- (18,-12);
\draw (18,-12) -- (0,-12);
\draw (0,-6) -- (0,-12);
\draw (6,-18) -- (12,-18);
\draw (8,0) -- (8,-24);
\draw (10,0) -- (10,-24);
\draw (0,-8) -- (18,-8);
\draw (0,-10) -- (18,-10);
\draw (6,-2) -- (12,-2);
\draw (6,-4) -- (12,-4);
\draw (6,-14) -- (12,-14);
\draw (6,-16) -- (12,-16);
\draw (6,-18) -- (12,-18);
\draw (6,-20) -- (12,-20);
\draw (6,-22) -- (12,-22);
\draw (2,-6) -- (2,-12);
\draw (4,-6) -- (4,-12);
\draw (14,-6) -- (14,-12);
\draw (16,-6) -- (16,-12);
\end{tikzpicture}
\end{document}

示例输出

我尝试在该图片的每个方块中输入一个数字,使得对于 6 个“明显”的大方块中的每一个,输入内容为 0、1、2、3、4、5、6、7、8。

我想也许可以使用 for 循环,但我需要知道如何在小方块的中心输入数字。

亲切的问候。

答案1

我猜你会考虑以下几点:

在此处输入图片描述

有点粗鲁但是有效的 MWE:

\documentclass[12pt,a4paper]{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{chains, positioning}

\begin{document}
\begin{center}
    \begin{tikzpicture}[
node distance = 0pt,
  start chain = A going right,
  start chain = B going right,
  start chain = C going right,
  start chain = D going right,
S/.style = {draw, minimum size=15mm, outer sep=0pt}
                        ]
\foreach \i in {0,1,2}
{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n11\i) [S, on chain=A] {\i};
\node (n12\i) [S,below=of n11\i] {\j};
\node (n13\i) [S,below=of n12\i] {\k};
}
\foreach \i in {0,1,2}
{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n21\i) [S, on chain=A] {\i};
\node (n22\i) [S,below=of n21\i] {\j};
\node (n23\i) [S,below=of n22\i] {\k};
}
\foreach \i in {0,1,2}
{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n31\i) [S, on chain=A] {\i};
\node (n32\i) [S,below=of n31\i] {\j};
\node (n33\i) [S,below=of n32\i] {\k};
}
%vertical, upper part
    \begin{scope}[shift={(45mm,45mm)}]
\foreach \i in {0,1,2}
{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n21\i) [S, on chain=B] {\i};
\node (n22\i) [S,below=of n21\i] {\j};
\node (n23\i) [S,below=of n22\i] {\k};
}
    \end{scope}
%vertical, lover part
    \begin{scope}[shift={(45mm,-45mm)}]
\foreach \i in {0,1,2}
{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n21\i) [S, on chain=C] {\i};
\node (n22\i) [S,below=of n21\i] {\j};
\node (n23\i) [S,below=of n22\i] {\k};
}
    \end{scope}
    \begin{scope}[shift={(45mm,-90mm)}]
\foreach \i in {0,1,2}
{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n31\i) [S, on chain=D] {\i};
\node (n32\i) [S,below=of n31\i] {\j};
\node (n23\i) [S,below=of n32\i] {\k};
}
    \end{scope}
    \end{tikzpicture}
\end{center}
\end{document}

答案2

如果数字是相关的,正如 Zarko 所建议的,也可以这样做。

\documentclass[border=2mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach\x/\y in {0/0, 0/3, -3/6, 0/6, 3/6, 0/9} \foreach\i in {0,1,2} \foreach\j in {0,1,2}
{%
  \draw (\x+\i,\y+\j) rectangle (\x+\i+1,\y+\j+1);
  \pgfmathtruncatemacro\n{3*(2-\j)+\i};
  \node at (\x+\i+0.5,\y+\j+0.5) {$\n$};
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

更新了我的答案以包括边框:

\documentclass[crop,border=10pt]{standalone}
\usepackage{tikz}

\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[every node/.style={font={\Huge\bfseries\sffamily},scale=1.5}]

\foreach\zoneX/\zoneY in {0/0, 0/1, 0/2, 0/3, -1/1, 1/1}{
    \foreach\tilenumber in {0,1,...,8} {
        % localX = tilenumber % 3 (modulo)
        % localY = tilenumber / 3 (integer division)
        \pgfmathtruncatemacro{\localX}{\tilenumber/3}
        \pgfmathtruncatemacro{\localX}{\tilenumber-\localX*3}
        \pgfmathtruncatemacro{\localY}{\tilenumber/3}
        \node at ($ (6*\zoneX + 2*\localX + 7, -6*\zoneY - 2*\localY - 1) $) {\tilenumber};
        \draw[line width=1.5pt] ($ (6*\zoneX + 2*\localX + 6, -6*\zoneY - 2*\localY) $) rectangle +(2,-2);
    }
    \draw[line width=6pt] ($ (6*\zoneX + 6, -6*\zoneY) $) rectangle +(6,-6);
}

\end{tikzpicture}
\end{document}

使用元组 foreach 的想法来自 Juan。\usetikzlibrary{calc}美元符号之间的计算需要。

带数字和粗边框的网格

看起来不错,并且代码相当紧凑。

答案4

关于您的要求,希望用方块中的字母代替数字。这可以通过在序言中定义新命令来实现:

\newcommand\makealph[1]{% see https://tex.stackexchange.com/questions/595043/converting-numbers-to-letters-but-starting-with-0-a-instead-of-1-a
\ifcase\numexpr#1\relax a\or b\or c\or d\or e\or f\or g\or h\or i\fi}

然后每个循环写为(考虑它们的链名称):

{
    \pgfmathsetmacro{\j}{int(\i + 3)}
    \pgfmathsetmacro{\k}{int(\i + 6)}
\node (n11\i) [S, on chain=A] {\makealph{\i}};   % <---
\node (n12\i) [S,below=of n11\i] {\makealph{\j}};% <---
\node (n13\i) [S,below=of n12\i] {\makealph{\k}};% <---
}

这一变化的结果是:

在此处输入图片描述

相关内容