3

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!

4
  • 1
    \include always makes a new page and is designed for including things like whole chapters. To input a fragment such as a table you want \input. You can input two tabular side by side using \input into the same table You do not need minipage – David Carlisle Dec 1 '13 at 19:27
  • @DavidCarlisle Alright, but xtable in R (for making Latex tables out of R code) seems to produce only whole table environments OR just the rows without anything else. That's bad for making tabulars, because I'd have to edit the files manually. Is it possible to use input somehow in the minipage environment? (also edited the Question. I meant input, my bad for calling it include) – TomM Dec 1 '13 at 19:32
  • you don't have to edit the files, TeX is a macro expansion language, and macros can be redefined.... (I may post an answer in a bit) you can use \input more or less anywhere, but minipage won't obviously help (as you can not put a table environment in a minipage) – David Carlisle Dec 1 '13 at 19:33
  • 1
    It would have helped if you had provided a complete working document showing all definitions (eg Y is not a standard column type) But we can use the example @Jubobs has provided now. – David Carlisle Dec 1 '13 at 19:36
6

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}%
    }%
}
4
  • @DavidCarlisle Thanks about the missing % :) – jub0bs Dec 1 '13 at 19:53
  • Nice, exactly what I was looking for. Saves me a lot of time, thanks! – TomM Dec 1 '13 at 20:43
  • @Jubobs little follow-up question: What do I have to change in the macro if my tables are slightly more complicated, sthg like: \begin{table}[htbp] \centering \caption[short cap]{long cap} \label{} {\normalsize \begin{tabular}{lYY} \end{tabular} } \end{table} – TomM Dec 1 '13 at 20:58
  • 1
    @TomM See my edit. – jub0bs Dec 1 '13 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.