2

(This question grew out of this one.)

I've produced two snippets of code I want to \input{} in my main tex file. The two snippets look very similar, but it works only with one the snippets and I don't get why.

Reproducible example

Main tex file:

\documentclass[11pt, a4paper]{article}

\usepackage{booktabs}
\usepackage[flushleft]{threeparttable}

\begin{document}

\begin{table}[!t]
\begin{small}
\begin{threeparttable}
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{lllll}
\toprule
& \multicolumn{4}{c}{\textit{Dep. var: } price in dollars} \\
\cline{2-5} 
 & (1) & (2) & (3) & (4)\\
 \midrule
\input{test.tex}
\bottomrule
\end{tabular}
}
\end{threeparttable}
\begin{tablenotes}
\item\textit{Note:} Here I'm putting many great comments about my regression setup. *** p$<$0.01, ** p$<$0.05, * p$<$0.1.
\end{tablenotes}
\end{small}
\end{table}

\end{document}

Snippet 1 (works for this):

            &\multicolumn{1}{c}{price}&\multicolumn{1}{c}{price}&\multicolumn{1}{c}{price}&\multicolumn{1}{c}{price}\\
\midrule
weight      &        2.04\sym{***}&        1.75\sym{**} &        2.06\sym{**} &        4.36\sym{***}\\
            &      (5.42)         &      (2.72)         &      (3.13)         &      (3.74)         \\
\addlinespace
mpg         &                     &       -49.5         &       -56.2         &       -86.8         \\
            &                     &     (-0.57)         &     (-0.66)         &     (-1.03)         \\
\addlinespace
headroom    &                     &                     &      -675.6         &                     \\
            &                     &                     &     (-1.72)         &                     \\
\addlinespace
length      &                     &                     &                     &      -104.9\sym{*}  \\
            &                     &                     &                     &     (-2.64)         \\
\midrule
Obs.        &          74         &          74         &          74         &          74         \\
R$^2$       &        0.29         &        0.29         &        0.32         &        0.36         \\

this works and produces:

enter image description here

Snippet 2 (doesn't work):

\midrule
weight      &        2.04\sym{***}&        1.75\sym{**} &        2.06\sym{**} &        4.36\sym{***}\\
            &      (5.42)         &      (2.72)         &      (3.13)         &      (3.74)         \\
\addlinespace
mpg         &                     &       -49.5         &       -56.2         &       -86.8         \\
            &                     &     (-0.57)         &     (-0.66)         &     (-1.03)         \\
\addlinespace
headroom    &                     &                     &      -675.6         &                     \\
            &                     &                     &     (-1.72)         &                     \\
\addlinespace
length      &                     &                     &                     &      -104.9\sym{*}  \\
            &                     &                     &                     &     (-2.64)         \\
\midrule
Obs.        &          74         &          74         &          74         &          74         \\
R$^2$       &        0.29         &        0.29         &        0.32         &        0.36         \\

And I get the following error message:

D:\test\test.tex:1: Misplaced \noalign. [\midrule]
D:\test\test.tex:2: Missing } inserted. [w]
D:\test\test.tex:2: Missing } inserted. [w]
D:\test\test.tex:2: Missing } inserted. [w]
D:\test\test.tex:2: Missing \cr inserted. [w]
D:\test\test.tex:2: Missing { inserted. [w]
D:\test\test.tex:2: Misplaced alignment tab character &. [weight      &]
D:\test\test.tex:2: Misplaced alignment tab character &. [weight      &        2.04\sym{***}&]
D:\test\test.tex:2: Misplaced alignment tab character &. [...       2.04\sym{***}&        1.75\sym{**} &]
D:\test\test.tex:2: Misplaced alignment tab character &. [...       1.75\sym{**} &        2.06\sym{**} &]
D:\test\test.tex:3: Misplaced \cr. [            &]
D:\test\test.tex:3: Misplaced alignment tab character &. [            &]
D:\test\test.tex:3: Misplaced alignment tab character &. [            &      (5.42)         &]
D:\test\test.tex:3: Misplaced alignment tab character &. [...     (5.42)         &      (2.72)         &]
D:\test\test.tex:3: Misplaced alignment tab character &. [...     (2.72)         &      (3.13)         &]
D:\test\test.tex:4: Misplaced \cr. [\addlinespace]
D:\test\test.tex:4: Misplaced \noalign. [\addlinespace]
D:\test\test.tex:5: Missing $ inserted. [m]
D:\test\test.tex:5: Missing } inserted. [m]
D:\test\test.tex:5: Missing } inserted. [m]
D:\test\test.tex:5: Missing } inserted. [m]
D:\test\test.tex:5: Extra }, or forgotten \endgroup. [m]
D:\test\test.tex:5: Misplaced alignment tab character &. [mpg         &]
D:\test\test.tex:5: Misplaced alignment tab character &. [mpg         &                     &]
D:\test\test.tex:5: Misplaced alignment tab character &. [...                    &       -49.5         &]
D:\test\test.tex:5: Misplaced alignment tab character &. [...      -49.5         &       -56.2         &]
D:\test\test.tex:6: Misplaced alignment tab character &. [            &]
D:\test\test.tex:6: Misplaced alignment tab character &. [            &                     &]
D:\test\test.tex:6: Misplaced alignment tab character &. [...                    &     (-0.57)         &]
D:\test\test.tex:6: Misplaced alignment tab character &. [...    (-0.57)         &     (-0.66)         &]
D:\test\test.tex:7: Misplaced \noalign. [\addlinespace]
D:\test\test.tex:8: Misplaced alignment tab character &. [headroom    &]
D:\test\test.tex:8: Misplaced alignment tab character &. [headroom    &                     &]
D:\test\test.tex:8: Misplaced alignment tab character &. [...                    &                     &]
D:\test\test.tex:8: Misplaced alignment tab character &. [...                    &      -675.6         &]
D:\test\test.tex:9: Misplaced alignment tab character &. [            &]
D:\test\test.tex:9: Misplaced alignment tab character &. [            &                     &]
D:\test\test.tex:9: Misplaced alignment tab character &. [...                    &                     &]
D:\test\test.tex:9: Misplaced alignment tab character &. [...                    &     (-1.72)         &]
D:\test\test.tex:10: Misplaced \noalign. [\addlinespace]
D:\test\test.tex:11: Misplaced alignment tab character &. [length      &]
D:\test\test.tex:11: Misplaced alignment tab character &. [length      &                     &]
D:\test\test.tex:11: Misplaced alignment tab character &. [...                   &                     &]
D:\test\test.tex:11: Misplaced alignment tab character &. [...                   &                     &]
D:\test\test.tex:12: Misplaced alignment tab character &. [            &]
D:\test\test.tex:12: Misplaced alignment tab character &. [            &                     &]
D:\test\test.tex:12: Misplaced alignment tab character &. [...                   &                     &]
D:\test\test.tex:12: Misplaced alignment tab character &. [...                   &                     &]
D:\test\test.tex:13: Misplaced \noalign. [\midrule]
D:\test\test.tex:14: Misplaced alignment tab character &. [Obs.        &]
D:\test\test.tex:14: Misplaced alignment tab character &. [Obs.        &          74         &]
D:\test\test.tex:14: Misplaced alignment tab character &. [...        74         &          74         &]
D:\test\test.tex:14: Misplaced alignment tab character &. [...        74         &          74         &]
D:\test\test.tex:15: Misplaced alignment tab character &. [R$^2$       &]
D:\test\test.tex:15: Misplaced alignment tab character &. [R$^2$       &        0.29         &]
D:\test\test.tex:15: Misplaced alignment tab character &. [...      0.29         &        0.29         &]
D:\test\test.tex:15: Misplaced alignment tab character &. [...      0.29         &        0.32         &]
D:\test\regepr.tex:20: Misplaced \noalign. [\bottomrule]
D:\test\regepr.tex:21: Misplaced \crcr. [\end{tabular}]
D:\test\regepr.tex:21: Extra }, or forgotten \endgroup. [\end{tabular}]
D:\test\regepr.tex:21: Extra }, or forgotten \endgroup. [\end{tabular}]
D:\test\regepr.tex:21: Extra }, or forgotten $. [\end{tabular}]
D:\test\regepr.tex:21: Extra }, or forgotten $. [\end{tabular}]
D:\test\regepr.tex:21: Missing $ inserted. [\end{tabular}]
  • Is the difference only the first line in the first snippet? – Tiuri Aug 10 '17 at 14:58
  • Yes! Sorry I could have made that more clear. But why would the first line of &\multicolumn{1}{c}{price} ... matter? – ulima2_ Aug 10 '17 at 15:14
  • I tried your code and can reproduce the error. It only appears when doing the input, not when putting the snippet directly to the tex file. Something seems to fail when the \input first tires to include a \midrule. And I have no idea why. – Tiuri Aug 10 '17 at 15:16
  • neither the error message nor the posted code has anything to do with \include so it would be good if you change the title to reflect the actual problem (and remove the include tag) – David Carlisle Aug 10 '17 at 15:38
  • Sorry I don't understand: The error was caused by \include failing with \midrule so it seems important to mention it. – ulima2_ Aug 11 '17 at 12:00
4

\midrule(like \hline and \multicolumn) has to be the first non-expandable command in the cell, any command even \relax or {} before it will cause it to give that error. LaTeX \input is not expandable so can not be used here. You could use

\myinput test.tex

where \myinput is defined by

\makeatletter
\let\myinput\@@input
\makeatother

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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