Missing number while using loops

That's something I picked up from David Carlisle some time ago. (I created files a_<i>.tex and t_<i>.tex with contents a<i> and t<i> for i=1,2,3, respectively.) \myline is recursively defined, i.e. it "calls" itself until the counter hits the critical value (3 in this example).

\documentclass{article}
\newcounter{ii}
\setcounter{ii}{0}
\def\singleline{\input{a_\number\value{ii}.tex} &
\input{t_\number\value{ii}.tex}\\}
\def\myline{\stepcounter{ii}%
\ifnum\value{ii}<4
\singleline
\myline
\fi}

\begin{document} 
\begin{tabular}{ll}
 \myline
\end{tabular}
\end{document} 

enter image description here


A simple loop in expl3. In the final argument to \makeloop you specify the loop template, with #1 standing for the current loop value.

\begin{filecontents*}{\jobname-a1.tex}
aaa1
\end{filecontents*}
\begin{filecontents*}{\jobname-b1.tex}
bbb1
\end{filecontents*}
\begin{filecontents*}{\jobname-a2.tex}
aaa2
\end{filecontents*}
\begin{filecontents*}{\jobname-b2.tex}
bbb2
\end{filecontents*}
\begin{filecontents*}{\jobname-a3.tex}
aaa3
\end{filecontents*}
\begin{filecontents*}{\jobname-b3.tex}
bbb3
\end{filecontents*}
\begin{filecontents*}{\jobname-a4.tex}
aaa4
\end{filecontents*}
\begin{filecontents*}{\jobname-b4.tex}
bbb4
\end{filecontents*}

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\makeloop}{O{1}mm}
 {
  \cs_gset:Nn \__rashid_loop_temp:n { #3 }
  \int_step_function:nnN { #1 } { #2 } \__rashid_loop_temp:n
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{ll}
\makeloop{4}{\input{\jobname-a#1}\unskip & \input{\jobname-b#1}\unskip \\}
\end{tabular}

\bigskip

\begin{tabular}{ll}
aaa1 & bbb1 \\
aaa2 & bbb2 \\
aaa3 & bbb3 \\
aaa4 & bbb4 \\
\end{tabular}

\end{document}

I used \jobname just not to clobber my files.

Note \unskip to remove the space inserted by \input (the second table is for a check).

The \makeloop command has an optional argument for specifying a different starting point: \makeloop[4]{7}{...} would make a loop with values 4,5,6,7.

enter image description here