将数字添加到网格 Tikz LaTeX

Add number to a grid Tikz LaTeX

如何在下面的网格之外添加数字。我尝试了以下代码,但它没有居中,我无法输入数字。

  \documentclass{article}
    \usepackage{geometry}
    \geometry{hmargin=1cm,vmargin=1cm}
    \usepackage{tikz}
    \usetikzlibrary {shapes.geometric, arrows, arrows.meta}
    \def\width{20}
    \def\hauteur{25}
    \begin{document}
    \begin{tikzpicture}[x=1cm, y=1cm]
    \draw[step=1mm, line width=0.1mm, black!5!white] (0,0) grid (\width,\hauteur);
    \draw[step=5mm, line width=0.2mm, black!10!white] (0,0) grid (\width,\hauteur);
    \draw[step=5cm, line width=0.5mm, black!10!black] (0,0) grid (\width,\hauteur);
    \draw[step=1cm, line width=0.3mm, black!15!white] (0,0) grid (\width,\hauteur);
    \end{tikzpicture}
    \end{document}

您可以尝试结合使用 \foreach 循环和 \node 功能。我在下面添加了源代码:

\documentclass{article}
\usepackage{geometry}
\geometry{hmargin=1cm,vmargin=1cm}
\usepackage{tikz}
\usetikzlibrary {shapes.geometric, arrows, arrows.meta}
    
\def\width{20}
\def\hauteur{25}
    
\begin{document}
    \begin{tikzpicture}[x=1cm, y=1cm]
    \draw[step=1mm, line width=0.1mm, black!5!white] (0,0) grid (\width,\hauteur);
    \draw[step=5mm, line width=0.2mm, black!10!white] (0,0) grid (\width,\hauteur);
    \draw[step=5cm, line width=0.5mm, black!10!black] (0,0) grid (\width,\hauteur);
    \draw[step=1cm, line width=0.3mm, black!15!white] (0,0) grid (\width,\hauteur);
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%% ADD THE PART BELOW %%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \foreach \x in {0, ..., \width} {%
      % Bottom
      \node[anchor=north] at (\x,0) {\x};
      
      % Top
      \node[anchor=south] at (\x,\hauteur) {\x};
    }
    
    \foreach \y in {0, ..., \hauteur} {%
      % Left
      \node[anchor=east] at (0,\y) {\y};
      
      % Right
      \node[anchor=west] at (\width,\y) {\y};
    }
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    \end{tikzpicture}
\end{document}

这将产生以下输出:

当然,如果你愿意,你仍然可以稍微调整一下间距。这就是您想要的结果吗?