我正在尝试定义表格列的最大宽度,请参阅以下代码:
\documentclass{article}
\usepackage[]{float}
\usepackage[]{hyperref}
\usepackage[]{booktabs}
\usepackage[]{array}
\usepackage[]{varwidth}
\title{}
\author{}
\date{\today}
\newcolumntype{\vbf}{>{\begin{varwidth}{3.0cm}}l<{\end{varwidth}}}
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{\vbf\vbf\vbf\vbf\vbf\vbf}
\toprule
0 & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 & 210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 & 331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 & 282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 & 297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
错误是:
! Misplaced \noalign.
\midrule ^^@-\noalign
{\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global \@...
l.21 \midrule
我该如何修复这个错误?
答案1
\arraybackslash
错误是由于您缺少列定义而导致的。我还建议不要使用宏来命名列类型\vbf
,最好选择一些字母,例如,在您想要重复的答案中使用过的字母。所以:
\documentclass{article}
\usepackage[]{float}
\usepackage[]{booktabs}
\usepackage[]{array}
\usepackage[]{varwidth}
\newcolumntype{M}{>{\begin{varwidth}{4cm}\arraybackslash}l<{\end{varwidth}}}
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{*{7}{M}}
\toprule
0 & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 & 210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 & 331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 & 282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 & 297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
效果正如你预期。已测试!
答案2
您找到的示例表明了varwidth
一种非常不同的情况,即需要在单元格中使用换行符。在您的表格中,您没有这种东西,因此输出与您仅使用 时完全相同l
。
下面是证明该说法的代码,使用\tabularnewline
而不是\\
作为结尾行:
\documentclass{article}
\usepackage[]{booktabs}
\usepackage[]{array}
\usepackage[]{varwidth}
\newcolumntype{\vbf}{>{\begin{varwidth}{3.0cm}}l<{\end{varwidth}}}
\title{}
\author{}
\date{\today}
\begin{document}
\begin{tabular}{\vbf\vbf\vbf\vbf\vbf\vbf}
\toprule
0 & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 & 210947.01 & 6716332.35 & 6808728.66 \tabularnewline
2 & 9992538.31 & 10211855.55 & 331381.44 & 6444971.54 & 6403637.45 \tabularnewline
3 & 9989460.45 & 10174367.75 & 282403.66 & 7065539.95 & 7152965.85 \tabularnewline
4 & 11773342.70 & 10413039.41 & 297383.17 & 6267637.00 & 6375493.07 \tabularnewline
\bottomrule
\end{tabular}
\bigskip
\begin{tabular}{l l l l l l}
\toprule
0 & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 & 210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 & 331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 & 282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 & 297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}
\end{document}
以下是输入表格的另一种方法,使用siunitx
:
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{table}[htp]
\centering
\sisetup{group-digits=false}
\begin{tabular}{
c
S[table-format=8.2]
S[table-format=8.2]
S[table-format=6.2]
S[table-format=7.2]
S[table-format=7.2]
}
\toprule
0 & {1} & {2} & {3} & {4} & {5} \\
\midrule
1 & 11802316.16 & 10270230.14 & 210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 & 331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 & 282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 & 297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案3
V
如果您使用定义的列类型varwidth
,您将直接获得预期的输出。
但是,就您而言,正如其他答案所述,最好使用S
列siunitx
。
\documentclass{article}
\usepackage{float}
\usepackage{booktabs}
\usepackage{array}
\usepackage{varwidth}% must be loaded after array
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{V{3cm}V{3cm}V{3cm}V{3cm}V{3cm}V{3cm}}
\toprule
0 & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 & 210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 & 331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 & 282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 & 297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}