Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

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

share|improve this answer
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. –  tohecz 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.

share|improve this answer
    
Still, Stephan Lehmke show it is possible, becuase his solution works... ;-) –  tohecz Apr 4 '12 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. –  David Carlisle Apr 4 '12 at 11:01
    
@DavidCarlisle Then this is not a solution but a comment ... :-/ –  tohecz Apr 4 '12 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.