LaTeX 表格制作指南
从基础到 booktabs 专业三线表
表格是学术论文中呈现数据的核心工具。本教程从最基本的 tabular 环境出发,逐步讲解列对齐、三线表、跨行跨列和长表格等进阶技巧,帮助你制作出符合期刊要求的专业表格。
基本表格
LaTeX 中最基础的表格由 tabular 环境创建。你需要指定列的数量和对齐方式:l 表示左对齐,c 表示居中,r 表示右对齐。列之间用 & 分隔,行末用 \\ 换行。
\begin{tabular}{l c r}
Name & Score & Grade \\
Alice & 95 & A \\
Bob & 82 & B \\
Carol & 78 & C \\
\end{tabular}除了 l、c、r 之外,还有一个非常实用的列说明符 p{width},它可以创建固定宽度的段落列,内容过长时会自动换行:
\begin{tabular}{l p{6cm} r}
Method & Description & Accuracy \\
\hline
SVM & A supervised learning model for classification & 89.3\% \\
ResNet-50 & Deep residual network with 50 layers for image
recognition tasks & 94.1\% \\
\end{tabular}列对齐与边框
你可以在列说明符之间加 | 来添加竖线边框,用 \hline 添加水平线:
\begin{tabular}{|l|c|r|}
\hline
Name & Score & Grade \\
\hline
Alice & 95 & A \\
Bob & 82 & B \\
Carol & 78 & C \\
\hline
\end{tabular}booktabs 宏包制作标准的三线表。booktabs 三线表
booktabs 宏包提供了三个核心命令来替代 \hline:\toprule(顶线,最粗)、\midrule(中线,中等粗细)和 \bottomrule(底线,最粗)。这三条线粗细各异、间距优化,是学术出版的事实标准。
\usepackage{booktabs}
\begin{table}[htbp]
\centering
\caption{不同优化器在 CIFAR-10 上的表现}
\label{tab:optimizers}
\begin{tabular}{l c c c}
\toprule
Optimizer & Learning Rate & Epochs & Accuracy (\%) \\
\midrule
SGD & 0.01 & 200 & 93.2 \\
Adam & 0.001 & 150 & 94.5 \\
AdamW & 0.001 & 150 & 95.1 \\
LAMB & 0.005 & 100 & 94.8 \\
\bottomrule
\end{tabular}
\end{table}使用 booktabs 时有几个要点:不要使用竖线 |,不要使用 \hline,让数据自然对齐,保持表格简洁。如果需要在数据行之间增加分隔(例如分组),可以使用 \cmidrule 命令:
\begin{tabular}{l c c}
\toprule
\multicolumn{1}{l}{Model} &
\multicolumn{2}{c}{Metrics} \\
\cmidrule(lr){2-3}
& Precision & Recall \\
\midrule
Baseline & 0.82 & 0.79 \\
Ours & 0.91 & 0.88 \\
\bottomrule
\end{tabular}跨列与跨行
当你需要让一个单元格横跨多列时,使用 \multicolumn{cols}{align}{text}。需要跨行时,先加载 multirow 宏包,然后使用 \multirow{rows}{width}{text}。
\usepackage{booktabs}
\usepackage{multirow}
\begin{table}[htbp]
\centering
\caption{跨行跨列示例}
\label{tab:multi}
\begin{tabular}{l l c c}
\toprule
\multirow{2}{*}{Category} &
\multirow{2}{*}{Method} &
\multicolumn{2}{c}{Dataset} \\
\cmidrule(lr){3-4}
& & MNIST & CIFAR-10 \\
\midrule
\multirow{2}{*}{Traditional}
& SVM & 97.1 & 62.3 \\
& Random Forest & 96.8 & 55.7 \\
\midrule
\multirow{2}{*}{Deep Learning}
& CNN & 99.3 & 91.2 \\
& ResNet & 99.5 & 94.7 \\
\bottomrule
\end{tabular}
\end{table}\multicolumn 还有一个常见用途:临时修改某个单元格的对齐方式。例如在 \multicolumn{1}{l}{text} 中,虽然只跨一列,但可以将该单元格改为左对齐。
长表格
普通的 tabular 不能跨页。当表格行数很多时,需要使用 longtable 宏包。它允许表格自动在页面底部断开,在下一页继续,并且可以设置每页重复的表头和表尾。
\usepackage{longtable}
\usepackage{booktabs}
\begin{longtable}{l c p{7cm}}
\caption{实验参数一览表} \label{tab:params} \\
\toprule
Parameter & Value & Description \\
\midrule
\endfirsthead
% 后续页面的表头
\multicolumn{3}{l}{\textit{续表~\ref{tab:params}}} \\
\toprule
Parameter & Value & Description \\
\midrule
\endhead
% 每页底部
\midrule
\multicolumn{3}{r}{\textit{续下页}} \\
\endfoot
% 最后一页底部
\bottomrule
\endlastfoot
Learning Rate & 0.001 & Initial learning rate with cosine annealing \\
Batch Size & 256 & Per-GPU batch size, total effective batch 1024 \\
Weight Decay & 0.05 & Applied to all parameters except bias and norm \\
Warmup Epochs & 5 & Linear warmup from 0 to base learning rate \\
Total Epochs & 300 & Full training run on 4x A100 GPUs \\
Dropout & 0.1 & Applied after each attention and FFN block \\
Label Smoothing & 0.1 & Cross-entropy with label smoothing \\
% ... 可以继续添加更多行
\end{longtable}注意 longtable 不需要嵌套在 table 浮动体中,它自身就处理标题和编号。四个关键命令——\endfirsthead、\endhead、\endfoot、\endlastfoot——分别定义首页表头、后续页表头、每页表尾和末页表尾。
表格标题与交叉引用
在正式论文中,表格通常放在 table 浮动体环境里,用 \caption 添加标题,用 \label 设置引用标签,然后在正文中通过 \ref 引用。这是学术论文的标准做法。
\begin{table}[htbp]
\centering
\caption{各模型在测试集上的性能对比}
\label{tab:results}
\begin{tabular}{l c c c}
\toprule
Model & Precision & Recall & F1-Score \\
\midrule
BERT & 88.2 & 85.7 & 86.9 \\
RoBERTa & 90.1 & 88.4 & 89.2 \\
GPT-2 & 87.5 & 86.2 & 86.8 \\
Ours & \textbf{92.3} & \textbf{90.8} & \textbf{91.5} \\
\bottomrule
\end{tabular}
\end{table}
% 在正文中引用:
如表~\ref{tab:results}~所示,我们的方法在所有指标上
均优于基线模型。几个重要细节:\label 必须放在 \caption 之后,否则引用编号会出错。浮动体参数 [htbp] 告诉 LaTeX 优先将表格放在当前位置(h)、页面顶部(t)、底部(b),或单独一页(p)。建议使用 tab: 前缀命名表格标签(如 tab:results),与图片的 fig: 前缀区分。
CoCraft 的表格设计器
虽然手写 LaTeX 表格代码是每位研究者的基本功,但当表格规模较大或需要频繁调整时,手动对齐 & 和 \\ 会变得繁琐且容易出错。CoCraft 编辑器内置了可视化表格设计器:你可以像在电子表格中一样添加行列、合并单元格、设置对齐方式,设计器会自动生成格式规范的 LaTeX 代码(默认使用 booktabs 三线表样式),你只需专注于数据本身。
在 CoCraft 中设计表格
使用可视化表格设计器,所见即所得地创建 LaTeX 表格。
edit_document免费开始使用