mcpField
L
latex

latex

github.com/igbuend文档处理964 次安装33 周前更新
代码仓库

关于 latex

latex 是介绍 LaTeX 文档结构、排版、数学公式、引用和常用宏包的技能,面向撰写学术、技术等结构化文档的人士,也涵盖演示文稿和简历。

主要功能

  1. 01文档结构与文本排版
  2. 02数学符号与公式
  3. 03章节、列表和交叉引用
  4. 04表格、插图与文献引用
  5. 05Beamer 演示文稿与页面布局

适用场景

  1. 01撰写学术论文或学位论文
  2. 02排版技术文档或报告
  3. 03制作 Beamer 演示文稿
  4. 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 选项
选项示例
widthwidth=0.5\textwidth
heightheight=5cm
scalescale=0.75
angleangle=90
trimtrim=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 工作流
  1. 创建 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}
}
  1. 在文档中:
\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. 编译: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 对比
特性natbibbiblatex
后端BibTeXBiber(推荐)
灵活性样式有限高度可自定义
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)
siunitxSI 单位和数字格式
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. 提示和最佳实践

  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} 会自动生成“Figure 1”文本
  11. **版本控制:**LaTeX 是纯文本 — 使用 Git
  12. 每行一句话 — 让版本控制中的差异更清晰

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}
数学运算符
命令符号命令符号
\sinsin\coscos
\tantan\loglog
\lnln\expexp
\minmin\maxmax
\limlim\sumΣ
\prodΠ\int∫
箭头
命令符号
\rightarrow、\to→
\leftarrow←
\leftrightarrow↔
\Rightarrow⇒
\Leftarrow⇐
\Leftrightarrow⇔
\mapsto↦
\uparrow↑
\downarrow↓

参考资料

评论

支持 Markdown