可重现的示例

可重现的示例

(这个问题源于一。)

我在主 tex 文件中生成了两个代码片段\input{}。这两个片段看起来非常相似,但它只对其中一个片段有效,我不明白为什么。

可重现的示例

主 tex 文件:

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

片段 1(适用于此):

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

作品并产生:

在此处输入图片描述

片段 2(无效):

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

我收到以下错误信息:

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}]

答案1

\midrule(如\hline\multicolumn)必须是单元格中第一个不可扩展的命令,任何甚至在其之前\relax{}之前的命令都会导致它给出该错误。LaTeX\input不可扩展,因此不能在这里使用。您可以使用

\myinput test.tex

其中\myinput定义为

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

相关内容