当我尝试构建一个包含两列以上的表格时,出现错误

当我尝试构建一个包含两列以上的表格时,出现错误

我是 Latex 新手,正在使用一个名为的 Python 包stargazer来生成 Latex 代码。每当我尝试制作包含 2 列以上的输出时,都会出现错误。

! Extra alignment tab has been changed to \cr.
<template> \endtemplate 

l.10 ...column{3}{c}{\textit{Dependent variable:}}'

我知道这个问题的措辞并不完美。如果能得到任何帮助,我都会非常感激,但又不想因为发帖格式不好而被人大声呵斥。

为什么这样做

\documentclass{article}

\begin{document}

\begin{table}[!htbp] \centering
  \label{}
\begin{tabular}{@{\extracolsep{5pt}}lcc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{2}{c}{\textit{Dependent variable:}} \
\cr \cline{2-3}
\\[-1.8ex] & (1) & (2) \\
\hline \\[-1.8ex]
 Best Random & 0.392$^{***}$ & \\
  & (0.041) & \\
  & & \\
 Worst Random & & 0.518$^{***}$ \\
  & & (0.046) \\
  & & \\
 const & 0.002$^{}$ & 0.004$^{***}$ \\
  & (0.001) & (0.001) \\
  & & \\
\hline \\[-1.8ex]
 Observations & 156.0 & 156.0 \\
 R${2}$ & 0.375 & 0.453 \\
 Adjusted R${2}$ & 0.371 & 0.45 \\
 Residual Std. Error & 0.014(df = 154.0) & 0.013(df = 154.0)  \\
 F Statistic & 92.575$^{***}$ (df = 1.0; 154.0) & 127.778$^{***}$ (df = 1.0; 154.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
\end {document}

但这不行:

\begin{table}[!htbp] \centering
  \label{}
\begin{tabular}{@{\extracolsep{5pt}}lcc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{3}{c}{\textit{Dependent variable:}} \
\cr \cline{3-4}
\\[-1.8ex] & (1) & (2) & (3) \\
\hline \\[-1.8ex]
 Best Random & 0.392$^{***}$ & & \\
  & (0.041) & & \\
  & & & \\
 Mean Randoms & & & 0.856$^{***}$ \\
  & & & (0.043) \\
  & & & \\
 Worst Random & & 0.518$^{***}$ & \\
  & & (0.046) & \\
  & & & \\
 const & 0.002$^{}$ & 0.004$^{***}$ & 0.001$^{}$ \\
  & (0.001) & (0.001) & (0.001) \\
  & & & \\
\hline \\[-1.8ex]
 Observations & 156.0 & 156.0 & 156.0 \\
 R${2}$ & 0.375 & 0.453 & 0.717 \\
 Adjusted R${2}$ & 0.371 & 0.45 & 0.716 \\
 Residual Std. Error & 0.014(df = 154.0) & 0.013(df = 154.0) & 0.009(df = 154.0)  \\
 F Statistic & 92.575$^{***}$ (df = 1.0; 154.0) & 127.778$^{***}$ (df = 1.0; 154.0) & 391.008$^{***}$ (df = 1.0; 154.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}

答案1

siunitx您可以使用和来获得更好的表格booktabs

数字列用 声明S(请参阅 手册siunitx)。不包含要在小数点处对齐的数字的单元格应将其内容括起来。

我添加了几个特别指定表格的命令;为了保持宽度较短,“df”部分设置在数字下方。

\documentclass{article}
\usepackage{siunitx,booktabs}

\begin{document}

\begin{table}[!htbp]
\centering
\sisetup{
  input-open-uncertainty={},
  input-close-uncertainty={},
}
\newcommand{\tn}[1]{\makebox[0pt][l]{$^{#1}$}}
\newcommand{\splitcell}[1]{\begin{tabular}[t]{@{}c@{}}#1\end{tabular}}

\caption{Some caption}\label{a-label}

\begin{tabular}{
  @{}
  l
  S[table-format=1.3,table-space-text-pre={(},table-space-text-post={)}]
  S[table-format=1.3,table-space-text-pre={(},table-space-text-post={)}]
  S[table-format=1.3,table-space-text-pre={(},table-space-text-post={)}]
  @{}
}
\toprule
& \multicolumn{3}{c}{\textit{Dependent variable:}} \\
\cmidrule(l){2-4}
& {(1)} & {(2)} & {(3)} \\
\midrule
Best Random  &  0.392\tn{***} &                & \\
             & (0.041)        &                & \\
\addlinespace
Mean Randoms &                &                &  0.856\tn{***} \\
             &                &                & (0.043) \\
\addlinespace
Worst Random &                &  0.518\tn{***} & \\
             &                & (0.046)        & \\
\addlinespace
const        &  0.002         &  0.004\tn{***} &  0.001 \\
             & (0.001)        & (0.001)        & (0.001) \\
\midrule
Observations        & {156}   & {156}   & {156} \\
\addlinespace
R$_{2}$             & {0.375} & {0.453} & {0.717} \\
\addlinespace
Adjusted R$_{2}$    & {0.371} & {0.45}  & {0.716} \\
\addlinespace
Residual Std. Error & {\splitcell{0.014 \\ (df = 154.0)}}
                    & {\splitcell{0.013 \\ (df = 154.0)}}
                    & {\splitcell{0.009 \\ (df = 154.0)}}  \\
\addlinespace
F Statistic & {\splitcell{92.575\tn{***} \\ (df = 1.0; 154.0)}}
            & {\splitcell{127.778\tn{***} \\ (df = 1.0; 154.0)}}
            & {\splitcell{391.008\tn{***} \\ (df = 1.0; 154.0)}} \\
\midrule[\heavyrulewidth]
\multicolumn{4}{@{}l@{}}{\textit{Note:} $^{*}$~$p<0.1$; $^{**}$~$p<0.05$; $^{***}$~$p<0.01$}
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

答案2

第二个表在定义中需要一个额外的列--已添加

斜坡从 3 列延伸到 4 列——从 2 列改为 4 列

但你的桌子太宽了

您需要更改最后两行,以使其占用更少的空间

残差标准误差 0.014 新行 df=154

在此处输入图片描述

\documentclass{article}

\begin{document}

\begin{table}[!htbp] \centering
  \label{}
\begin{tabular}{@{\extracolsep{5pt}}lcc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{2}{c}{\textit{Dependent variable:}} \
\cr \cline{2-3}
\\[-1.8ex] & (1) & (2) \\
\hline \\[-1.8ex]
 Best Random & 0.392$^{***}$ & \\
  & (0.041) & \\
  & & \\
 Worst Random & & 0.518$^{***}$ \\
  & & (0.046) \\
  & & \\
 const & 0.002$^{}$ & 0.004$^{***}$ \\
  & (0.001) & (0.001) \\
  & & \\
\hline \\[-1.8ex]
 Observations & 156.0 & 156.0 \\
 R${2}$ & 0.375 & 0.453 \\
 Adjusted R${2}$ & 0.371 & 0.45 \\
 Residual Std. Error & 0.014(df = 154.0) & 0.013(df = 154.0)  \\
 F Statistic & 92.575$^{***}$ (df = 1.0; 154.0) & 127.778$^{***}$ (df = 1.0; 154.0) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{table}
\begin{table}[!htbp] \centering
    \label{}
    \begin{tabular}{@{\extracolsep{5pt}}lccl}%<---------added l column
        \\[-1.8ex]\hline
        \hline \\[-1.8ex]
        & \multicolumn{3}{c}{\textit{Dependent variable:}} \
        \cr \cline{2-4}%<---------------------------------added cline 2-4
        \\[-1.8ex] & (1) & (2) & (3) \\
        \hline \\[-1.8ex]
        Best Random & 0.392$^{***}$ & & \\
        & (0.041) & & \\
        & & & \\
        Mean Randoms & & & 0.856$^{***}$ \\
        & & & (0.043) \\
        & & & \\
        Worst Random & & 0.518$^{***}$ & \\
        & & (0.046) & \\
        & & & \\
        const & 0.002$^{}$ & 0.004$^{***}$ & 0.001$^{}$ \\
        & (0.001) & (0.001) & (0.001) \\
        & & & \\
        \hline \\[-1.8ex]
        Observations & 156.0 & 156.0 & 156.0 \\
        R${2}$ & 0.375 & 0.453 & 0.717 \\
        Adjusted R${2}$ & 0.371 & 0.45 & 0.716 \\
        Residual Std. Error & 0.014(df = 154.0) & 0.013(df = 154.0) & 0.009(df = 154.0)  \\
        F Statistic & 92.575$^{***}$ (df = 1.0; 154.0) & 127.778$^{***}$ (df = 1.0; 154.0) & 391.008$^{***}$ (df = 1.0; 154.0) \\
        \hline
        \hline \\[-1.8ex]
        \textit{Note:} & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
    \end{tabular}
\end{table}
\end {document}

编辑第二张桌子的装修--更改最后两行

\begin{table}[!htbp] \centering
    \label{}
    \begin{tabular}{@{\extracolsep{5pt}}lccl}
        \\[-1.8ex]\hline
        \hline \\[-1.8ex]
        & \multicolumn{3}{c}{\textit{Dependent variable:}} \
        \cr \cline{2-4}
        \\[-1.8ex] & (1) & (2) & (3) \\
        \hline \\[-1.8ex]
        Best Random & 0.392$^{***}$ & & \\
        & (0.041) & & \\
        & & & \\
        Mean Randoms & & & 0.856$^{***}$ \\
        & & & (0.043) \\
        & & & \\
        Worst Random & & 0.518$^{***}$ & \\
        & & (0.046) & \\
        & & & \\
        const & 0.002$^{}$ & 0.004$^{***}$ & 0.001$^{}$ \\
        & (0.001) & (0.001) & (0.001) \\
        & & & \\
        \hline \\[-1.8ex]
        Observations & 156.0 & 156.0 & 156.0 \\
        R${2}$ & 0.375 & 0.453 & 0.717 \\
        Adjusted R${2}$ & 0.371 & 0.45 & 0.716 \\
        Residual Std. Error & 0.014 & 0.013 & 0.009  \\
        &(df = 154.0)&(df = 154.0)&(df = 154.0)\\
        F Statistic & 92.575$^{***}$ & 127.778$^{***}$ & 391.008$^{***}$\\
        & (df = 1.0; 154.0)& (df = 1.0; 154.0)& (df = 1.0; 154.0)\\
        \hline
        \hline \\[-1.8ex]
        \textit{Note:} & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
    \end{tabular}
\end{table}

在此处输入图片描述

再进行一次编辑,将星号与数字对齐

\begin{table}[!htbp] \centering
    \label{}
    \begin{tabular}{@{\extracolsep{5pt}}lccl}
        \\[-1.8ex]\hline
        \hline \\[-1.8ex]
        & \multicolumn{3}{c}{\textit{Dependent variable:}} \
        \cr \cline{2-4}
        \\[-1.8ex] & (1) & (2) & (3) \\
        \hline \\[-1.8ex]
        Best Random & 0.392$^{***}$ & & \\
        & (0.041)\phantom{$^{***}$} & & \\
        & & & \\
        Mean Randoms & & & 0.856$^{***}$ \\
        & & & (0.043)\phantom{$^{***}$} \\
        & & & \\
        Worst Random & & 0.518$^{***}$ & \\
        & & (0.046)\phantom{$^{***}$} & \\
        & & & \\
        const & 0.002$^{}$ & 0.004$^{***}$ & 0.001$^{}$ \\
        & (0.001) & (0.001)\phantom{$^{***}$} & (0.001) \\
        & & & \\
        \hline \\[-1.8ex]
        Observations & 156.0 & 156.0 & 156.0 \\
        R${2}$ & 0.375 & 0.453 & 0.717 \\
        Adjusted R${2}$ & 0.371 & 0.45 & 0.716 \\
        Residual Std. Error & 0.014 & 0.013 & 0.009  \\
        &(df = 154.0)&(df = 154.0)&(df = 154.0)\\
        F Statistic & 92.575$^{***}$ & 127.778$^{***}$ & 391.008$^{***}$\\
        & (df = 1.0; 154.0)& (df = 1.0; 154.0)& (df = 1.0; 154.0)\\
        \hline
        \hline \\[-1.8ex]
        \textit{Note:} & \multicolumn{3}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
    \end{tabular}
\end{table}

在此处输入图片描述

编辑

稍微打扮一下永远不会有坏处

\begin{table}[!htbp] \centering
    \label{}
    \begin{tabular}{lccc}
        \\[-1.8ex]\hline
        \hline \\[-1.8ex]
        & \multicolumn{3}{c}{\textit{Dependent variable:}} \
        \cr \cline{2-4}
        \\[-1.8ex] & (1) & (2) & (3) \\
        \hline \\[-1.8ex]
        Best Random & 0.392$^{***}$ & & \\
        & (0.041)\phantom{$^{***}$} & & \\
        & & & \\
        Mean Randoms & & & 0.856$^{***}$ \\
        & & & (0.043)\phantom{$^{***}$} \\
        & & & \\
        Worst Random & & 0.518$^{***}$ & \\
        & & (0.046)\phantom{$^{***}$} & \\
        & & & \\
        const & 0.002$^{}$ & 0.004$^{***}$ & 0.001$^{}$ \\
        & (0.001) & (0.001)\phantom{$^{***}$} & (0.001) \\
        & & & \\
        \hline \\[-1.8ex]
        Observations & 156.0 & 156.0 & 156.0 \\
        R${2}$ & 0.375 & 0.453 & 0.717 \\
        Adjusted R${2}$ & 0.371 & 0.45 & 0.716 \\ \rowcolor{black!20}
        Residual Std. Error & 0.014 & 0.013 & 0.009  \\ \rowcolor{black!20}
        &(df = 154.0)&(df = 154.0)&(df = 154.0)\\          \rowcolor{green!20}
        F Statistic & 92.575$^{***}$ & 127.778$^{***}$ & 391.008$^{***}$\\ \rowcolor{green!20}
        & (df = 1.0; 154.0)& (df = 1.0; 154.0)& (df = 1.0; 154.0)\\
        \hline
        \hline \\[-1.8ex]
        \textit{Note:} & \multicolumn{3}{l}{{\color{black!90}$^{*}$p$<$0.1};{\color{red!80} $^{**}$p$<$0.05};{\color{blue!80!red} $^{***}$p$<$0.01}} \\
    \end{tabular}

在此处输入图片描述

相关内容