I want to write some text under the rectangles drawn using tikz, to draw an array and its indices.
Here is a mwe:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{arrows, chains, fit, quotes}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (1,1) node[pos=.5] {$\#0$}; %node[anchor=north, pos=.5]{0};
\draw (1,0) rectangle (2,1) node[pos=.5] {$\#0$};
\draw (2,0) rectangle (3,1) node[pos=.5] {$\#1$};
\draw (3,0) rectangle (4,1) node[pos=.5] {$\#2$};
\draw (4,0) rectangle (5,1) node[pos=.5] {$\#1$};
\foreach \x in {0,1,2,3,4}
\draw (\x cm,0pt) -- (\x cm,0pt) node[anchor=north, pos=.5] {$\x$};
\end{tikzpicture}
\end{document}
Here is the result
What I'd like to achieve is each number (those without #) centered under each box.
I also tried to use another node as in the comment, but can't manage to position it under the box and centered.
Best Answer
exploiting
chains
library and uses labels for text below nodes: