booktabs

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

booktabs + tabularx + multirow + longtable — Professional Tables

booktabs + tabularx + multirow + longtable — 专业表格制作

Setup

环境配置

latex
\usepackage{booktabs}    % professional horizontal rules
\usepackage{tabularx}    % auto-width columns
\usepackage{multirow}    % cells spanning rows
\usepackage{longtable}   % multi-page tables
\usepackage{colortbl}    % colored rows/cells (optional)
\usepackage{siunitx}     % number alignment (optional)
latex
\usepackage{booktabs}    % 专业水平分隔线
\usepackage{tabularx}    % 自动宽度列
\usepackage{multirow}    % 跨行单元格
\usepackage{longtable}   % 多页表格
\usepackage{colortbl}    % 行/单元格着色(可选)
\usepackage{siunitx}     % 数字对齐(可选)

Ugly vs Professional — Before/After

简陋表格 vs 专业表格 — 前后对比

❌ Bad Table

❌ 糟糕的表格

latex
\begin{tabular}{|l|c|r|}
\hline
Name & Score & Grade \\ \hline
Alice & 95 & A \\ \hline
Bob & 82 & B \\ \hline
Carol & 78 & C \\ \hline
\end{tabular}
latex
\begin{tabular}{|l|c|r|}
\hline
姓名 & 分数 & 等级 \\ \hline
Alice & 95 & A \\ \hline
Bob & 82 & B \\ \hline
Carol & 78 & C \\ \hline
\end{tabular}

✅ Good Table

✅ 规范的表格

latex
\begin{tabular}{lcc}
\toprule
Name  & Score & Grade \\
\midrule
Alice & 95    & A \\
Bob   & 82    & B \\
Carol & 78    & C \\
\bottomrule
\end{tabular}
Rules: Never use vertical lines. Never use
\hline
. Use
\toprule
,
\midrule
,
\bottomrule
.
latex
\begin{tabular}{lcc}
\toprule
姓名  & 分数 & 等级 \\
\midrule
Alice & 95    & A \\
Bob   & 82    & B \\
Carol & 78    & C \\
\bottomrule
\end{tabular}
规则: 切勿使用竖线,切勿使用
\hline
。请使用
\toprule
\midrule
\bottomrule

booktabs Rules

booktabs 命令说明

CommandUse
\toprule
Top of table (thicker)
\midrule
Between header and body
\bottomrule
Bottom of table (thicker)
\cmidrule(lr){2-4}
Partial rule spanning columns 2–4
\addlinespace
Extra vertical space (group separator)
\addlinespace[5pt]
Custom extra space
命令用途
\toprule
表格顶部(较粗)
\midrule
表头与表体之间
\bottomrule
表格底部(较粗)
\cmidrule(lr){2-4}
跨2-4列的部分分隔线
\addlinespace
额外垂直间距(分组分隔)
\addlinespace[5pt]
自定义额外间距

cmidrule Trimming

cmidrule 修剪设置

latex
\cmidrule(l){1-2}   % trim left
\cmidrule(r){3-4}   % trim right
\cmidrule(lr){2-3}  % trim both
\cmidrule{1-4}      % no trim
latex
\cmidrule(l){1-2}   % 左侧修剪
\cmidrule(r){3-4}   % 右侧修剪
\cmidrule(lr){2-3}  % 两侧修剪
\cmidrule{1-4}      % 不修剪

Grouped Rows Example

分组行示例

latex
\begin{tabular}{llr}
\toprule
Category & Item   & Price \\
\midrule
Fruit    & Apple  & 1.20 \\
         & Banana & 0.80 \\
\addlinespace
Dairy    & Milk   & 2.50 \\
         & Cheese & 4.00 \\
\bottomrule
\end{tabular}
latex
\begin{tabular}{llr}
\toprule
类别 & 物品   & 价格 \\
\midrule
水果    & 苹果  & 1.20 \\
         & 香蕉 & 0.80 \\
\addlinespace
乳制品    & 牛奶   & 2.50 \\
         & 奶酪 & 4.00 \\
\bottomrule
\end{tabular}

tabularx — Auto-Width Columns

tabularx — 自动宽度列

latex
% X columns expand to fill \textwidth
\begin{tabularx}{\textwidth}{lXr}
\toprule
ID & Description & Price \\
\midrule
1  & A very long description that will wrap automatically & \$10 \\
2  & Short & \$20 \\
\bottomrule
\end{tabularx}
Column typeBehavior
l
,
c
,
r
Fixed (natural width)
X
Expands to fill remaining space (left-aligned)
>{\centering\arraybackslash}X
Centered X column
>{\raggedleft\arraybackslash}X
Right-aligned X column
p{3cm}
Paragraph, fixed width
latex
% X列会自动扩展以填满\textwidth宽度
\begin{tabularx}{\textwidth}{lXr}
\toprule
编号 & 描述 & 价格 \\
\midrule
1  & 一段会自动换行的超长描述文本 & \$10 \\
2  & 短描述 & \$20 \\
\bottomrule
\end{tabularx}
列类型行为
l
,
c
,
r
固定宽度(自适应内容)
X
扩展以填充剩余空间(左对齐)
>{\centering\arraybackslash}X
居中对齐的X列
>{\raggedleft\arraybackslash}X
右对齐的X列
p{3cm}
段落式,固定宽度

multirow

multirow 跨行单元格

latex
\usepackage{multirow}

\begin{tabular}{lcr}
\toprule
\multirow{2}{*}{Name} & \multicolumn{2}{c}{Scores} \\
\cmidrule(l){2-3}
 & Math & English \\
\midrule
Alice & 95 & 88 \\
Bob   & 72 & 91 \\
\bottomrule
\end{tabular}
Syntax:
\multirow{nrows}{width}{content}
— use
*
for auto width.
\multicolumn{ncols}{alignment}{content}
— built into LaTeX.
latex
\usepackage{multirow}

\begin{tabular}{lcr}
\toprule
\multirow{2}{*}{姓名} & \multicolumn{2}{c}{分数} \\
\cmidrule(l){2-3}
 & 数学 & 英语 \\
\midrule
Alice & 95 & 88 \\
Bob   & 72 & 91 \\
\bottomrule
\end{tabular}
语法:
\multirow{nrows}{width}{content}
— 使用
*
表示自动宽度。
\multicolumn{ncols}{alignment}{content}
— LaTeX内置命令。

Complex Header Example

复杂表头示例

latex
\begin{tabular}{lcccccc}
\toprule
& \multicolumn{3}{c}{Treatment A} & \multicolumn{3}{c}{Treatment B} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
Subject & Pre & Post & $\Delta$ & Pre & Post & $\Delta$ \\
\midrule
1 & 5.2 & 3.1 & $-2.1$ & 4.8 & 4.5 & $-0.3$ \\
2 & 6.1 & 2.8 & $-3.3$ & 5.5 & 5.2 & $-0.3$ \\
\bottomrule
\end{tabular}
latex
\begin{tabular}{lcccccc}
\toprule
& \multicolumn{3}{c}{处理方案A} & \multicolumn{3}{c}{处理方案B} \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
受试者 & 预处理 & 后处理 & $\Delta$ & 预处理 & 后处理 & $\Delta$ \\
\midrule
1 & 5.2 & 3.1 & $-2.1$ & 4.8 & 4.5 & $-0.3$ \\
2 & 6.1 & 2.8 & $-3.3$ & 5.5 & 5.2 & $-0.3$ \\
\bottomrule
\end{tabular}

longtable — Multi-Page Tables

longtable — 多页表格

latex
\usepackage{longtable}

\begin{longtable}{lcc}
% Header for first page
\toprule
Name & Value & Unit \\
\midrule
\endfirsthead

% Header for continuation pages
\multicolumn{3}{l}{\textit{Continued from previous page}} \\
\toprule
Name & Value & Unit \\
\midrule
\endhead

% Footer for all pages except last
\midrule
\multicolumn{3}{r}{\textit{Continued on next page}} \\
\endfoot

% Footer for last page
\bottomrule
\endlastfoot

% Data rows
Alpha   & 1.0 & m \\
Beta    & 2.5 & kg \\
% ... many more rows ...
Omega   & 9.9 & s \\

\caption{Long experimental data.}
\label{tab:longdata}
\end{longtable}
Note:
longtable
replaces
tabular
entirely (not inside
table
environment). Caption and label go inside.
latex
\usepackage{longtable}

\begin{longtable}{lcc}
% 第一页表头
\toprule
名称 & 数值 & 单位 \\
\midrule
\endfirsthead

% 后续页表头
\multicolumn{3}{l}{\textit{接上文}} \\
\toprule
名称 & 数值 & 单位 \\
\midrule
\endhead

% 除最后一页外的页脚
\midrule
\multicolumn{3}{r}{\textit{下文续表}} \\
\endfoot

% 最后一页页脚
\bottomrule
\endlastfoot

% 数据行
Alpha   & 1.0 & m \\
Beta    & 2.5 & kg \\
% ... 更多数据行 ...
Omega   & 9.9 & s \\

\caption{长实验数据表。}
\label{tab:longdata}
\end{longtable}
注意:
longtable
完全替代
tabular
(不可嵌套在
table
环境中)。标题和标签需放在环境内部。

colortbl — Row and Cell Coloring

colortbl — 行与单元格着色

latex
\usepackage{colortbl}
\usepackage[table]{xcolor}  % or load xcolor with table option

% Colored row
\rowcolor{blue!10}
Alice & 95 & A \\

% Colored cell
Alice & \cellcolor{green!20} 95 & A \\

% Alternating row colors (xcolor table option)
\rowcolors{2}{gray!10}{white}  % start at row 2, alternate
\begin{tabular}{lcc}
\toprule
\rowcolor{white}  % override for header
Name & Score & Grade \\
\midrule
Alice & 95 & A \\  % gray
Bob & 82 & B \\    % white
Carol & 78 & C \\  % gray
\bottomrule
\end{tabular}
latex
\usepackage{colortbl}
\usepackage[table]{xcolor}  % 或加载带table选项的xcolor

% 着色行
\rowcolor{blue!10}
Alice & 95 & A \\

% 着色单元格
Alice & \cellcolor{green!20} 95 & A \\

% 交替行颜色(需xcolor的table选项)
\rowcolors{2}{gray!10}{white}  % 从第2行开始,交替颜色
\begin{tabular}{lcc}
\toprule
\rowcolor{white}  % 表头覆盖颜色设置
姓名 & 分数 & 等级 \\
\midrule
Alice & 95 & A \\  % 灰色
Bob & 82 & B \\    % 白色
Carol & 78 & C \\  % 灰色
\bottomrule
\end{tabular}

siunitx S Columns — Number Alignment

siunitx S列 — 数字对齐

latex
\usepackage{siunitx}

\begin{tabular}{l S[table-format=3.2] S[table-format=1.3]}
\toprule
{Material} & {Density} & {Error} \\
\midrule
Steel      & 785.00    & 0.050 \\
Aluminum   & 270.00    & 0.120 \\
Titanium   & 450.00    & 0.008 \\
Gold       & 1932.00   & 0.300 \\
\bottomrule
\end{tabular}
S column: Aligns numbers by decimal point. Wrap text headers in
{...}
.
S optionExampleMeaning
table-format
3.2
3 digits . 2 decimals
table-format
+2.1e3
sign, digits, exponent
round-mode
places
Round to decimal places
round-precision
2
Number of decimal places
latex
\usepackage{siunitx}

\begin{tabular}{l S[table-format=3.2] S[table-format=1.3]}
\toprule
{材料} & {密度} & {误差} \\
\midrule
& 785.00    & 0.050 \\
& 270.00    & 0.120 \\
& 450.00    & 0.008 \\
& 1932.00   & 0.300 \\
\bottomrule
\end{tabular}
S列: 按小数点对齐数字。文本表头需用
{...}
包裹。
S列选项示例说明
table-format
3.2
3位整数 . 2位小数
table-format
+2.1e3
符号、数字、指数
round-mode
places
按小数位数四舍五入
round-precision
2
保留小数位数

Full Example — Publication-Quality Table

完整示例 — 出版级表格

latex
\begin{table}[htbp]
  \centering
  \caption{Performance comparison of algorithms.}
  \label{tab:comparison}
  \begin{tabular}{l S[table-format=2.1] S[table-format=3.0] c}
    \toprule
    {Method} & {Accuracy (\%)} & {Time (ms)} & {Converged?} \\
    \midrule
    Baseline   & 72.3 & 150 & Yes \\
    Improved   & 85.1 & 230 & Yes \\
    Proposed   & 91.7 &  98 & Yes \\
    \addlinespace
    Oracle     & 99.2 &  45 & N/A \\
    \bottomrule
  \end{tabular}
\end{table}
latex
\begin{table}[htbp]
  \centering
  \caption{算法性能对比。}
  \label{tab:comparison}
  \begin{tabular}{l S[table-format=2.1] S[table-format=3.0] c}
    \toprule
    {方法} & {准确率 (\%)} & {耗时 (ms)} & {是否收敛} \\
    \midrule
    基准算法   & 72.3 & 150 &\\
    改进算法   & 85.1 & 230 &\\
    提出算法   & 91.7 &  98 &\\
    \addlinespace
    理想模型     & 99.2 &  45 & 不适用 \\
    \bottomrule
  \end{tabular}
\end{table}

Common Pitfalls

常见问题

ProblemCauseFix
Vertical linesUsing `` in column spec
\hline
Old habitUse
\toprule/\midrule/\bottomrule
Table too wideToo many columnsUse
tabularx
with
X
columns, or
\resizebox
longtable
in
table
Incompatible
longtable
replaces
table
environment
\multirow
misaligned
Row height differencesAdd
\rule{0pt}{2.5ex}
or adjust manually
S
column header error
Text in S columnWrap header in
{...}
Footnotes in table
\footnote
fails in tabular
Use
\tablefootnote
(tablefootnote pkg) or threeparttable
booktabs
+
colortbl
gaps
Rules have gaps with colorAdd
\abovetopsep=0pt
or use
\arrayrulecolor
问题原因解决方法
出现竖线列格式中使用了``
使用
\hline
旧习惯替换为
\toprule/\midrule/\bottomrule
表格过宽列数过多使用带
X
列的
tabularx
,或
\resizebox
缩放
longtable
嵌套在
table
两者不兼容
longtable
直接替代
table
环境
\multirow
对齐错位
行高不一致添加
\rule{0pt}{2.5ex}
或手动调整
S列表头报错S列中包含文本表头用
{...}
包裹
表格内脚注失效
\footnote
在tabular中无法正常工作
使用
\tablefootnote
(需tablefootnote宏包)或threeparttable
booktabs
+
colortbl
出现间隙
分隔线与颜色区域存在间隙添加
\abovetopsep=0pt
或使用
\arrayrulecolor

Tips

技巧提示

  • Three rules only:
    \toprule
    ,
    \midrule
    ,
    \bottomrule
    — almost never need more
  • Use
    \addlinespace
    to group rows visually instead of extra rules
  • Put
    \caption
    above the table (convention for tables, unlike figures)
  • \small
    or
    \footnotesize
    before tabular to shrink wide tables
  • threeparttable
    package for proper table footnotes
  • \arraystretch
    to adjust row height:
    \renewcommand{\arraystretch}{1.2}
  • 仅用三种分隔线:
    \toprule
    \midrule
    \bottomrule
    — 几乎无需其他分隔线
  • 使用
    \addlinespace
    通过留白分组行,而非额外分隔线
  • 表格标题放在表格上方(表格的惯例,与图片不同)
  • 在tabular前使用
    \small
    \footnotesize
    缩小过宽的表格
  • 使用threeparttable宏包实现规范的表格脚注
  • 使用
    \arraystretch
    调整行高:
    \renewcommand{\arraystretch}{1.2}