# Cannot use \toprule when doing \input inside tabular — why?

Using \input inside a tabular environment does not seem to work in all cases, but I don't see why.

The MWE below gives the following error message:

! Misplaced \noalign.
\toprule ->\noalign
{\ifnum 0=`}\fi \@aboverulesep =\abovetopsep \global \@b...
l.1 \toprule

File main.tex:

\documentclass{standalone}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{ll}
\input{inp}
\end{tabular}
\end{document}

File inp.tex:

\toprule
a & b \\
c & d \\
\bottomrule

The document compiles properly if the contents of inp.tex are pasted into the tabular environment. What is happening here?

\documentclass{standalone}
\usepackage{booktabs}

\begin{document}
\begin{tabular}{ll}
\toprule
a & b \\
c & d \\
\bottomrule
\end{tabular}
\end{document}

EDIT: The following question is related: Why is \input not expandable?

-

\input is trying to be intelligent, and this seems to be upsetting LaTeX's picky tabular definition.

But you can access the original \input primitive from TeX which is less dangerous at this point.

After defining

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

using \primitiveinput inside the tabular should work.

-
I would just prefer putting \makeatletter\let\primitiveinput\@@input\makeatother in the preamble and using it with \primitiveinput imp\relax. Calling your macro \primitiveinput is confusing: it is not the TeX primitive, but a macro that calls the primitive. –  yo' Apr 4 '12 at 10:58
Of course you can use it in any way you prefer. But I think a more LaTeXy definition is less confusing to users. Additionally, your \relax would again disturb the tranquility of tabular, giving an error for \primitiveinput inp\relax\midrule. So the user would have to strictly adhere to leaving a space behind the file name, something which might not come naturally to LaTeX users... –  Stephan Lehmke Apr 4 '12 at 11:03
Thank you, this seems reasonable. Is this related: tex.stackexchange.com/questions/43006/… ? –  krlmlr Apr 4 '12 at 11:44
Yep, that's exactly the same problem, only wrapped in an extremely long question ;-) –  Stephan Lehmke Apr 4 '12 at 11:47

Some commands e.g. \toprule, \hline or \multicolumn must come directly at the start of a cell. There can't be nothing before, this includes commands like \input.

-
Still, Stephan Lehmke show it is possible, becuase his solution works... ;-) –  yo' Apr 4 '12 at 10:56
@tohecz, Both are right, it just depends when you look. \omit or noalign or \span must be the first thing after expansion (you need to allow expansion as none of the commands mentioned are primitive) the primitive \@@input is expandable so basicaly is not there according to this definition, but LaTeX's \input is not expandable and so leaves a token before the \omit. –  David Carlisle Apr 4 '12 at 11:01
@DavidCarlisle Then this is not a solution but a comment ... :-/ –  yo' Apr 4 '12 at 11:08