15

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}

Result of compilation

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

2 Answers 2

23

\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.

5
  • 1
    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, 2012 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... Apr 4, 2012 at 11:03
  • Thank you, this seems reasonable. Is this related: tex.stackexchange.com/questions/43006/… ?
    – krlmlr
    Apr 4, 2012 at 11:44
  • Yep, that's exactly the same problem, only wrapped in an extremely long question ;-) Apr 4, 2012 at 11:47
  • But then why on earth call it a primitive when it obviously isn't? By all means call it something else if that's better. LaTeX user or not, I'd rather you not call a spade a pitch fork.
    – cfr
    Mar 19, 2017 at 3:45
4

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

4
  • Still, Stephan Lehmke show it is possible, becuase his solution works... ;-)
    – yo'
    Apr 4, 2012 at 10:56
  • 2
    @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. Apr 4, 2012 at 11:01
  • @DavidCarlisle Then this is not a solution but a comment ... :-/
    – yo'
    Apr 4, 2012 at 11:08
  • 1
    Thanks! This was the solution for me. I had 1) \begin{tabular}, 2) \small, 3) \toprule. Moving 2 before 1 solved the problem for me. Thanks! Mar 1, 2018 at 1:16

You must log in to answer this question.

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