latex

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

LaTeX Skill

LaTeX 使用技能

1. Overview

1. 概述

LaTeX is a markup language and typesetting system for producing high-quality documents. It excels at structured documents with complex formatting, mathematics, cross-references, and bibliographies.
When to use LaTeX:
  • Academic papers, journal submissions
  • Theses and dissertations
  • Technical documentation
  • CVs and résumés
  • Presentations (Beamer)
  • Books, reports, letters
Key concepts:
ConceptDescription
PreambleEverything before
\begin{document}
— class, packages, settings
Document bodyContent between
\begin{document}
and
\end{document}
CommandsStart with
\
, e.g.
\textbf{bold}
. Optional args in
[]
, required in
{}
Environments
\begin{name}...\end{name}
blocks for scoped formatting
PackagesExtensions loaded with
\usepackage{name}
in the preamble
LaTeX是一种用于生成高质量文档的标记语言和排版系统,尤其擅长处理包含复杂格式、数学公式、交叉引用和参考文献的结构化文档。
LaTeX适用场景:
  • 学术论文、期刊投稿
  • 硕士/博士学位论文
  • 技术文档
  • 个人简历(CVs/résumés)
  • 演示文稿(Beamer)
  • 书籍、报告、信函
核心概念:
概念描述
导言区(Preamble)位于
\begin{document}
之前的所有内容——文档类、宏包、设置项
文档主体(Document body)位于
\begin{document}
\end{document}
之间的内容
命令(Commands)
\
开头,例如
\textbf{bold}
。可选参数放在
[]
中,必填参数放在
{}
环境(Environments)
\begin{name}...\end{name}
格式的代码块,用于局部范围的格式设置
宏包(Packages)在导言区通过
\usepackage{name}
加载的扩展工具,用于增强LaTeX功能

2. Document Structure

2. 文档结构

Document Classes

文档类

latex
\documentclass[options]{class}
ClassUse case
article
Short documents, papers, assignments
report
Longer documents with chapters
book
Books (front/back matter, chapters)
beamer
Presentations/slides
letter
Formal letters
memoir
Flexible — replaces article/report/book
latex
\documentclass[options]{class}
文档类适用场景
article
短篇文档、论文、作业
report
包含章节的长篇文档
book
书籍(包含前言/后语、章节)
beamer
演示文稿/幻灯片
letter
正式信函
memoir
灵活通用的文档类,可替代article/report/book

Common Class Options

常用文档类选项

latex
\documentclass[12pt,a4paper,twocolumn,draft]{article}
OptionValues
Font size
10pt
,
11pt
,
12pt
Paper
a4paper
,
letterpaper
,
a5paper
Columns
onecolumn
,
twocolumn
Sides
oneside
,
twoside
Draft
draft
(shows overfull boxes, skips images)
latex
\documentclass[12pt,a4paper,twocolumn,draft]{article}
选项可选值
字体大小
10pt
,
11pt
,
12pt
纸张尺寸
a4paper
,
letterpaper
,
a5paper
分栏方式
onecolumn
,
twocolumn
单面/双面
oneside
,
twoside
草稿模式
draft
(显示超宽行标记,跳过图片加载)

Minimal Document

最小文档示例

latex
\documentclass[12pt,a4paper]{article}

% === PREAMBLE ===
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amssymb}
\usepackage{graphicx}
\usepackage{hyperref}

\title{My Document}
\author{Author Name}
\date{\today}

% === BODY ===
\begin{document}
\maketitle
\tableofcontents

\section{Introduction}
Content here.

\end{document}
latex
\documentclass[12pt,a4paper]{article}

% === 导言区 ===
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath, amssymb}
\usepackage{graphicx}
\usepackage{hyperref}

\title{我的文档}
\author{作者姓名}
\date{\today}

% === 主体内容 ===
\begin{document}
\maketitle
\tableofcontents

\section{引言}
内容写在这里。

\end{document}

3. Text Formatting

3. 文本格式设置

Style Commands

样式命令

CommandResult
\textbf{text}
bold
\textit{text}
italic
\underline{text}
underlined
\emph{text}
emphasis (italic, or upright if already italic)
\texttt{text}
monospace
\textsc{text}
SMALL CAPS
\textsf{text}
sans-serif
命令效果
\textbf{text}
粗体
\textit{text}
斜体
\underline{text}
下划线
\emph{text}
强调(默认斜体,若已为斜体则变为正体)
\texttt{text}
等宽字体
\textsc{text}
小型大写字母
\textsf{text}
无衬线字体

Font Sizes

字体大小

Smallest to largest:
\tiny  \scriptsize  \footnotesize  \small  \normalsize
\large  \Large  \LARGE  \huge  \Huge
Use as:
{\large This text is large.}
or as environment.
从小到大依次为:
\tiny  \scriptsize  \footnotesize  \small  \normalsize
\large  \Large  \LARGE  \huge  \Huge
使用方式:
{\large 这段文字是大号字体。}
或作为环境使用。

Font Families

字体族

CommandDeclarationFamily
\textrm{}
\rmfamily
Serif (Roman)
\textsf{}
\sffamily
Sans-serif
\texttt{}
\ttfamily
Monospace
命令声明式命令字体族
\textrm{}
\rmfamily
衬线字体(罗马体)
\textsf{}
\sffamily
无衬线字体
\texttt{}
\ttfamily
等宽字体

Alignment

对齐方式

latex
\begin{center}    Centered text.    \end{center}
\begin{flushleft} Left-aligned.     \end{flushleft}
\begin{flushright} Right-aligned.   \end{flushright}
Or inline:
\centering
,
\raggedright
,
\raggedleft
latex
\begin{center}    居中对齐文本。    \end{center}
\begin{flushleft} 左对齐文本。     \end{flushleft}
\begin{flushright} 右对齐文本。   \end{flushright}
或行内命令:
\centering
\raggedright
\raggedleft

Spacing

间距设置

latex
\vspace{1cm}          % vertical space
\hspace{2em}          % horizontal space
\vfill                % stretch vertical
\hfill                % stretch horizontal
\\[0.5cm]             % line break with extra space
\setlength{\parskip}{0.5em}   % paragraph spacing
\setlength{\parindent}{0pt}   % remove paragraph indent
\noindent             % suppress indent for one paragraph
Line spacing (requires
setspace
package):
latex
\usepackage{setspace}
\onehalfspacing    % or \doublespacing, \singlespacing
latex
\vspace{1cm}          % 垂直间距
\hspace{2em}          % 水平间距
\vfill                % 可拉伸垂直间距
\hfill                % 可拉伸水平间距
\\[0.5cm]             % 换行并添加额外间距
\setlength{\parskip}{0.5em}   % 段落间距
\setlength{\parindent}{0pt}   % 取消段落首行缩进
\noindent             % 取消当前段落的首行缩进
行间距(需加载
setspace
宏包):
latex
\usepackage{setspace}
\onehalfspacing    % 或 \doublespacing(双倍行距)、\singlespacing(单倍行距)

Special Characters

特殊字符

These characters must be escaped:
CharacterLaTeXCharacterLaTeX
%
\%
$
\$
&
\&
#
\#
_
\_
{
\{
}
\}
~
\textasciitilde
^
\textasciicircum
\
\textbackslash
以下字符需要转义后使用:
字符LaTeX写法字符LaTeX写法
%
\%
$
\$
&
\&
#
\#
_
\_
{
\{
}
\}
~
\textasciitilde
^
\textasciicircum
\
\textbackslash

4. Document Organization

4. 文档组织

Sectioning

章节划分

latex
\part{Part Title}           % only in report/book
\chapter{Chapter Title}     % only in report/book
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
\paragraph{Paragraph}
\paragraphsub{Subparagraph}
Starred versions (
\section*{}
) suppress numbering and TOC entry.
latex
\part{篇标题}           % 仅在report/book类中可用
\chapter{章标题}     % 仅在report/book类中可用
\section{}
\subsection{小节}
\subsubsection{子小节}
\paragraph{段落}
\paragraphsub{子段落}
带星号的版本(如
\section*{}
)会取消编号和目录条目。

Table of Contents

目录

latex
\tableofcontents    % requires two compilations
\listoffigures
\listoftables
latex
\tableofcontents    % 需要编译两次才能生成正确目录
\listoffigures
\listoftables

Lists

列表

latex
% Bulleted
\begin{itemize}
  \item First item
  \item Second item
\end{itemize}

% Numbered
\begin{enumerate}
  \item First
  \item Second
\end{enumerate}

% Labeled
\begin{description}
  \item[Term] Definition here.
\end{description}
Customize with
enumitem
package:
latex
\usepackage{enumitem}
\begin{enumerate}[label=(\alph*), start=1]
latex
% 无序列表
\begin{itemize}
  \item 第一项
  \item 第二项
\end{itemize}

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

% 描述列表
\begin{description}
  \item[术语] 定义内容。
\end{description}
使用
enumitem
宏包自定义列表:
latex
\usepackage{enumitem}
\begin{enumerate}[label=(\alph*), start=1]

Cross-References

交叉引用

latex
\section{Methods}\label{sec:methods}
See Section~\ref{sec:methods} on page~\pageref{sec:methods}.
With
hyperref
, use
\autoref{sec:methods}
for automatic "Section 2" text.
Rule: Always place
\label
after
\caption
(in floats) or after the sectioning command.
latex
\section{研究方法}\label{sec:methods}
请参见第~\ref{sec:methods}节,页码~\pageref{sec:methods}
加载
hyperref
宏包后,可使用
\autoref{sec:methods}
自动生成类似“第2节”的文本。
规则:
\label
必须放在浮动体的
\caption
之后,或章节命令之后。

Footnotes

脚注

latex
This has a footnote.\footnote{Footnote text here.}
latex
这段文字带有脚注。\footnote{脚注内容写在这里。}

5. Mathematics

5. 数学公式

Inline vs Display

行内公式与行间公式

latex
Inline: $E = mc^2$ or \(E = mc^2\)

Display:
\[ E = mc^2 \]

% Numbered equation:
\begin{equation}\label{eq:einstein}
  E = mc^2
\end{equation}
latex
行内公式:$E = mc^2$\(E = mc^2\)

行间公式:
\[ E = mc^2 \]

% 带编号的公式:
\begin{equation}\label{eq:einstein}
  E = mc^2
\end{equation}

Essential Packages

必备宏包

latex
\usepackage{amsmath}    % align, cases, matrices, etc.
\usepackage{amssymb}    % extra symbols (ℝ, ℤ, etc.)
\usepackage{mathtools}  % extends amsmath (dcases, coloneqq, etc.)
latex
\usepackage{amsmath}    % 增强的数学环境,如align、cases、矩阵等
\usepackage{amssymb}    % 额外的数学符号(ℝ、ℤ等)
\usepackage{mathtools}  % 扩展amsmath功能(如dcases、coloneqq等)

Common Constructs

常用公式结构

latex
% Fractions
\frac{a}{b}          \dfrac{a}{b}  (display-size)

% Roots
\sqrt{x}             \sqrt[3]{x}

% Sub/superscripts
x_{i}   x^{2}   x_{i}^{2}   a_{i,j}

% Sums, products, integrals
\sum_{i=1}^{n} x_i      \prod_{i=1}^{n} x_i
\int_{0}^{\infty} f(x)\,dx
\lim_{x \to \infty} f(x)

% Brackets (auto-sized)
\left( \frac{a}{b} \right)
\left[ ... \right]
\left\{ ... \right\}
latex
% 分数
\frac{a}{b}          \dfrac{a}{b} (行间公式大小的分数)

% 根式
\sqrt{x}             \sqrt[3]{x}

% 下标/上标
x_{i}   x^{2}   x_{i}^{2}   a_{i,j}

% 求和、乘积、积分
\sum_{i=1}^{n} x_i      \prod_{i=1}^{n} x_i
\int_{0}^{\infty} f(x)\,dx
\lim_{x \to \infty} f(x)

% 自动调整大小的括号
\left( \frac{a}{b} \right)
\left[ ... \right]
\left\{ ... \right\}

Matrices

矩阵

latex
\begin{pmatrix} a & b \\ c & d \end{pmatrix}   % (a b; c d)
\begin{bmatrix} a & b \\ c & d \end{bmatrix}   % [a b; c d]
\begin{vmatrix} a & b \\ c & d \end{vmatrix}   % |a b; c d| (determinant)
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}   % {a b; c d}
latex
\begin{pmatrix} a & b \\ c & d \end{pmatrix}   % (a b; c d)
\begin{bmatrix} a & b \\ c & d \end{bmatrix}   % [a b; c d]
\begin{vmatrix} a & b \\ c & d \end{vmatrix}   % |a b; c d|(行列式)
\begin{Bmatrix} a & b \\ c & d \end{Bmatrix}   % {a b; c d}

Aligned Equations

对齐公式

latex
\begin{align}
  f(x) &= x^2 + 2x + 1 \label{eq:f} \\
  g(x) &= x^3 - 1       \label{eq:g}
\end{align}

% No numbering:
\begin{align*}
  a &= b + c \\
  d &= e + f
\end{align*}
latex
\begin{align}
  f(x) &= x^2 + 2x + 1 \label{eq:f} \\
  g(x) &= x^3 - 1       \label{eq:g}
\end{align}

% 无编号的对齐公式:
\begin{align*}
  a &= b + c \\
  d &= e + f
\end{align*}

Cases

分段函数

latex
f(x) = \begin{cases}
  x^2  & \text{if } x \geq 0 \\
  -x^2 & \text{if } x < 0
\end{cases}
latex
f(x) = \begin{cases}
  x^2  & \text{if } x \geq 0 \\
  -x^2 & \text{if } x < 0
\end{cases}

Greek Letters

希腊字母

LowerCommandUpperCommand
α
\alpha
Α
A
β
\beta
Β
B
γ
\gamma
Γ
\Gamma
δ
\delta
Δ
\Delta
ε
\epsilon
,
\varepsilon
Ε
E
θ
\theta
Θ
\Theta
λ
\lambda
Λ
\Lambda
μ
\mu
π
\pi
Π
\Pi
σ
\sigma
Σ
\Sigma
φ
\phi
,
\varphi
Φ
\Phi
ω
\omega
Ω
\Omega
小写命令大写命令
α
\alpha
Α
A
β
\beta
Β
B
γ
\gamma
Γ
\Gamma
δ
\delta
Δ
\Delta
ε
\epsilon
,
\varepsilon
Ε
E
θ
\theta
Θ
\Theta
λ
\lambda
Λ
\Lambda
μ
\mu
π
\pi
Π
\Pi
σ
\sigma
Σ
\Sigma
φ
\phi
,
\varphi
Φ
\Phi
ω
\omega
Ω
\Omega

Common Math Symbols

常用数学符号

SymbolCommandSymbolCommand
\leq
\geq
\neq
\approx
±
\pm
×
\times
÷
\div
·
\cdot
\in
\notin
\subset
\subseteq
\cup
\cap
\infty
\partial
\nabla
\forall
\exists
\to
,
\rightarrow
\Rightarrow
\Leftrightarrow
\mathbb{R}
\mathbb{Z}
\dots
,
\ldots
,
\cdots
符号命令符号命令
\leq
\geq
\neq
\approx
±
\pm
×
\times
÷
\div
·
\cdot
\in
\notin
\subset
\subseteq
\cup
\cap
\infty
\partial
\nabla
\forall
\exists
\to
,
\rightarrow
\Rightarrow
\Leftrightarrow
\mathbb{R}
\mathbb{Z}
\dots
,
\ldots
,
\cdots

6. Tables

6. 表格

Basic Table

基础表格

latex
\begin{table}[htbp]
  \centering
  \caption{Results summary}\label{tab:results}
  \begin{tabular}{lcrp{4cm}}
    \toprule
    Name & Count & Score & Description \\
    \midrule
    Alpha & 10 & 95.2 & First entry \\
    Beta  & 20 & 87.1 & Second entry \\
    \bottomrule
  \end{tabular}
\end{table}
latex
\begin{table}[htbp]
  \centering
  \caption{结果汇总}\label{tab:results}
  \begin{tabular}{lcrp{4cm}}
    \toprule
    名称 & 数量 & 得分 & 描述 \\
    \midrule
    Alpha & 10 & 95.2 & 第一条记录 \\
    Beta  & 20 & 87.1 & 第二条记录 \\
    \bottomrule
  \end{tabular}
\end{table}

Column Specifiers

列格式说明

SpecAlignment
l
Left
c
Center
r
Right
p{width}
Paragraph (top-aligned, fixed width)
m{width}
Middle-aligned paragraph (requires
array
)
``
格式符对齐方式
l
左对齐
c
居中对齐
r
右对齐
p{width}
段落式(顶部对齐,固定宽度)
m{width}
垂直居中的段落式(需加载
array
宏包)
``

Key Packages

关键宏包

latex
\usepackage{booktabs}   % \toprule, \midrule, \bottomrule (professional rules)
\usepackage{multirow}   % \multirow{nrows}{width}{text}
\usepackage{longtable}  % tables spanning multiple pages
\usepackage{tabularx}   % auto-width X columns
latex
\usepackage{booktabs}   % 提供\toprule、\midrule、\bottomrule(专业表格线)
\usepackage{multirow}   % 提供\multirow{nrows}{width}{text}实现跨行单元格
\usepackage{longtable}  % 支持跨多页的表格
\usepackage{tabularx}   % 支持自动宽度的X列

Multi-column/row

跨列/跨行单元格

latex
\multicolumn{3}{c}{Spanning header} \\
\multirow{2}{*}{Tall cell}
latex
\multicolumn{3}{c}{跨三列的表头} \\
\multirow{2}{*}{跨行单元格}

7. Figures and Images

7. 图表与图片

latex
\usepackage{graphicx}

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.8\textwidth]{images/photo.png}
  \caption{A descriptive caption.}\label{fig:photo}
\end{figure}
latex
\usepackage{graphicx}

\begin{figure}[htbp]
  \centering
  \includegraphics[width=0.8\textwidth]{images/photo.png}
  \caption{描述性标题。}\label{fig:photo}
\end{figure}

includegraphics Options

includegraphics 选项

OptionExample
width
width=0.5\textwidth
height
height=5cm
scale
scale=0.75
angle
angle=90
trim
trim=1cm 2cm 1cm 2cm, clip
(left bottom right top)
选项示例
width
width=0.5\textwidth
height
height=5cm
scale
scale=0.75
angle
angle=90
trim
trim=1cm 2cm 1cm 2cm, clip
(左、下、右、上)

Float Placement

浮动体位置参数

SpecifierMeaning
h
Here (approximately)
t
Top of page
b
Bottom of page
p
Dedicated float page
!
Override internal limits
H
Exactly here (requires
float
package)
参数含义
h
此处(尽可能靠近当前位置)
t
页面顶部
b
页面底部
p
单独的浮动体页面
!
覆盖内部限制,强制放置
H
精确放置在当前位置(需加载
float
宏包)

Subfigures

子图

latex
\usepackage{subcaption}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.48\textwidth}
    \includegraphics[width=\textwidth]{img1.png}
    \caption{First}\label{fig:sub1}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.48\textwidth}
    \includegraphics[width=\textwidth]{img2.png}
    \caption{Second}\label{fig:sub2}
  \end{subfigure}
  \caption{Both images}\label{fig:both}
\end{figure}
latex
\usepackage{subcaption}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.48\textwidth}
    \includegraphics[width=\textwidth]{img1.png}
    \caption{第一张图}\label{fig:sub1}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.48\textwidth}
    \includegraphics[width=\textwidth]{img2.png}
    \caption{第二张图}\label{fig:sub2}
  \end{subfigure}
  \caption{两张图片合集}\label{fig:both}
\end{figure}

8. Bibliographies and Citations

8. 参考文献与引用

BibTeX Workflow

BibTeX 工作流程

  1. Create
    references.bib
    :
bibtex
@article{smith2023,
  author  = {Smith, John and Doe, Jane},
  title   = {An Important Paper},
  journal = {Journal of Examples},
  year    = {2023},
  volume  = {42},
  pages   = {1--15},
  doi     = {10.1234/example}
}

@book{knuth1984,
  author    = {Knuth, Donald E.},
  title     = {The {\TeX}book},
  publisher = {Addison-Wesley},
  year      = {1984}
}
  1. In document:
latex
\usepackage{natbib}  % or use biblatex
\bibliographystyle{plainnat}

As shown by \citet{smith2023}...  % Smith and Doe (2023)
This is known \citep{smith2023}.  % (Smith and Doe, 2023)

\bibliography{references}
  1. Compile:
    pdflatex → bibtex → pdflatex → pdflatex
  1. 创建
    references.bib
    文件:
bibtex
@article{smith2023,
  author  = {Smith, John and Doe, Jane},
  title   = {An Important Paper},
  journal = {Journal of Examples},
  year    = {2023},
  volume  = {42},
  pages   = {1--15},
  doi     = {10.1234/example}
}

@book{knuth1984,
  author    = {Knuth, Donald E.},
  title     = {The {\TeX}book},
  publisher = {Addison-Wesley},
  year      = {1984}
}
  1. 在文档中引用:
latex
\usepackage{natbib}  % 或使用biblatex
\bibliographystyle{plainnat}

\citet{smith2023}所示...  % 显示为Smith and Doe (2023)
这一结论见\citep{smith2023}% 显示为(Smith and Doe, 2023)

\bibliography{references}
  1. 编译顺序:
    pdflatex → bibtex → pdflatex → pdflatex

BibLaTeX Workflow (Modern)

BibLaTeX 工作流程(现代方式)

latex
\usepackage[backend=biber, style=authoryear]{biblatex}
\addbibresource{references.bib}

\textcite{smith2023}   % Smith and Doe (2023)
\parencite{smith2023}  % (Smith and Doe, 2023)

\printbibliography
Compile:
pdflatex → biber → pdflatex → pdflatex
latex
\usepackage[backend=biber, style=authoryear]{biblatex}
\addbibresource{references.bib}

\textcite{smith2023}   % 显示为Smith and Doe (2023)
\parencite{smith2023}  % 显示为(Smith and Doe, 2023)

\printbibliography
编译顺序:
pdflatex → biber → pdflatex → pdflatex

natbib vs biblatex

natbib vs biblatex

Featurenatbibbiblatex
BackendBibTeXBiber (recommended)
FlexibilityLimited stylesHighly customizable
UnicodePoorFull support
RecommendationLegacy projectsNew projects
特性natbibbiblatex
后端工具BibTeXBiber(推荐)
灵活性样式有限高度可定制
Unicode支持完全支持
推荐场景遗留项目新项目

9. Code Listings

9. 代码列表

verbatim

verbatim 环境

latex
\begin{verbatim}
def hello():
    print("Hello, world!")
\end{verbatim}

Inline: \verb|some_code()|
latex
\begin{verbatim}
def hello():
    print("Hello, world!")
\end{verbatim}

行内代码:\verb|some_code()|

listings Package

listings 宏包

latex
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{mystyle}{
  basicstyle=\ttfamily\small,
  keywordstyle=\color{blue},
  commentstyle=\color{gray},
  stringstyle=\color{red},
  numbers=left,
  numberstyle=\tiny,
  frame=single,
  breaklines=true
}

\begin{lstlisting}[language=Python, style=mystyle, caption={Example}]
def factorial(n):
    """Compute factorial."""
    if n <= 1:
        return 1
    return n * factorial(n - 1)
\end{lstlisting}
latex
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{mystyle}{
  basicstyle=\ttfamily\small,
  keywordstyle=\color{blue},
  commentstyle=\color{gray},
  stringstyle=\color{red},
  numbers=left,
  numberstyle=\tiny,
  frame=single,
  breaklines=true
}

\begin{lstlisting}[language=Python, style=mystyle, caption={示例代码}]
def factorial(n):
    """计算阶乘。"""
    if n <= 1:
        return 1
    return n * factorial(n - 1)
\end{lstlisting}

minted Package (Prettier, Requires Pygments)

minted 宏包(更美观,需依赖Pygments)

latex
\usepackage{minted}

\begin{minted}[linenos, frame=lines, fontsize=\small]{python}
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)
\end{minted}
Compile with:
pdflatex -shell-escape document.tex
latex
\usepackage{minted}

\begin{minted}[linenos, frame=lines, fontsize=\small]{python}
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)
\end{minted}
编译命令:
pdflatex -shell-escape document.tex

10. Presentations (Beamer)

10. 演示文稿(Beamer)

latex
\documentclass{beamer}
\usetheme{Madrid}          % or: Berlin, Warsaw, Metropolis, etc.
\usecolortheme{default}    % or: beaver, crane, dolphin, etc.

\title{My Presentation}
\author{Author}
\date{\today}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}{Outline}
  \tableofcontents
\end{frame}

\section{Introduction}
\begin{frame}{Introduction}
  \begin{itemize}
    \item<1-> First point (appears on slide 1+)
    \item<2-> Second point (appears on slide 2+)
    \item<3-> Third point
  \end{itemize}
\end{frame}

\begin{frame}{With Columns}
  \begin{columns}
    \begin{column}{0.5\textwidth}
      Left content
    \end{column}
    \begin{column}{0.5\textwidth}
      Right content
    \end{column}
  \end{columns}
\end{frame}

\end{document}
Popular themes:
Madrid
,
Berlin
,
Metropolis
(modern, clean),
Warsaw
,
CambridgeUS
latex
\documentclass{beamer}
\usetheme{Madrid}          % 可选主题:Berlin, Warsaw, Metropolis等
\usecolortheme{default}    % 可选配色:beaver, crane, dolphin等

\title{我的演示文稿}
\author{作者}
\date{\today}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}{目录}
  \tableofcontents
\end{frame}

\section{引言}
\begin{frame}{引言}
  \begin{itemize}
    \item<1-> 第一点(从第1张幻灯片开始显示)
    \item<2-> 第二点(从第2张幻灯片开始显示)
    \item<3-> 第三点
  \end{itemize}
\end{frame}

\begin{frame}{分栏布局}
  \begin{columns}
    \begin{column}{0.5\textwidth}
      左侧内容
    \end{column}
    \begin{column}{0.5\textwidth}
      右侧内容
    \end{column}
  \end{columns}
\end{frame}

\end{document}
热门主题:
Madrid
Berlin
Metropolis
(现代简洁风格)、
Warsaw
CambridgeUS

Overlay Commands

叠加显示命令

CommandEffect
\pause
Show content incrementally
\onslide<2>{text}
Show on slide 2 only
\only<1>{text}
Only on slide 1 (no space reserved)
\visible<2->{text}
Visible from slide 2 onward
\alert<2>{text}
Highlighted on slide 2
命令效果
\pause
内容逐步显示
\onslide<2>{text}
仅在第2张幻灯片显示
\only<1>{text}
仅在第1张幻灯片显示(不保留空白)
\visible<2->{text}
从第2张幻灯片开始显示
\alert<2>{text}
在第2张幻灯片高亮显示

11. Page Layout

11. 页面布局

Margins (geometry)

边距设置(geometry宏包)

latex
\usepackage[margin=2.5cm]{geometry}
% or:
\usepackage[top=3cm, bottom=3cm, left=2cm, right=2cm]{geometry}
latex
\usepackage[margin=2.5cm]{geometry}
% 或自定义各边距:
\usepackage[top=3cm, bottom=3cm, left=2cm, right=2cm]{geometry}

Headers and Footers (fancyhdr)

页眉页脚(fancyhdr宏包)

latex
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}                          % clear all
\fancyhead[L]{Left Header}
\fancyhead[R]{\rightmark}          % current section
\fancyfoot[C]{\thepage}            % page number center
\renewcommand{\headrulewidth}{0.4pt}
latex
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}                          % 清空所有默认设置
\fancyhead[L]{左侧页眉}
\fancyhead[R]{\rightmark}          % 当前节标题
\fancyfoot[C]{\thepage}            % 居中显示页码
\renewcommand{\headrulewidth}{0.4pt}

Page Numbering

页码样式

latex
\pagenumbering{roman}   % i, ii, iii (for front matter)
\pagenumbering{arabic}  % 1, 2, 3 (for main content)
\thispagestyle{empty}   % no number on this page
latex
\pagenumbering{roman}   % 罗马数字i, ii, iii(用于前言部分)
\pagenumbering{arabic}  % 阿拉伯数字1, 2, 3(用于正文)
\thispagestyle{empty}   % 当前页不显示页码

Multi-Column

多栏布局

latex
\usepackage{multicol}
\begin{multicols}{2}
  Content in two columns...
\end{multicols}
latex
\usepackage{multicol}
\begin{multicols}{2}
  双栏布局的内容...
\end{multicols}

12. Common Packages Reference

12. 常用宏包参考

PackagePurpose
amsmath
Enhanced math environments
amssymb
Extra math symbols (ℝ, ℤ, etc.)
mathtools
Extensions to amsmath
graphicx
Image inclusion
hyperref
Clickable links, PDF metadata
geometry
Page margins and dimensions
booktabs
Professional table rules
multirow
Multi-row table cells
longtable
Multi-page tables
tabularx
Auto-width table columns
xcolor
Color support
listings
Code listings
minted
Syntax-highlighted code (needs Pygments)
tikz
Programmatic graphics and diagrams
pgfplots
Data plots (built on TikZ)
fancyhdr
Custom headers/footers
setspace
Line spacing
enumitem
Customize list environments
subcaption
Subfigures and subtables
float
Extra float control (
[H]
placement)
babel
Language/hyphenation support
inputenc
Input encoding (
utf8
)
fontenc
Font encoding (
T1
)
microtype
Microtypographic enhancements
cleveref
Smart cross-references (
\cref
)
siunitx
SI units and number formatting
algorithm2e
Algorithm pseudocode
natbib
Citation management
biblatex
Modern bibliography management
csquotes
Context-sensitive quotation marks
url
Typeset URLs
caption
Customize caption formatting
appendix
Appendix management
Load order tip: Load
hyperref
last (or near-last). Exceptions:
cleveref
goes after
hyperref
.
宏包用途
amsmath
增强数学环境
amssymb
提供额外数学符号(ℝ、ℤ等)
mathtools
扩展amsmath功能
graphicx
插入图片
hyperref
生成可点击链接、PDF元数据
geometry
设置页面边距和尺寸
booktabs
生成专业表格线
multirow
实现表格跨行单元格
longtable
生成跨多页的表格
tabularx
自动调整表格列宽
xcolor
支持颜色设置
listings
插入代码列表
minted
语法高亮代码块(需Pygments)
tikz
编程方式绘制图形和图表
pgfplots
绘制数据图表(基于TikZ)
fancyhdr
自定义页眉页脚
setspace
设置行间距
enumitem
自定义列表环境
subcaption
生成子图和子表格
float
增强浮动体控制(如
[H]
参数)
babel
支持多语言和断字
inputenc
设置输入编码(如utf8)
fontenc
设置字体编码(如T1)
microtype
微排版优化
cleveref
智能交叉引用(
\cref
命令)
siunitx
排版SI单位和数字格式
algorithm2e
排版算法伪代码
natbib
引用管理
biblatex
现代参考文献管理
csquotes
上下文敏感的引号
url
排版URL链接
caption
自定义标题格式
appendix
附录管理
加载顺序提示:
hyperref
宏包应最后加载(或接近最后)。例外:
cleveref
需在
hyperref
之后加载。

13. Compilation

13. 编译

Engines

编译引擎

EngineUse when
pdflatex
Default. Most compatible. ASCII/Latin input.
xelatex
System fonts (TTF/OTF), full Unicode
lualatex
System fonts + Lua scripting, full Unicode
引擎适用场景
pdflatex
默认引擎,兼容性最好,支持ASCII/Latin编码
xelatex
支持系统字体(TTF/OTF)和完整Unicode编码
lualatex
支持系统字体+Lua脚本,完整Unicode编码

Compilation Sequence

编译顺序

bash
undefined
bash
% 基础文档编译
pdflatex document.tex

% 含参考文献的文档(BibTeX)
pdflatex document.tex
bibtex document
pdflatex document.tex
pdflatex document.tex

% 含参考文献的文档(Biber/BibLaTeX)
pdflatex document.tex
biber document
pdflatex document.tex
pdflatex document.tex

% 自动化编译(推荐)
latexmk -pdf document.tex           % 使用pdflatex编译
latexmk -xelatex document.tex       % 使用xelatex编译
latexmk -lualatex document.tex      % 使用lualatex编译
latexmk -pdf -pvc document.tex      % 实时预览,自动重新编译

Basic document

常见错误及解决方法

pdflatex document.tex
错误信息原因解决方法
Undefined control sequence
命令拼写错误或缺少对应宏包检查命令拼写;添加
\usepackage
加载宏包
Missing $ inserted
数学符号未放在数学环境中将符号包裹在
$...$
或其他数学环境中
Missing \begin{document}
导言区存在错误检查导言区的语法错误
File not found
文件路径错误或文件缺失检查文件名和路径;确认文件扩展名正确
Overfull \hbox
行宽超过页面宽度重新措辞;添加
\allowbreak
;或使用
\sloppy
命令
Undefined citation
参考文献条目缺失或未运行bibtex运行bibtex/biber;检查.bib文件中的引用键
Too many unprocessed floats
待处理的浮动体过多添加
\clearpage
命令;或使用
[H]
参数强制放置
Dimension too large
图片缩放参数错误检查
width
/
height
的取值
Package clash
两个宏包冲突只加载其中一个;查阅宏包文档确认兼容性

With bibliography (BibTeX)

阅读日志文件

pdflatex document.tex bibtex document pdflatex document.tex pdflatex document.tex
  • 查看
    .log
    文件获取完整错误上下文
  • 错误以
    !
    开头的行显示
  • 警告以
    LaTeX Warning:
    Package xyz Warning:
    开头
  • 出现
    Rerun to get cross-references right
    提示时,需再次编译文档

With bibliography (Biber/BibLaTeX)

14. 技巧与最佳实践

pdflatex document.tex biber document pdflatex document.tex pdflatex document.tex
  1. 多文件项目: 使用
    \input{chapters/intro}
    (直接插入内容)或
    \include{chapters/intro}
    (自动添加
    \clearpage
    ,支持
    \includeonly
    命令)
  2. 自定义导言区: 将宏包加载等设置移至
    preamble.sty
    preamble.tex
    ,然后通过
    \input{preamble}
    导入
  3. 标签位置:
    \caption{...}\label{fig:x}
    —— 标签必须放在标题之后,绝不能颠倒顺序
  4. 非断空格: 使用
    Figure~\ref{fig:x}
    Section~\ref{sec:y}
    —— 避免在标签编号前换行
  5. 草稿模式:
    \documentclass[draft]{article}
    —— 加快编译速度,显示超宽行标记
  6. 浮动体中用
    \centering
    而非
    center
    —— 避免额外垂直间距
  7. 避免用
    \\
    换行来分隔段落
    —— 改用空白行
  8. 使用
    booktabs
    宏包
    —— 专业表格避免使用
    \hline
    和竖线
  9. 加载
    microtype
    宏包
    —— 零成本提升排版质量
  10. 使用
    cleveref
    宏包
    ——
    \cref{fig:x}
    自动生成“图1”这类文本
  11. 版本控制: LaTeX是纯文本文件,推荐使用Git进行版本管理
  12. 每行一个句子 —— 便于在版本控制系统中查看差异

Automated (recommended)

15. 快速参考

最常用命令

latexmk -pdf document.tex # pdflatex latexmk -xelatex document.tex # xelatex latexmk -lualatex document.tex # lualatex latexmk -pdf -pvc document.tex # continuous preview
undefined
任务命令
粗体
\textbf{text}
斜体
\textit{text}
等宽字体
\texttt{text}
创建节
\section{Title}
交叉引用
\ref{label}
引用文献
\cite{key}
添加脚注
\footnote{text}
插入图片
\includegraphics[width=\textwidth]{file}
插入URL
\url{https://...}
\href{url}{text}
列表项
\item
新页面
\newpage
\clearpage
注释
% comment
行内数学公式
$E=mc^2$
行间数学公式
\[ E=mc^2 \]
分数
\frac{a}{b}

Common Errors and Fixes

数学运算符

ErrorCauseFix
Undefined control sequence
Typo or missing packageCheck spelling; add
\usepackage
Missing $ inserted
Math symbol outside math modeWrap in
$...$
Missing \begin{document}
Error in preambleCheck preamble for typos
File not found
Wrong path or missing fileCheck filename/path, check extension
Overfull \hbox
Line too wideRephrase, add
\allowbreak
, or use
\sloppy
Undefined citation
Missing bib entry or no bibtex runRun bibtex/biber, check .bib keys
Too many unprocessed floats
Too many figures queuedAdd
\clearpage
or use
[H]
Dimension too large
Image scaling issueCheck
width
/
height
values
Package clash
Two packages conflictLoad one, check docs for compatibility
命令符号命令符号
\sin
sin
\cos
cos
\tan
tan
\log
log
\ln
ln
\exp
exp
\min
min
\max
max
\lim
lim
\sum
Σ
\prod
Π
\int

Reading Log Files

箭头符号

  • Look at the
    .log
    file for full error context
  • Errors show as
    !
    lines
  • Warnings show as
    LaTeX Warning:
    or
    Package xyz Warning:
  • Rerun to get cross-references right
    → compile again
命令符号
\rightarrow
,
\to
\leftarrow
\leftrightarrow
\Rightarrow
\Leftarrow
\Leftrightarrow
\mapsto
\uparrow
\downarrow

14. Tips and Best Practices

参考资料

  1. Multi-file projects: Use
    \input{chapters/intro}
    (inserts inline) or
    \include{chapters/intro}
    (adds
    \clearpage
    , enables
    \includeonly
    )
  2. Custom preamble: Move package loading to
    preamble.sty
    or
    preamble.tex
    , then
    \input{preamble}
  3. Label after caption:
    \caption{...}\label{fig:x}
    — never the reverse
  4. Non-breaking spaces:
    Figure~\ref{fig:x}
    ,
    Section~\ref{sec:y}
    — prevents linebreak before number
  5. Draft mode:
    \documentclass[draft]{article}
    — speeds compilation, shows overfull boxes
  6. Use
    \centering
    not
    center
    inside floats (avoids extra vertical spacing)
  7. Avoid
    \\
    for paragraph breaks
    — use blank lines instead
  8. Use
    booktabs
    — never
    \hline
    and vertical lines for professional tables
  9. Use
    microtype
    — improves typography with zero effort
  10. Use
    cleveref
    \cref{fig:x}
    auto-generates "Figure 1" text
  11. Version control: LaTeX is plain text — use Git
  12. One sentence per line — makes diffs cleaner in version control

15. Quick Reference

Most Common Commands

TaskCommand
Bold
\textbf{text}
Italic
\textit{text}
Monospace
\texttt{text}
Section
\section{Title}
Reference
\ref{label}
Citation
\cite{key}
Footnote
\footnote{text}
Image
\includegraphics[width=\textwidth]{file}
URL
\url{https://...}
or
\href{url}{text}
List item
\item
New page
\newpage
or
\clearpage
Comment
% comment
Math inline
$E=mc^2$
Math display
\[ E=mc^2 \]
Fraction
\frac{a}{b}

Math Operators

CommandSymbolCommandSymbol
\sin
sin
\cos
cos
\tan
tan
\log
log
\ln
ln
\exp
exp
\min
min
\max
max
\lim
lim
\sum
Σ
\prod
Π
\int

Arrows

CommandSymbol
\rightarrow
,
\to
\leftarrow
\leftrightarrow
\Rightarrow
\Leftarrow
\Leftrightarrow
\mapsto
\uparrow
\downarrow

References