3

I have always believed that \input was strictly equivalent to copy-pasting some code in a master latex file. But I currently experience this strange behaviour. Here is a minimal example.

My main file is:

\documentclass{article}
\begin{document}
\begin{tabular}{lcc} 
\input{table_file}
\hline
\end{tabular}
\end{document}

and the inputted file is table_file.tex:

$T$ & 2.3 & 1.3 \\
average &  9.1 & 0.393 \\

Compiling the main tex file throws a string of errors. Here is the relevant piece of the log file:

(./table_file.tex)
! Misplaced \noalign.
\hline ->\noalign 
                  {\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
l.5 \hline

! You can't use \hrule' here except with leaders. \hline ->\noalign {\ifnum 0=}\fi \hrule @height \arrayrulewidth \futurelet... l.5 \hline

! Missing number, treated as zero. <to be read again> \futurelet l.5 \hline

! Illegal unit of measure (pt inserted). <to be read again> \futurelet l.5 \hline

I'm not sure what is going on here... If I try to copy-paste the contents of table_file.tex into the main file:

\documentclass{article}
\begin{document}
\begin{tabular}{lcc} 
$T$ & 2.3 & 1.3 \\
average &  9.1 & 0.393 \\
\hline
\end{tabular}
\end{document}

it now works flawlessly (no error in the log).

Last (important) thing: if I compile the main file without the \hline, there is no issue.

Any idea what is going on and how to remedy the issue? I'm relying heavily on inputting table rows into tabular environment so I'm eager to understand what is creating this!

Roland
  • 133

1 Answers1

6

You can use the “primitive \input”:

\begin{filecontents*}{\jobname-table.tex}
$T$ & 2.3 & 1.3 \\
average &  9.1 & 0.393 \\
\end{filecontents*}

\documentclass{article}

\makeatletter \newcommand\tableinput[1]{@@input{#1}} \makeatother

\begin{document}

\begin{tabular}{lcc} \tableinput{\jobname-table} \hline \end{tabular}

\end{document}

enter image description here

You need a fairly recent TeX distribution for this. Otherwise

\makeatletter
\newcommand\tableinput[1]{\@@input #1 }
\makeatother

should work as well.

egreg
  • 1,121,712