5

I've been trying to read a number of tex files into a tabular. I have two sets of files numbered t_1.tex ... t_10.tex and a_1.tex ... a_10.tex.

This is the code I have so far:

\newcount\ii
\ii=0
\def\myline{}%
\loop
\ifnum\ii<10
\advance\ii by1
\edef\myline{%
\myline
\input{t_\the\ii.tex} & \input{a_\the\ii.tex}\\%
}%
\repeat

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

I get Missing number, treated as zero. \repeat.

There are a couple of similar posts here and here. But I can't find the issue in my case.

Thanks for your help.

1
  • @Schrödinger'scat Thanks for your comment, could you please give an example for my case?
    – Rash
    Commented Aug 23, 2019 at 3:39

2 Answers 2

4

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

2
  • Thanks, it works perfectly!
    – Rash
    Commented Aug 23, 2019 at 3:51
  • @Rashid You're welcome!
    – user194703
    Commented Aug 23, 2019 at 3:56
2

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

1
  • Thanks for the answer!
    – Rash
    Commented Aug 25, 2019 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .