[Tex/LaTex] Produce 2 side-by-side tables from tables as external files (source: xtable in R)

rspacingtables

Is it possible to put two tables next to each other, which are in separate .tex files, and added to the document via \input? If it's not, I could also create just the 'raw' contents of the tables, i.e. just the rows and columns by using the content.only command of xtable.

Assume I got the following table TWO times, saved as two separate tex files:

\begin{table}
\begin{tabular}{lYY}
\toprule 
& \multicolumn{1}{c}{$N_1$} & 
\multicolumn{1}{c}{$N_2$} \\
\midrule 
\multicolumn{1}{l}{$S_1$} &  0.14 & -1.243 \\ 
\multicolumn{1}{l}{$S_2$} &  0.217 &  4.132 \\ 
\multicolumn{1}{l}{$S_3$} &  7.350 & -9.913 \\ 
\multicolumn{1}{l}{$S_4$} & 0.132 & 6.664 \\ 
\bottomrule 
\end{tabular}
\end{table}

Can I put these next to each other by using input? I know you need to utilize the minipage environment somehow, but I don't know if this works in combination with include. I could not make it work in some tests.

If include doesn't work, I might create just the content to put it inside the minipage environment at the right positions. Either way, please help me with the code. Thanks!

Best Answer

The answer is: yes, it is possible.

Under the assumption that your .tex files each consists in some code of the form \begin{table}\begin{tabular}{...} ... \end{tabular}\end{table}, you can

  • locally redefine the table environment to do nothing, and
  • input that code inside a subtable environment; such an environment uses minipage internally, which means you don't have to use minipage explicitly.

Note that, in this specific case, you want to use \input, not \include; see When should I use \input vs. \include? for more details.

See my example below, in which I've defined a macro called \insertmytabular, for more automation.

enter image description here

\documentclass{article}

\usepackage{booktabs}
\usepackage{subcaption}
\usepackage{filecontents}

\newcommand\insertmytabular[3][.45]%
{%
    {% extra group to make the redefinition of table local
        \renewenvironment{table}[1][]{}{}
        \begin{subtable}[b]{#1\textwidth}
            \centering
            \input{#2}
            \caption{#3}
        \end{subtable}%
    }%
}

\begin{filecontents*}{tabularstuff.tex}
\begin{table}
\begin{tabular}{lll}
\toprule 
& \multicolumn{1}{c}{$N_1$} & 
\multicolumn{1}{c}{$N_2$} \\
\midrule 
$S_1$ &  0.14 & -1.243 \\ 
$S_2$ &  0.217 &  4.132 \\ 
$S_3$ &  7.350 & -9.913 \\ 
$S_4$ & 0.132 & 6.664 \\ 
\bottomrule 
\end{tabular}
\end{table}
\end{filecontents*}

\begin{document}
\begin{table}
    \insertmytabular{tabularstuff}{first table}
    \hfill
    \insertmytabular{tabularstuff}{second table}
    \caption{two tables}
\end{table}
\end{document}

Edit (answer to follow-up question): in that case, define \insertmytabular as follows:

\newcommand\insertmytabular[3][.45]%
{%
    {% extra group to make the redefinition of table local
        \renewenvironment{table}[1][]{}{}
        \begin{subtable}[b]{#1\textwidth}
            \input{#2}
        \end{subtable}%
    }%
}