4

enter image description here

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{tikzpicture}
\draw[step=0.5cm,color=gray] (-1.5,-1.5) grid (1.5,1.5);
\matrix[matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm}]{
6 & 9 & 7 & 2 & 6 & 4 & 5\\
2 & 6 & 9 & 7 & 4 & 6 & 5\\
2 & 4 & 6 & 9 & 7 & 5 & 6\\
2 & 4 & 5 & 6 & 9 & 7 & 6\\
2 & 4 & 5 & 6 & 6 & 9 & 7\\
2 & 4 & 5 & 6 & 6 & 7 & 9\\};
\end{tikzpicture}

How can I fix this grid? I think I have tried everything and yet it only crosses through the number :(

1
  • Try \matrix[xshift=0.25cm,... Commented Jun 20, 2020 at 17:45

2 Answers 2

6

Just draw the contours of each cell with for example : every node/.style={draw}

screenshot

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
%\draw[step=0.5cm,color=gray] (-1.5,-1.5) grid (1.5,1.5);
\matrix[matrix of nodes,nodes={inner sep=0pt,text width=.5cm,align=center,minimum height=.5cm},every node/.style={draw}]{
6 & 9 & 7 & 2 & 6 & 4 & 5\\
2 & 6 & 9 & 7 & 4 & 6 & 5\\
2 & 4 & 6 & 9 & 7 & 5 & 6\\
2 & 4 & 5 & 6 & 9 & 7 & 6\\
2 & 4 & 5 & 6 & 6 & 9 & 7\\
2 & 4 & 5 & 6 & 6 & 7 & 9\\};
\end{tikzpicture}
\end{document}
1
  • Thanks a bunch man Commented Jun 20, 2020 at 18:40
5

A small variation of @AndréC answer:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
    \matrix[matrix of nodes,
            nodes = {draw, minimum size=5mm, 
                     anchor=center, 
                     inner sep=0pt, outer sep=0pt},
            column sep=-\pgflinewidth,
            row sep=-\pgflinewidth,
            ]
{
6 & 9 & 7 & 2 & 6 & 4 & 5\\
2 & 6 & 9 & 7 & 4 & 6 & 5\\
2 & 4 & 6 & 9 & 7 & 5 & 6\\
2 & 4 & 5 & 6 & 9 & 7 & 6\\
2 & 4 & 5 & 6 & 6 & 9 & 7\\
2 & 4 & 5 & 6 & 6 & 7 & 9\\
};
    \end{tikzpicture}
\end{document}

enter image description here

Note:

  • cells style is defined by nodes (not needed separate style for every nodes)
  • distance between cells are controlled by
column sep=-\pgflinewidth,
row sep=-\pgflinewidth,

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .