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

Example output

I am trying to enter in each of the squares of this picture a number such that for each of the 6 "obvious" big squares, the entries are 0,1,2,3,4,5,6,7,8.

I thought maybe using a for loop, but I need to know how to enter the number in the center of the small squares.

Kind Regards.

LonLon
  • 442
Dedekid
  • 85
  • 1
    On attached image I see 54 squares. Where exactly you like to have numbers? Pleas, add this information to your image. – Zarko May 20 '21 at 08:16

4 Answers4

4

I guessing that you after the following:

enter image description here

A bit rude but working 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}

Zarko
  • 296,517
2

Updated my answer to include the borders as well:

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

The idea of using a tuple foreach is from Juan. The \usetikzlibrary{calc} is required for the calculations between dollar signs.

Grid with numbers and thick borders

Looks nice, and the code is rather compact.

2

If the numbers are correlative, as Zarko suggests, this can also be done like this.

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

enter image description here

Juan Castaño
  • 28,426
2

Regarding your request, that instead of numbers you have letters in squares. This can be obtained by defining new command in preamble:

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

and than each loop writes as (considering their chains names):

{
    \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}};% <---
}

Result of this changes is:

enter image description here

Zarko
  • 296,517