6

I have 160 tex files named "regj.tex" for j=1,...,160. Each file contains a table. These tex files do not contain the preamble/begin{document}/end{document}. For example reg1.tex is

\begin{table}[htbp]\centering
\caption{Reg1}
\begin{tabular}{l*{9}{c}}
\hline\hline
            &\multicolumn{1}{c}{(1.1)}&\multicolumn{1}{c}{(1.2)}&\multicolumn{1}{c}{(1.3)}&\multicolumn{1}{c}{(2.1)}&\multicolumn{1}{c}{(2.2)}&\multicolumn{1}{c}{(2.3)}&\multicolumn{1}{c}{(3.1)}&\multicolumn{1}{c}{(3.2)}&\multicolumn{1}{c}{(3.3)}\\
\hline
A&      0.0599&       0.172&       0.110&       0.200&       0.574&       0.368&       640.2&      1836.0&      1177.8\\
            &         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)\\
[1em]
B&      -3.181&      -2.978&      -2.967&      -0.875&      -0.197&      -0.160&     -2929.9&      -762.1&      -641.9\\
            &         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)\\
[1em]
C&      -13.09&      -15.68&      -15.28&       15.56&       6.904&       8.245&     44234.8&     16538.5&     20827.6\\
            &         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)\\
[1em]
D&   -259451.5&   -242691.5&   -241750.9&    -70803.4&    -14782.8&    -11638.9&-238755093.5& -59586716.2& -49531616.2\\
            &         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)\\
[1em]
E&     69993.2&     65360.3&     65152.5&     20567.4&      5081.7&      4387.2&  70028268.8&  20501094.0&  18279814.0\\
            &         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)&         (.)\\
\hline
Number of observations:&          30&          30&          30&          30&          30&          30&          30&          30&          30\\
\hline\hline
\multicolumn{10}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\end{tabular}
\end{table}

I would like to create an executable tex file reg0.tex calling all the tables with a loop. How can I do it?

1
  • Just use the pgffor package, and its \foreach feature. Note also the build in ... Effect
    – daleif
    Oct 24, 2015 at 19:24

2 Answers 2

13

There are numerous ways to do this, but the simplest given what you've described is to use the pgffor package which provides a simple syntax for such loops:

\documentclass{article}
\usepackage{pgffor}
% next code just generates some files to mimic yours
\begin{filecontents}{test-table-1.tex}
\begin{table}[htpb]
\begin{tabular}{ccc}
A & B & C\\
A & B & C
\end{tabular}
\end{table}
\end{filecontents}
\begin{filecontents}{test-table-2.tex}
\begin{table}[htpb]
\begin{tabular}{ccc}
D & E & F\\
D & E & F
\end{tabular}
\end{table}
\end{filecontents}
\begin{filecontents}{test-table-3.tex}
\begin{table}[htpb]
\begin{tabular}{ccc}
G & H & I\\
G & E & F
\end{tabular}
\end{table}
\end{filecontents}
%end of files
\begin{document}
\foreach \x in {1,...,3}{
\input{test-table-\x}
}
\end{document}

output of code

6

Here's an approach based on the LaTeX 3 syntax made available by expl3. I modified the table a bit to get it to fit. Otherwise, I just duplicated your example to 3 different files with 3 different captions.

Obviously, geometry is optional. I just used it to make things fit for demonstration purposes.

The preamble defines a new command with the following syntax

\inputloop[<optional prefix>]{<number of files>}

If no optional prefix is given, reg is used as the default.

\documentclass[a4paper]{article}
\usepackage{expl3,xparse,geometry}
\geometry{scale=.9}
\ExplSyntaxOn
\NewDocumentCommand\inputloop { O { reg } m }
 {
  \int_step_inline:nnnn { 1 } { 1 } { #2 }
   {
    \input { #1 ##1 }
   }
 }
\ExplSyntaxOff
\begin{document}
  \small
  \inputloop{3}
\end{document}

looping input

9
  • I took the liberty of streamlining a bit your code. ;-)
    – egreg
    Oct 24, 2015 at 21:52
  • @egreg I was about to do it! I just have to understand it before I use it....
    – cfr
    Oct 24, 2015 at 21:54
  • Slicker, isn't it? :-)
    – egreg
    Oct 24, 2015 at 21:56
  • @egreg Yes, of course. Thank you :). How do I know when I need a \begin_group: ... \end_group:. Is it just when I'd need a group? I mean, I saw lots of examples using it where I didn't think people would have used groups - at least, it wasn't obvious why - so I started to think there was something different here.
    – cfr
    Oct 24, 2015 at 21:58
  • In the old code there was no need for groups. There would be if you nested calls using the same variable. It's no different from standard TeX programming.
    – egreg
    Oct 24, 2015 at 22:00

You must log in to answer this question.

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