表格行居中导致整个表格断裂

表格行居中导致整个表格断裂

下面是我试图诊断的表格:

\documentclass{article}
\usepackage{array, makecell}

\begin{document}

\begin{table*}
  \begin{tabular}{@{}  >{\centering} m{1.67cm}| >{\centering} m{0.7cm} 
                       >{\centering} m{0.7cm} >{\centering} m{0.7cm} >{\centering} m{0.7cm} 
                       >{\centering} m{0.7cm} >{\centering} m{0.7cm} >{\centering} m{0.7cm}                 
                       >{\centering} m{0.7cm} @{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \newline $8$ \newline $9$ & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table*}
\end{document}

如果我尝试编译它,它会给出几个Extra alignment tab has been changed to \cr错误,整个表格一团糟。但是,如果我>{\centering}从最后一列中删除最后一个,一切看起来都很好。我怎样才能包含居中并使表格正常显示?包含@{}在表格括号中的来自这个问题,但最终问题还是没能得到解决。

还有一个附加问题,如果我将文档类从 更改articlerevtex4-1,则包含换行符的条目的行(第一行)不再垂直居中。有人知道这是怎么回事吗,以及如何使用 revtex 在表格中实现垂直居中?

答案1

以下是一些建议。在表格的标题中,我解释了这些变化:

在此处输入图片描述

\documentclass{article}
\usepackage{array, makecell}

\begin{document}

\begin{table}
\caption{corrected the use of centering by adding arraybackslash, simplified column declaration}
  \begin{tabular}{@{}  >{\centering\arraybackslash} m{1.67cm}| *{8}{>{\centering\arraybackslash} m{0.7cm}}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ \newline $8$ \newline $9$ & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}


\begin{table}
\caption{Used makecell from the makecell package in order to get the linebreaks in column 8}
  \begin{tabular}{@{}  >{\centering\arraybackslash} p{1.67cm}| *{8}{>{\centering\arraybackslash} p{0.7cm}}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \makecell{$7$ \\ $8$ \\ $9$} & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}


\begin{table}
\caption{Since no automatic linebreaks in table cells seem to be needed, why not just go for simple c type columns?}
  \begin{tabular}{@{}  c| *{8}{c}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \makecell{$7$ \\ $8$ \\ $9$} & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}

\begin{table}
\caption{If you prefer wider cells but do not need automatic linebreaks, why not go for the new column type W from the array package?}
  \begin{tabular}{@{}  Wc{1.67cm}| *{8}{Wc{0.7cm}}@{}}
       Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \makecell{$7$ \\ $8$ \\ $9$} & $8$ \\
       \hline
       \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
      \hline
       1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
      \hline
      1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
      \hline
  \end{tabular}
\end{table}
\end{document}

答案2

这是一个使用和嵌套表格在标题中创建多行单元格的变tabular*\extracolsep{fill}三部分表是调整标题宽度以匹配表格宽度的简单方法

在此处输入图片描述

\documentclass{article}
\usepackage{array, threeparttable}

\setlength{\extrarowheight}{2pt}
\renewcommand*{\arraystretch}{1.1}

\begin {document}
\begin{table}
    \begin{threeparttable}
    \centering
    \caption{A caption with lot of interesting information of how to interpret the complicated figures and mathematics in the tabular}
        \begin{tabular*}{0.8\linewidth}{@{\extracolsep{\fill}}@{}c| *{8}{c}@{}}
            Test & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & \begin{tabular}{@{}c@{}}
                                                       $7$ \\ $8$ \\ $9$\end{tabular} & $8$ \\
           \hline
           \hline
           1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
           \hline
           1 & 0 & 0 & 0 & 0 & 0 & 0& 0 & 1 \\
           \hline
           1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 1 \\
           \hline
        \end{tabular*}
    \end{threeparttable}
\end{table}

\end{document}

相关内容