Centering in TikZ matrix of nodes
align=center
without any text width
is the problem. Comment it out and all text will be centered (with or without space after it) or add a text width
over which the text could be centered.
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [
matrix of nodes,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={
rectangle, draw=black, minimum height=1.25em, minimum width=1.25em,
anchor=center, %align=center, %text width=2em,
inner sep=0pt, outer sep=0pt
}
] {
0 & 3 & 6 & 9 & 12 & 15 & 18 & 21 \\
};
\end{tikzpicture}
\end{document}
I am not sure what other bells and whistles you want in your real application but rather than using a matrix of nodes
for this you might want to consider using a macro with an embedded for-loop:
\documentclass[tikz, border=4mm]{standalone}
\newcommand\myarray[1]{%
\begin{tikzpicture}
\foreach \x [count=\c] in {#1} {
\draw[thick](\c-0.5,0-0.5) rectangle ++ (1,1);
\draw(\c,0)node{\x};
}
\end{tikzpicture}%
}
\begin{document}
\myarray{0, 3, 6, 9, 12, 15, 18, 21}
\end{document}
This produces:
The main benefit of this approach is that it simplifies the code quite a lot, especially if you have a lot of these arrays. Of course, you can fine tune at will with shading, colours, font sizes, box size etc etc.
EDIT
Motivated by the OP's comment, here is a 2D version:
\documentclass[tikz, border=4mm]{standalone}
\newcommand\myarray[1]{%
\begin{tikzpicture}
\foreach \row [count=\r] in {#1} {
\foreach \x [count=\c] in \row {
\draw[thick](\c-0.5,-\r-0.5) rectangle ++ (1,1);
\draw(\c,-\r)node{\x};
}
}
\end{tikzpicture}%
}
\begin{document}
\myarray{{0,3,6,9,12,15,18,21},{2,4,7,11},{4,5,6,7}}
\end{document}
This code produces: