1

I want to be able to process data using a python script and write it to an external file which can be imported into a latex table environment.

The data is formatted exactly as it would be in the main .tex file (separated by &, with \\ at the end of each row), so i was hoping I would be able to just use the \input command to input it directly into the latex source when it compiles, but it comes up with a misplaced \noalign error and doesn't display the bottom rule of the table.

Below is a minimal worked example where \input is used, which creates the error - along with a counter-example, where the same data is written directly into the latex source which works fine:

\documentclass{article}
\usepackage{booktabs}

\begin{filecontents}{test.tab} 1 & 2 & 3 & 4\ 5 & 6 & 7 & 8\ \end{filecontents}

\begin{document} \begin{table}[t] \centering \caption{Test table (input)} \begin{tabular}{lrrr} \toprule test 1 & test 2 & test 3 & test 4\ \midrule % \input{test.tab} % \bottomrule \end{tabular} \end{table}

\begin{table}[h!] \centering \caption{Test table (no input)} \begin{tabular}{lrrr} \toprule test 1 & test 2 & test 3 & test 4\ \midrule % 1 & 2 & 3 & 4\ 5 & 6 & 7 & 8\ % \bottomrule \end{tabular} \end{table} \end{document}

this produces:

enter image description here

Curiously enough, if I put the entire tabular environment into the file, and then use \input{test.tab} within the table environment tags, it works fine:

\begin{filecontents}{test.tab}
\begin{tabular}{lrrr} \toprule
test 1 & test 2 & test 3 & test 4\\ \midrule
%
1 & 2 & 3 & 4\\
5 & 6 & 7 & 8\\
%
\bottomrule
\end{tabular}
\end{filecontents}

but I would rather not do that, as I want to be able to edit the table itself easily, and don't want to have to re-generate all the data (or edit all of the files manually) every time I make a change.

Is there a simple way of inputting data directly into a table from an external file using the \input command (or something similar) like this? or do I have to use something like pgfplotstables to parse my data first?

1 Answers1

3

You need an expanded input there:

\documentclass{article}
\usepackage{booktabs}

\begin{filecontents}{test.tab} 1 & 2 & 3 & 4\ 5 & 6 & 7 & 8\ \end{filecontents}

\makeatletter \def\expinput#1{@@input#1 } \makeatother

\begin{document} \begin{table}[t] \centering \caption{Test table (input)} \begin{tabular}{lrrr} \toprule test 1 & test 2 & test 3 & test 4\ \midrule % \expinput{test.tab} % \bottomrule \end{tabular} \end{table}

\begin{table}[h!] \centering \caption{Test table (no input)} \begin{tabular}{lrrr} \toprule test 1 & test 2 & test 3 & test 4\ \midrule % 1 & 2 & 3 & 4\ 5 & 6 & 7 & 8\ % \bottomrule \end{tabular} \end{table} \end{document}

enter image description here

koleygr
  • 20,105