跳转至

LaTeX排版

LaTeX排版结构示意图

🎯 学习目标

完成本章学习后,你将能够: - 理解LaTeX的工作原理和为什么学术论文使用LaTeX - 配置LaTeX工作环境(Overleaf在线或本地TeX Live + VS Code) - 掌握LaTeX基础语法,排版结构化文档 - 插入数学公式、图片、表格和代码块 - 使用BibTeX管理参考文献 - 使用IEEE、ACM、NeurIPS等学术模板 - 用TikZ绘制简单的模型结构图


一、LaTeX是什么

LaTeX(发音为"LAH-tek"或"LAY-tek")是一套基于TeX的排版系统,由Leslie Lamport在1980年代开发。与Word的"所见即所得"(WYSIWYG)不同,LaTeX采用"所写即所得"(WYSIWYM)的理念——你编写标记语言源代码,LaTeX引擎将其编译为精美的PDF文档。

为什么学术论文用LaTeX

对比项 LaTeX Word
数学公式 ⭐⭐⭐⭐⭐ 原生支持,排版精美 ⭐⭐⭐ 公式编辑器功能有限
参考文献 ⭐⭐⭐⭐⭐ BibTeX自动管理 ⭐⭐⭐ 需要Endnote辅助
排版质量 ⭐⭐⭐⭐⭐ 专业出版级别 ⭐⭐⭐ 一般
模板支持 ⭐⭐⭐⭐⭐ 所有顶会都提供LaTeX模板 ⭐⭐ 少数会议提供Word模板
格式一致性 ⭐⭐⭐⭐⭐ 编译保证一致 ⭐⭐ 容易格式错乱
学习曲线 ⭐⭐ 较陡 ⭐⭐⭐⭐ 容易上手
协作 ⭐⭐⭐⭐ Overleaf在线协作 ⭐⭐⭐⭐ Office在线协作

💡 提示:在AI/CS领域,几乎所有顶级会议(NeurIPS, ICML, CVPR, ACL等)都使用LaTeX提交论文。掌握LaTeX是必备技能。


二、安装方案

2.1 Overleaf(在线方案,推荐新手)

Overleaf是最流行的在线LaTeX编辑器,直接在浏览器中使用,无需安装。

特点: - 无需配置环境,注册即用 - 实时预览编译结果 - 支持多人在线协作 - 丰富的模板库 - 与Zotero集成

使用步骤: 1. 访问 https://www.overleaf.com/ 注册账号 2. 点击"New Project"创建项目 3. 选择模板或空白项目 4. 左侧编写代码,右侧实时预览

⚠️ 注意:Overleaf免费版有编译时长限制(通常60秒),对于大型项目可能不够。如果编译超时,可以考虑升级或使用本地方案。

2.2 TeX Live + VS Code(本地方案)

适合需要离线工作或项目较大的情况。

安装步骤

Step 1:安装TeX Live

Bash
# Windows:下载TeX Live安装程序
# https://www.tug.org/texlive/acquire-netinstall.html
# 选择"install-tl-windows.exe",完整安装约需5GB空间

# macOS:安装MacTeX
brew install --cask mactex

# Linux:
sudo apt-get install texlive-full

Step 2:配置VS Code

  1. 安装VS Code扩展:LaTeX Workshop(作者James Yu)
  2. 配置settings.json:
JSON
{
  "latex-workshop.latex.autoBuild.run": "onSave",
  "latex-workshop.latex.tools": [
    {
      "name": "latexmk",
      "command": "latexmk",
      "args": [
        "-synctex=1",
        "-interaction=nonstopmode",
        "-file-line-error",
        "-pdf",
        "-outdir=%OUTDIR%",
        "%DOC%"
      ]
    }
  ],
  "latex-workshop.view.pdf.viewer": "tab"
}
  1. 保存.tex文件时自动编译,在VS Code中预览PDF

三、LaTeX基础语法

3.1 文档结构

每个LaTeX文档都遵循相同的基本结构:

TeX
% 文档类型声明
\documentclass[12pt,a4paper]{article}

% 导言区(Preamble):加载包、定义命令
\usepackage[utf8]{inputenc}      % 编码
\usepackage{amsmath,amssymb}     % 数学包
\usepackage{graphicx}            % 图片
\usepackage{hyperref}            % 超链接
\usepackage{booktabs}            % 精美表格

\title{My First LaTeX Document}
\author{Your Name}
\date{\today}

% 正文区
\begin{document}

\maketitle   % 生成标题

\begin{abstract}
This is the abstract of the paper.
\end{abstract}

\section{Introduction}
Your introduction text here.

\section{Method}
Your method description here.

\end{document}

3.2 标题与章节

TeX
\section{一级标题}           % 1. 一级标题
\subsection{二级标题}        % 1.1 二级标题
\subsubsection{三级标题}     % 1.1.1 三级标题
\paragraph{段落标题}         % 加粗的段落标题(无编号)

3.3 文本格式

TeX
\textbf{加粗文本}            % Bold
\textit{斜体文本}            % Italic
\underline{下划线}           % Underline
\texttt{等宽字体}            % Monospace(用于代码)

% 无序列表
\begin{itemize}
  \item 第一项
  \item 第二项
  \item 第三项
\end{itemize}

% 有序列表
\begin{enumerate}
  \item 第一步
  \item 第二步
  \item 第三步
\end{enumerate}

3.4 数学公式

LaTeX对数学公式的支持是其最大的优势之一。

行内公式

TeX
将公式嵌入文本中:$E = mc^2$ 描述了质能等价关系。

行间公式(无编号)

TeX
$$
\mathcal{L} = -\sum_{i=1}^{N} y_i \log(\hat{y}_i) + (1-y_i)\log(1-\hat{y}_i)
$$

行间公式(带编号)

TeX
\begin{equation}
\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
\label{eq:attention}
\end{equation}

多行对齐公式

TeX
\begin{align}
\mathcal{L}_{total} &= \mathcal{L}_{cls} + \lambda_1 \mathcal{L}_{reg} + \lambda_2 \mathcal{L}_{aux} \\
&= -\sum_i y_i \log p_i + \lambda_1 \|w\|_2^2 + \lambda_2 \mathcal{L}_{aux}
\end{align}

3.5 常用数学符号速查表

类别 符号 LaTeX代码
希腊字母 α, β, γ, δ, θ, λ, μ, σ \alpha, \beta, \gamma, \delta, \theta, \lambda, \mu, \sigma
运算符 ×, ÷, ±, ∞, ≤, ≥, ≠, ≈ \times, \div, \pm, \infty, \leq, \geq, \neq, \approx
集合 ∈, ∉, ⊂, ⊃, ∩, ∪, ∅ \in, \notin, \subset, \supset, \cap, \cup, \emptyset
箭头 →, ←, ↔, ⇒, ⇐ \rightarrow, \leftarrow, \leftrightarrow, \Rightarrow, \Leftarrow
大型运算 ∑, ∏, ∫, ∂ \sum, \prod, \int, \partial
矩阵/向量 x, X, x̂ \mathbf{x}, \mathbf{X}, \hat{x}
花体/空心 𝒳, ℝ, 𝔼 \mathcal{X}, \mathbb{R}, \mathbb{E}
上下标 x², xᵢ, x_{ij} x^{2}, x_{i}, x_{ij}
分数 a/b \frac{a}{b}
根号 √x \sqrt{x}

四、图片插入

4.1 基本图片插入

TeX
\usepackage{graphicx}  % 在导言区加载

\begin{figure}[htbp]   % h=here, t=top, b=bottom, p=page
  \centering
  \includegraphics[width=0.8\textwidth]{figures/model.pdf}
  \caption{Overview of our proposed method.}
  \label{fig:model}
\end{figure}

% 在正文中引用:As shown in Figure~\ref{fig:model}, ...

4.2 子图排列

TeX
\usepackage{subcaption}  % 导言区

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{figures/result_a.pdf}
    \caption{Result on Dataset A}
    \label{fig:result_a}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\textwidth}
    \includegraphics[width=\textwidth]{figures/result_b.pdf}
    \caption{Result on Dataset B}
    \label{fig:result_b}
  \end{subfigure}
  \caption{Experimental results on two datasets.}
  \label{fig:results}
\end{figure}

4.3 图片位置控制

TeX
% 位置参数含义:
% h - 尽量放在当前位置
% t - 放在页面顶部
% b - 放在页面底部
% p - 放在单独一页
% ! - 强制覆盖LaTeX的美学规则

% 推荐:[htbp] 让LaTeX自动选择最佳位置
% 强制当前位置:[H](需要 \usepackage{float})

💡 提示:论文图片建议使用矢量格式(PDF或EPS),避免使用PNG/JPG(除非是真实照片)。矢量图放大不失真,排版效果更好。


五、表格制作

5.1 基本表格

TeX
\begin{table}[htbp]
  \centering
  \caption{Performance comparison on benchmark datasets.}
  \label{tab:results}
  \begin{tabular}{lccc}
    \toprule
    Method & Accuracy & F1-Score & Latency (ms) \\
    \midrule
    Baseline A & 85.2 & 83.6 & 12.5 \\
    Baseline B & 87.5 & 85.9 & 15.3 \\
    \textbf{Ours} & \textbf{91.3} & \textbf{89.9} & \textbf{10.2} \\
    \bottomrule
  \end{tabular}
\end{table}

5.2 booktabs精美表格

booktabs包提供了\toprule\midrule\bottomrule三种线,替代传统的\hline,效果更加专业。

TeX
\usepackage{booktabs}  % 导言区

% 对比效果:
% 传统表格使用 \hline —— 线条粗细一样,不够美观
% booktabs使用 \toprule/\midrule/\bottomrule —— 粗细有别,专业感强

5.3 多行多列

TeX
\usepackage{multirow}  % 导言区

\begin{tabular}{lcccc}
  \toprule
  \multirow{2}{*}{Method} & \multicolumn{2}{c}{Dataset A} & \multicolumn{2}{c}{Dataset B} \\
  \cmidrule(lr){2-3} \cmidrule(lr){4-5}
  & Acc. & F1 & Acc. & F1 \\
  \midrule
  Method 1 & 85.2 & 83.6 & 78.1 & 76.5 \\
  Method 2 & 87.5 & 85.9 & 80.3 & 78.9 \\
  \textbf{Ours} & \textbf{91.3} & \textbf{89.9} & \textbf{84.7} & \textbf{83.2} \\
  \bottomrule
\end{tabular}

六、代码块插入

6.1 listings包

TeX
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{mystyle}{
  backgroundcolor=\color{gray!10},
  commentstyle=\color{green!50!black},
  keywordstyle=\color{blue},
  numberstyle=\tiny\color{gray},
  stringstyle=\color{red},
  basicstyle=\ttfamily\footnotesize,
  breaklines=true,
  numbers=left,
  frame=single
}
\lstset{style=mystyle}

\begin{lstlisting}[language=Python, caption=Training loop example]
for epoch in range(num_epochs):
    for batch in dataloader:
        loss = model(batch)
        loss.backward()
        optimizer.step()
\end{lstlisting}

6.2 minted包

minted包提供更精美的代码高亮,但需要Python的Pygments库。

TeX
\usepackage{minted}

\begin{minted}[linenos, frame=lines, fontsize=\footnotesize]{python}
import torch
import torch.nn as nn

class MyModel(nn.Module):
    def __init__(self, hidden_size=256):
        super().__init__()
        self.fc = nn.Linear(hidden_size, 10)
\end{minted}

⚠️ 注意:使用minted需要在编译时加上-shell-escape参数:pdflatex -shell-escape main.tex。在Overleaf上默认支持。


七、引用管理

7.1 BibTeX基本用法

Step 1:创建references.bib文件,存放所有引用条目:

BibTeX
@inproceedings{devlin2019bert,
  title     = {{BERT}: Pre-training of Deep Bidirectional Transformers
               for Language Understanding},
  author    = {Devlin, Jacob and Chang, Ming-Wei and Lee, Kenton
               and Toutanova, Kristina},
  booktitle = {Proceedings of NAACL-HLT},
  year      = {2019}
}

@article{brown2020language,
  title   = {Language Models are Few-Shot Learners},
  author  = {Brown, Tom and others},
  journal = {Advances in Neural Information Processing Systems},
  volume  = {33},
  year    = {2020}
}

Step 2:在LaTeX正文中引用:

TeX
BERT~\cite{devlin2019bert} introduced masked language modeling.
Recent work~\cite{brown2020language} showed that scaling helps.

% 文末添加参考文献列表
\bibliographystyle{plain}   % 或 ieee, acm, etc.
\bibliography{references}   % 对应 references.bib

7.2 natbib / biblatex

TeX
% natbib:传统方案,兼容性好
\usepackage{natbib}
\citep{devlin2019bert}    % (Devlin et al., 2019)
\citet{devlin2019bert}    % Devlin et al. (2019)

% biblatex:现代方案,功能更强
\usepackage[backend=biber, style=numeric]{biblatex}
\addbibresource{references.bib}
\printbibliography

八、常用学术模板

获取方式

会议/期刊 模板获取
NeurIPS https://neurips.cc/Conferences/2025/CallForPapers
ICML https://icml.cc/ → Author Guidelines
ICLR OpenReview提交系统提供
CVPR https://cvpr.thecvf.com/ → Author Guidelines
ACL https://www.aclweb.org/portal/
IEEE Overleaf搜索"IEEE Conference Template"
ACM https://www.acm.org/publications/proceedings-template

Overleaf快捷方式:在Overleaf的Templates页面搜索会议名称,可以直接使用模板创建项目。

使用建议

  1. 务必使用官方最新模板:每年的模板可能有更新
  2. 不要修改模板的格式设置:字号、页边距、行距等都不要改
  3. 注意页数限制:大多数会议主论文限8-9页(不含参考文献)
  4. Appendix规则:有些会议允许补充材料(Supplementary),有些不允许

九、TikZ画图简介

TikZ是LaTeX中最强大的绘图包,可以直接在LaTeX中绘制矢量图。

简单示例:绘制神经网络层

TeX
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}

\begin{tikzpicture}[
  node distance=2cm,
  block/.style={rectangle, draw, fill=blue!20,
                minimum height=1cm, minimum width=2cm,
                rounded corners},
  arrow/.style={-Stealth, thick}
]
  % 节点
  \node[block] (input)  {Input};
  \node[block, right=of input] (encoder) {Encoder};
  \node[block, right=of encoder] (decoder) {Decoder};
  \node[block, right=of decoder] (output) {Output};

  % 箭头
  \draw[arrow] (input)   -- (encoder);
  \draw[arrow] (encoder) -- (decoder);
  \draw[arrow] (decoder) -- (output);
\end{tikzpicture}

💡 提示:TikZ功能极其强大,但学习曲线较陡。对于复杂的模型结构图,建议先用draw.io画出草图,再用TikZ重绘。或者直接使用draw.io导出PDF,在LaTeX中作为图片引入。


十、常见排版问题与解决

问题 原因 解决方案
图片/表格位置不对 LaTeX浮动体机制 使用[H]强制定位或调整[htbp]顺序
编译报错找不到包 包未安装 运行tlmgr install 包名 或使用完整版TeX Live
中文显示乱码 编码问题 使用\usepackage{ctex}或XeLaTeX+UTF-8
参考文献不显示 编译步骤不完整 运行顺序:pdflatex → bibtex → pdflatex → pdflatex
公式编号不对 equation环境使用不当 检查\begin{equation}\end{equation}匹配
表格太宽超出页面 内容过多 使用\resizebox{\textwidth}{!}{...}缩放
链接颜色太丑 hyperref默认设置 添加\hypersetup{colorlinks=true, linkcolor=blue}

⚠️ 注意:LaTeX的错误信息往往不够直观。遇到编译错误时,从日志文件(.log)中找到第一个错误的行号,从那里开始排查。


📝 本章小结

知识点 核心要点
环境搭建 Overleaf在线(新手推荐)或TeX Live + VS Code(本地)
文档结构 \documentclass → 导言区 → \begin{document} → 正文
数学公式 行内$...$、行间$$...$$equationalign
图表 figure + graphicxtable + booktabs
引用 BibTeX + \cite,配合natbib或biblatex
模板 使用官方最新模板,不修改格式设置
TikZ 绘制矢量模型图,学习曲线较陡但效果出色

🔗 延伸阅读