引言

LaTeX 是学术界公认的专业排版系统,尤其在数学、物理、计算机科学等领域被广泛使用。本文将介绍一些进阶技巧,帮助你写出更专业的学术论文。

1. 自定义命令与环境

1.1 自定义数学命令

为了提高写作效率和保持符号一致性,我们可以定义常用的数学命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
% 在导言区定义
\newcommand{\R}{\mathbb{R}} % 实数集
\newcommand{\N}{\mathbb{N}} % 自然数集
\newcommand{\Z}{\mathbb{Z}} % 整数集
\newcommand{\C}{\mathbb{C}} % 复数集

% 向量和矩阵
\newcommand{\bvec}[1]{\boldsymbol{#1}} % 粗体向量
\newcommand{\norm}[1]{\left\| #1 \right\|} % 范数
\newcommand{\inner}[2]{\langle #1, #2 \rangle} % 内积

% 偏导数
\newcommand{\pdv}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\ddv}[2]{\frac{d #1}{d #2}}

使用示例:对于 f:RnRf: \R^n \to \R,其梯度为:

\nabla f = \left( \pdv{f}{x_1}, \pdv{f}{x_2}, \ldots, \pdv{f}{x_n} \right)

1.2 带参数的命令

1
2
3
4
5
6
7
8
% 定义带可选参数的命令
\newcommand{\integral}[4][\infty]{%
\int_{#2}^{#1} #3 \, d#4
}

% 使用
\integral{0}{f(x)}{x} % 从 0 到 ∞
\integral[\pi]{0}{\sin x}{x} % 从 0 到 π

2. 定理环境配置

2.1 使用 amsthm 包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\usepackage{amsthm}

% 定义定理样式
\theoremstyle{plain} % 默认样式(斜体)
\newtheorem{theorem}{定理}[section]
\newtheorem{lemma}[theorem]{引理}
\newtheorem{proposition}[theorem]{命题}
\newtheorem{corollary}[theorem]{推论}

\theoremstyle{definition} % 定义样式(正体)
\newtheorem{definition}[theorem]{定义}
\newtheorem{example}[theorem]{例}

\theoremstyle{remark} % 注记样式
\newtheorem*{remark}{注} % 不编号
\newtheorem*{note}{注意}

2.2 自定义证明环境

1
2
3
4
5
6
7
8
9
10
11
12
% 自定义证明结束符号
\renewcommand{\qedsymbol}{$\blacksquare$}

% 或使用中文
\renewenvironment{proof}[1][证明]{%
\par\pushQED{\qed}%
\normalfont \topsep6\p@\@plus6\p@\relax
\trivlist
\item[\hskip\labelsep\textbf{#1.}]\ignorespaces
}{%
\popQED\endtrivlist\@endpefalse
}

3. 参考文献管理

3.1 BibTeX 基础

创建 references.bib 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@article{einstein1905,
author = {Einstein, Albert},
title = {Zur Elektrodynamik bewegter Körper},
journal = {Annalen der Physik},
volume = {322},
number = {10},
pages = {891--921},
year = {1905},
doi = {10.1002/andp.19053221004}
}

@book{rudin1976,
author = {Rudin, Walter},
title = {Principles of Mathematical Analysis},
publisher = {McGraw-Hill},
year = {1976},
edition = {3rd},
isbn = {978-0-07-054235-8}
}

@inproceedings{lecun1998,
author = {LeCun, Yann and Bottou, Léon and Bengio, Yoshua and Haffner, Patrick},
title = {Gradient-based learning applied to document recognition},
booktitle = {Proceedings of the IEEE},
volume = {86},
number = {11},
pages = {2278--2324},
year = {1998}
}

3.2 使用 biblatex

推荐使用现代的 biblatex 包:

1
2
3
4
5
6
7
8
9
10
11
12
\usepackage[
backend=biber,
style=numeric, % 或 authoryear, alphabetic
sorting=nyt, % 按名字、年份、标题排序
maxbibnames=3, % 参考文献列表最多显示3个作者
maxcitenames=2, % 正文引用最多显示2个作者
]{biblatex}

\addbibresource{references.bib}

% 在文档末尾
\printbibliography[title={参考文献}]

4. 图表绘制

4.1 TikZ 基础

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, positioning}

\begin{tikzpicture}
% 绘制坐标轴
\draw[->] (-0.5,0) -- (4,0) node[right] {$x$};
\draw[->] (0,-0.5) -- (0,3) node[above] {$y$};

% 绘制函数曲线
\draw[thick, blue, domain=0:3.5, samples=100]
plot (\x, {0.5*\x*\x - 0.1*\x*\x*\x});

% 标注
\node at (2.5, 2) {$f(x) = \frac{1}{2}x^2 - \frac{1}{10}x^3$};
\end{tikzpicture}

4.2 绘制神经网络图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
\usetikzlibrary{chains, fit, backgrounds}

\begin{tikzpicture}[
node distance=1cm,
neuron/.style={circle, draw, minimum size=0.8cm},
input/.style={neuron, fill=green!20},
hidden/.style={neuron, fill=blue!20},
output/.style={neuron, fill=red!20},
]
% 输入层
\foreach \i in {1,2,3} {
\node[input] (i\i) at (0, -\i) {$x_\i$};
}

% 隐藏层
\foreach \j in {1,2,3,4} {
\node[hidden] (h\j) at (2, -\j+0.5) {};
}

% 输出层
\foreach \k in {1,2} {
\node[output] (o\k) at (4, -\k-0.5) {$y_\k$};
}

% 连接
\foreach \i in {1,2,3} {
\foreach \j in {1,2,3,4} {
\draw[->] (i\i) -- (h\j);
}
}
\foreach \j in {1,2,3,4} {
\foreach \k in {1,2} {
\draw[->] (h\j) -- (o\k);
}
}
\end{tikzpicture}

5. 算法排版

5.1 使用 algorithm2e

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
\usepackage[ruled, linesnumbered]{algorithm2e}

\begin{algorithm}[H]
\caption{梯度下降算法}
\KwIn{初始点 $x_0$,学习率 $\eta$,迭代次数 $T$}
\KwOut{近似最优解 $x^*$}

$x \gets x_0$\;
\For{$t = 1$ \KwTo $T$}{
$g \gets \nabla f(x)$\;
$x \gets x - \eta \cdot g$\;
\If{$\|g\| < \epsilon$}{
\textbf{break}\;
}
}
\Return{$x$}\;
\end{algorithm}

6. 实用技巧

6.1 交叉引用

1
2
3
4
5
\usepackage{cleveref}  % 智能引用

% 使用
\cref{thm:main}所示... % 自动添加"定理"
\cref{eq:euler,eq:newton}可知... % 自动处理多个引用

6.2 单位排版

1
2
3
4
5
6
\usepackage{siunitx}

% 使用
速度为 \SI{3e8}{m/s}
质量为 \SI{9.109e-31}{kg}
温度为 \SI{273.15}{K}

6.3 代码高亮

1
2
3
4
5
6
7
8
9
\usepackage{minted}

\begin{minted}[linenos, frame=lines]{python}
def gradient_descent(f, x0, lr=0.01, epochs=1000):
x = x0
for _ in range(epochs):
x = x - lr * gradient(f, x)
return x
\end{minted}

推荐模板结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
\documentclass[11pt, a4paper]{article}

% 基础包
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{graphicx}
\usepackage{hyperref}

% 进阶包
\usepackage{tikz}
\usepackage{algorithm2e}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{cleveref}
\usepackage[backend=biber]{biblatex}

% 自定义命令
\input{macros.tex}

% 定理环境
\input{theorems.tex}

\begin{document}

\title{论文标题}
\author{作者名}
\date{\today}
\maketitle

\begin{abstract}
摘要内容...
\end{abstract}

\tableofcontents

\section{引言}
...

\printbibliography

\end{document}

总结

掌握 LaTeX 进阶技巧可以显著提高论文写作效率:

  1. 自定义命令:保持符号一致性,减少重复输入
  2. 定理环境:规范化数学文档结构
  3. 参考文献:使用 biblatex 实现自动化管理
  4. 图表绘制:TikZ 可以绑制出版级质量的图形
  5. 算法排版:清晰展示算法流程

参考资料