[Tex/LaTex] Append columns in table from two input files

tables

Suppose I have 2 files, in1.tex and in2.tex. In file in1.tex, the data is

1a & 1b \\
1c & 1d

and in in2.tex, the data is in a similar format:

2a & 2b \\
2c & 2d

In my scenario, these are output tables from Stata. To combine these vertically, I could just do

\input{in1.tex} \\
\input{in2.tex}

I'm wondering if there's a way I could append these two files so that they appear side by side… in other words, to get something like

1a & 1b & 2a & 2b \\
1c & 1d & 2c & 2d

EDIT: So that I can put this in a tabular/table environment.

\begin{table}[htbp]
    \begin{tabular}{*{4}{c}}
        ** some function of in1.tex and in2.tex here
    \end{tabular}
\end{table}

Thanks!

Best Answer

You don't need to merge in1.tex and in2.tex horizontally into in3.tex (say) before setting it in a tabular. Here's how you can do it without that merger:

enter image description here

\documentclass{article}

\usepackage{filecontents}

% First standalone table
\begin{filecontents*}{in1.tex}
1a & 1b \\
1c & 1d
\end{filecontents*}

% Second standalone table
\begin{filecontents*}{in2.tex}
2a & 2b \\
2c & 2d
\end{filecontents*}

% Third combined table
\begin{filecontents*}{in3.tex}
1a & 1b & 2a & 2b \\
1c & 1d & 2c & 2d
\end{filecontents*}

\begin{document}

% Combined in1.tex and in2.tex
\begin{tabular}[t]{ *{2}{c} }
  \input{in1}%
\end{tabular}%
\begin{tabular}[t]{ *{2}{c} }
  \input{in2}%
\end{tabular}

\bigskip

% Desired output
\begin{tabular}{ *{4}{c} }
  \input{in3}%
\end{tabular}

\end{document}

Some things to note here:

  1. Either end of the tabular structure has only half the regular \tabcolsep; that is, .5\tabcolsep. As such, a horizontal concatenation is equivalent to a regular \tabcolsep.

  2. \input{..}% ends with a %. See What is the use of percent signs (%) at the end of lines?

  3. End the first tabular using \end{tabular}%. Motivation similar to above.

  4. The two separate tabulars are [t]op aligned. However, in the case where the numbers of rows in in1.tex and in2.tex are equal, there is no real need for this forced alignment.

Related Question