[Tex/LaTex] Align text on top of a framebox

boxesframedvertical alignment

I am trying to create a series of boxes so that my students can input there id number for there final exam.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newsavebox{\idbox}
\sbox{\idbox}{\framebox[4.5mm]{\vrule depth 1.25mm width 0pt height 2.5mm}}
\newcommand{\nidbox}[1]{%
\foreach \i in {1,...,#1}
{
\usebox{\idbox}\kern-1.5pt
}}
\begin{document}
\nidbox{5}
\end{document}

What I want is to be able to put ID Number directly aligned on top of the first framebox and to be left justified. I have been trying to use \raisebox like:

\def\idnum#1{\raisebox{0.8\baselineskip}[1ex][2ex]{%
ID NUMBER}\kern-4cm #1}

where its implementation would be like \idnum{\nidbox{5}}.

Sincerely I do not know if it is the best approach but I believe that I am forcing it. I know that I can probably do it using a table or with an array but am trying to find out a different approach. Any insights into the matter will be highly appreciated.

Best Answer

I prefer expl3 to tikz for repeating things:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\idbox}{}
  {
   \framebox[4.5mm]{\vrule depth 1.25mm width 0pt height 2.5mm}
  }
\NewDocumentCommand{\idnum}{m}
  {
   \begin{tabular}{@{}l@{}}
   ID~Number\\
   \prg_replicate:nn { #1 } { \idbox \hspace{3pt} }
   \end{tabular}
  }
\ExplSyntaxOff
\begin{document}
\idnum{5}

\idnum{7}
\end{document}

A tabular environment is just the more convenient way to align objects. A tabular free macro could be

\NewDocumentCommand{\idnum}{ m }
  {
   \vbox:n
     {
      \hbox:n { \strut ID~Number }
      \hbox:n { \strut\prg_replicate:nn { #1 } { \idbox \hspace{3pt} } \unskip }
     }
  }

But I don't see why one should prefer this approach to the simplicity of a tabular.

enter image description here