关于 latex
latex 是介绍 LaTeX 文档结构、排版、数学公式、引用和常用宏包的技能,面向撰写学术、技术等结构化文档的人士,也涵盖演示文稿和简历。
主要功能
- 01文档结构与文本排版
- 02数学符号与公式
- 03章节、列表和交叉引用
- 04表格、插图与文献引用
- 05Beamer 演示文稿与页面布局
适用场景
- 01撰写学术论文或学位论文
- 02排版技术文档或报告
- 03制作 Beamer 演示文稿
- 04编排简历或正式信函
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ igbuend/ grimbard --skill latex安装后在 Claude Code 中输入/latex确认已加载。
详细介绍
LaTeX 技能
1. 概述
LaTeX 是一种标记语言和排版系统,用于制作高质量文档。它擅长处理结构化文档、复杂格式、数学内容、交叉引用和参考文献。
LaTeX 的适用场景:
- 学术论文、期刊投稿
- 学位论文和毕业论文
- 技术文档
- CV 和简历
- 演示文稿(Beamer)
- 书籍、报告、信件
关键概念:
| 概念 | 描述 |
|---|---|
| 导言区 | \begin{document} 之前的所有内容——文档类、宏包、设置 |
| 文档正文 | \begin{document} 和 \end{document} 之间的内容 |
| 命令 | 以 \ 开头,例如 \textbf{bold}。可选参数放在 [] 中,必需参数放在 {} 中 |
| 环境 | 用于限定格式范围的 \begin{name}...\end{name} 代码块 |
| 宏包 | 在导言区通过 \usepackage{name} 加载的扩展 |
2. 文档结构
文档类
\documentclass[options]{class}
| 文档类 | 用途 |
|---|---|
article | 短文档、论文、作业 |
report | 包含章节的较长文档 |
book | 书籍(前置/后置部分、章节) |
beamer | 演示文稿/幻灯片 |
letter | 正式信件 |
memoir | 灵活——可替代 article/report/book |
常见文档类选项
\documentclass[12pt,a4paper,twocolumn,draft]{article}
| 选项 | 值 |
|---|---|
| 字号 | 10pt、11pt、12pt |
| 纸张 | a4paper、letterpaper、a5paper |
| 分栏 | onecolumn、twocolumn |
| 页数 | oneside、twoside |
| 草稿 | draft(显示溢出边界的文本框,并跳过图片) |
最简文档
\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}
3. 文本格式
样式命令
| 命令 | 效果 |
|---|---|
\textbf{text} | 粗体 |
\textit{text} | 斜体 |
\underline{text} | 下划线 |
\emph{text} | 强调(斜体;若当前已为斜体,则改为正体) |
\texttt{text} | 等宽字体 |
\textsc{text} | 小型大写字母 |
\textsf{text} | 无衬线字体 |
字号
从小到大:
\tiny \scriptsize \footnotesize \small \normalsize
\large \Large \LARGE \huge \Huge
用法:{\large This text is large.},也可用作环境。
字体系列
| 命令 | 声明 | 字体系列 |
|---|---|---|
\textrm{} | \rmfamily | 衬线体(罗马体) |
\textsf{} | \sffamily | 无衬线体 |
\texttt{} | \ttfamily | 等宽字体 |
对齐方式
\begin{center} Centered text. \end{center}
\begin{flushleft} Left-aligned. \end{flushleft}
\begin{flushright} Right-aligned. \end{flushright}
也可内联使用:\centering、\raggedright、\raggedleft
间距
\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
行间距(需要 setspace 宏包):
\usepackage{setspace}
\onehalfspacing % or \doublespacing, \singlespacing
特殊字符
以下字符必须转义:
| 字符 | LaTeX | 字符 | LaTeX |
|---|---|---|---|
% | \% | $ | \$ |
& | \& | # | \# |
_ | \_ | { | \{ |
} | \} | ~ | \textasciitilde |
^ | \textasciicircum | \ | \textbackslash |
4. 文档组织
分节
\part{Part Title} % only in report/book
\chapter{Chapter Title} % only in report/book
\section{Section}
\subsection{Subsection}
\subsubsection{Subsubsection}
\paragraph{Paragraph}
\paragraphsub{Subparagraph}
带星号的版本(\section*{})会取消编号和目录条目。
目录
\tableofcontents % requires two compilations
\listoffigures
\listoftables
列表
% 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}
使用 enumitem 宏包进行自定义:
\usepackage{enumitem}
\begin{enumerate}[label=(\alph*), start=1]
交叉引用
\section{Methods}\label{sec:methods}
See Section~\ref{sec:methods} on page~\pageref{sec:methods}.
使用 hyperref 时,可用 \autoref{sec:methods} 自动生成“Section 2”文本。
规则: 始终将 \label 放在 \caption 之后(浮动体中),或放在分节命令之后。
脚注
This has a footnote.\footnote{Footnote text here.}
5. 数学
行内公式与独立公式
Inline: $E = mc^2$ or \(E = mc^2\)
Display:
\[ E = mc^2 \]
% Numbered equation:
\begin{equation}\label{eq:einstein}
E = mc^2
\end{equation}
必备宏包
\usepackage{amsmath} % align, cases, matrices, etc.
\usepackage{amssymb} % extra symbols (ℝ, ℤ, etc.)
\usepackage{mathtools} % extends amsmath (dcases, coloneqq, etc.)
常见结构
% 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\}
矩阵
\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}
对齐公式
\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*}
分段函数
f(x) = \begin{cases}
x^2 & \text{if } x \geq 0 \\
-x^2 & \text{if } x < 0
\end{cases}
希腊字母
| 小写 | 命令 | 大写 | 命令 |
|---|---|---|---|
| α | \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 |
常用数学符号
| 符号 | 命令 | 符号 | 命令 |
|---|---|---|---|
| ≤ | \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. 表格
基本表格
\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}
列格式说明符
| 格式 | 对齐方式 |
|---|---|
l | 左对齐 |
c | 居中对齐 |
r | 右对齐 |
p{width} | 段落(顶端对齐,固定宽度) |
m{width} | 段落居中对齐(需要 array) |
| ` | ` |
常用宏包
\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
多列/多行
\multicolumn{3}{c}{Spanning header} \\
\multirow{2}{*}{Tall cell}
7. 图形与图片
\usepackage{graphicx}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\textwidth]{images/photo.png}
\caption{A descriptive caption.}\label{fig:photo}
\end{figure}
includegraphics 选项
| 选项 | 示例 |
|---|---|
width | width=0.5\textwidth |
height | height=5cm |
scale | scale=0.75 |
angle | angle=90 |
trim | trim=1cm 2cm 1cm 2cm, clip(左、下、右、上) |
浮动体位置
| 说明符 | 含义 |
|---|---|
h | 此处(大致位置) |
t | 页面顶部 |
b | 页面底部 |
p | 专用浮动体页面 |
! | 覆盖内部限制 |
H | 就在此处(需要 float 宏包) |
子图
\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}
8. 参考文献与引用
BibTeX 工作流
- 创建
references.bib:
@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}
}
- 在文档中:
\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}
- 编译:
pdflatex → bibtex → pdflatex → pdflatex
BibLaTeX 工作流(现代)
\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 与 biblatex 对比
| 特性 | natbib | biblatex |
|---|---|---|
| 后端 | BibTeX | Biber(推荐) |
| 灵活性 | 样式有限 | 高度可自定义 |
| Unicode | 支持较差 | 完全支持 |
| 建议 | 旧项目 | 新项目 |
9. 代码列表
verbatim
\begin{verbatim}
def hello():
print("Hello, world!")
\end{verbatim}
Inline: \verb|some_code()|
listings 宏包
\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}
minted 宏包(效果更精美,需要 Pygments)
\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. 演示文稿(Beamer)
\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}
常用主题: Madrid、Berlin、Metropolis(现代、简洁)、Warsaw、CambridgeUS
覆盖命令
| 命令 | 效果 |
|---|---|
\pause | 逐步显示内容 |
\onslide<2>{text} | 仅在第 2 张幻灯片上显示 |
\only<1>{text} | 仅在第 1 张幻灯片上显示(不预留空间) |
\visible<2->{text} | 从第 2 张幻灯片开始显示 |
\alert<2>{text} | 在第 2 张幻灯片上突出显示 |
11. 页面布局
页边距(geometry)
\usepackage[margin=2.5cm]{geometry}
% or:
\usepackage[top=3cm, bottom=3cm, left=2cm, right=2cm]{geometry}
页眉和页脚(fancyhdr)
\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}
页码
\pagenumbering{roman} % i, ii, iii (for front matter)
\pagenumbering{arabic} % 1, 2, 3 (for main content)
\thispagestyle{empty} % no number on this page
多栏
\usepackage{multicol}
\begin{multicols}{2}
Content in two columns...
\end{multicols}
12. 常用宏包参考
| 宏包 | 用途 |
|---|---|
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. 编译
引擎
| 引擎 | 使用场景 |
|---|---|
pdflatex | 默认。兼容性最佳。适用于 ASCII/Latin 输入。 |
xelatex | 系统字体(TTF/OTF)、完整 Unicode 支持 |
lualatex | 系统字体 + Lua 脚本、完整 Unicode 支持 |
编译顺序
# Basic document
pdflatex document.tex
# With bibliography (BibTeX)
pdflatex document.tex
bibtex document
pdflatex document.tex
pdflatex document.tex
# With bibliography (Biber/BibLaTeX)
pdflatex document.tex
biber document
pdflatex document.tex
pdflatex document.tex
# Automated (recommended)
latexmk -pdf document.tex # pdflatex
latexmk -xelatex document.tex # xelatex
latexmk -lualatex document.tex # lualatex
latexmk -pdf -pvc document.tex # continuous preview
常见错误及修复方法
| 错误 | 原因 | 修复方法 |
|---|---|---|
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 | 两个宏包冲突 | 仅加载其中一个,并查看文档了解兼容性 |
阅读日志文件
- 查看
.log文件,了解完整错误上下文 - 错误会显示为
!行 - 警告会显示为
LaTeX Warning:或Package xyz Warning: Rerun to get cross-references right→ 再次编译
14. 提示和最佳实践
- **多文件项目:**使用
\input{chapters/intro}(内联插入)或\include{chapters/intro}(添加\clearpage,并支持\includeonly) - **自定义导言区:**将宏包加载移至
preamble.sty或preamble.tex,然后使用\input{preamble} - 标题后添加标签:
\caption{...}\label{fig:x}— 切勿反过来 - 不换行空格:
Figure~\ref{fig:x}、Section~\ref{sec:y}— 防止编号前换行 - 草稿模式:
\documentclass[draft]{article}— 加快编译速度,并显示溢出的文本框 - 在浮动体中使用
\centering,而非center(避免额外的垂直间距) - 避免用
\\分段 — 改用空行 - 使用
booktabs— 专业表格中不要使用\hline和竖线 - 使用
microtype— 无需额外操作即可改善排版 - 使用
cleveref—\cref{fig:x}会自动生成“Figure 1”文本 - **版本控制:**LaTeX 是纯文本 — 使用 Git
- 每行一句话 — 让版本控制中的差异更清晰
15. 快速参考
最常用的命令
| 任务 | 命令 |
|---|---|
| 粗体 | \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} |
数学运算符
| 命令 | 符号 | 命令 | 符号 |
|---|---|---|---|
\sin | sin | \cos | cos |
\tan | tan | \log | log |
\ln | ln | \exp | exp |
\min | min | \max | max |
\lim | lim | \sum | Σ |
\prod | Π | \int | ∫ |
箭头
| 命令 | 符号 |
|---|---|
\rightarrow、\to | → |
\leftarrow | ← |
\leftrightarrow | ↔ |
\Rightarrow | ⇒ |
\Leftarrow | ⇐ |
\Leftrightarrow | ⇔ |
\mapsto | ↦ |
\uparrow | ↑ |
\downarrow | ↓ |
参考资料
评论
支持 Markdown