18

I'm new to LaTeX. I have a table in the LaTeX format that I want to include in my document. Since it is a large table, I would like to save it in a different file and then just load it in my main document.

How can I do this?

3
  • 7
    if your table is already formated in LaTeX, just save it in any .tex file and use \input{yourtable} in main tex. If your table only contains raw data use pgfplotstable (part of pgfplots) package to include and process it from the external file. If you provide an example, you will get more accurate solutions.
    – Ignasi
    Dec 20, 2013 at 16:32
  • 1
    you can find a complete MWE of what @Ignasi describes in my answer to tex.stackexchange.com/questions/150085/…, for example
    – cmhughes
    Dec 21, 2013 at 4:08
  • Or you could include it using e.g. pdfpages if it is in a stable state and you want to avoid re-compiling it all the time. (Or maybe the standalone package can do this better? I've only just discovered its existence, though, so I'm unclear of its capabilities at present.)
    – cfr
    Dec 22, 2013 at 0:28

2 Answers 2

18

Your question doesn't explain how is your table except that it's large and that you would like to save on a different file, but you don't say if it's already formatted like a tabular or only contains raw data.

If your external file (table.tex) is already formatted, something like:

\begin{tabular}{ccc}
\toprule
A & B & C \\
\midrule
a & b & c \\
aa & bb & cc \\
\bottomrule
\end{tabular}

you just have to \input it in your text where you want. The command to be used is \input{name-of-your-file}.

But if your file only contains data with no LaTeX formatting commands, there exist several packages to process and format it in your document. One of these packages is pgfplotstable which is part of pgfplots.

As an example suppose that your data file (table-raw.txt) contains:

A B C 
a b c 
aa bb cc

with command \pgfplotstabletypeset[formatting options]{table-raw.txt} you can process and typeset its contents during your text compilation.

Next code shows and example with both solutions:

\documentclass{article}
\usepackage{booktabs}
\usepackage{pgfplotstable}

\begin{document}

File \texttt{table.tex} contains an already formatted \LaTeX\ tabular. 
To include it in your text just use \verb+\input{table.tex}+: where you want it.

\begin{center}
\input{table.tex}
\end{center}


File \texttt{table-raw.txt} contains some data organized in rows and columns. 
Its \LaTeX\ format will be generated with  \texttt{pgfplotstable} help.

\begin{center}
\pgfplotstabletypeset[%
   every head row/.style={before row=\toprule, 
                             after row=\midrule},
   every last row/.style={after row=\bottomrule},
   col sep=space, 
   header=true, 
   string type]{table-raw.txt}
\end{center}
\end{document}

enter image description here

5
  • My table is being forced to go on a new page, and I have given no such commands in the table.tex file. Any fix?
    – Man
    Apr 14, 2015 at 14:16
  • 1
    @Man, probably your table is forced to go on a new page because there is no enough space for it in present page. A tabular can not be broken between pages. If you need to break it I think you'll need longtable.
    – Ignasi
    Apr 14, 2015 at 15:23
  • \begin{table}\input{tablefile}\end{table} would be better if OP wants a floating table. Apr 27, 2015 at 14:53
  • it's not clear from the answer whether we can refer to \label{name_in_file} from the main document, though I assume it's the case? Feb 4, 2019 at 9:45
  • @MichaelChirico I'm not sure to ubderstand your question, but I hope this helps you. Otherwise, ask a new question. I think yours is not related with this one.
    – Ignasi
    Feb 4, 2019 at 11:21
0

This answer by @Werner on the main StackOverflow website worked very well for me using catchfile package, so I copied it here.

%main.tex
\documentclass{article}
\usepackage{filecontents,catchfile}
\begin{filecontents*}{table.tex}
%table.tex
\hline
a & b \\
\hline
c & d \\
\hline 
\end{filecontents*}

\begin{document}

Table test.

1. Insert a full table

\begin{tabular}{|c|c|}
  \hline
  a & b \\
  \hline
  c & d \\
  \hline
\end{tabular}

2. Input the body of table from a separate file

\CatchFileDef{\mytable}{table.tex}{}% table.tex > \mytable
\begin{tabular}{|c|c|} 
  \mytable
\end{tabular}

\end{document}

You must log in to answer this question.

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