Latex

目录

LaTeX 基本语法一览表

1. 文档结构

\documentclass{article}  % 文档类:article, report, book, beamer
\usepackage{amsmath}     % 数学包
\usepackage{graphicx}    % 图片包

\begin{document}
\title{标题}
\author{作者}
\date{\today}
\maketitle

\section{节标题}
\subsection{小节标题}

正文内容...

\end{document}

2. 文本格式

\textbf{粗体} \textit{斜体} \underline{下划线}
\emph{强调}  % 通常显示为斜体

{\small 小号字} {\Large 大号字} {\Huge 特大}

\begin{center} 居中文本 \end{center}
\begin{flushleft} 左对齐 \end{flushleft}
\begin{flushright} 右对齐 \end{flushright}

3. 数学模式

行内公式

$E = mc^2$\( a^2 + b^2 = c^2 \)

行间公式

\[ \sum_{i=1}^n i = \frac{n(n+1)}{2} \]

\begin{equation}
    \int_a^b f(x) dx = F(b) - F(a)
\end{equation}

\begin{align}
    x &= y + z \\
    a &= b \times c
\end{align}

常用数学符号

希腊字母:\alpha, \beta, \gamma, \Gamma, \Delta
运算符:\times, \div, \pm, \mp, \cdot
关系符:\leq, \geq, \neq, \approx, \equiv
集合:\in, \subset, \subseteq, \cup, \cap, \emptyset
箭头:\to, \rightarrow, \leftarrow, \Rightarrow, \Leftrightarrow
上下标:x^{2}, a_{i}, x^{a_{b}}
分数:\frac{a}{b}, \dfrac{a}{b}
根号:\sqrt{x}, \sqrt[n]{x}
求和/积分:\sum_{i=1}^n, \prod_{i=1}^n, \int_a^b, \oint
矩阵:
\begin{matrix} a & b \\ c & d \end{matrix}
\begin{pmatrix} a & b \\ c & d \end{pmatrix}
\begin{bmatrix} a & b \\ c & d \end{bmatrix}

4. 列表

无序列表:
\begin{itemize}
    \item 项目一
    \item 项目二
    \begin{itemize}
        \item 子项目
    \end{itemize}
\end{itemize}

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

描述列表:
\begin{description}
    \item[术语] 描述内容
    \item[另一个] 更多描述
\end{description}

5. 表格

\begin{tabular}{|c|c|c|}  % c:居中 l:左 r:右 |:竖线
    \hline
    列1 & 列2 & 列3 \\
    \hline
    数据1 & 数据2 & 数据3 \\
    数据4 & 数据5 & 数据6 \\
    \hline
\end{tabular}

% 更复杂的表格用 tabularx
\usepackage{tabularx}
\begin{tabularx}{\textwidth}{|X|X|X|}
    \hline
    自适应列 & 自适应列 & 自适应列 \\
    \hline
\end{tabularx}

6. 图片

\usepackage{graphicx}
\begin{figure}[htbp]  % h:这里 t:顶部 b:底部 p:单独页
    \centering
    \includegraphics[width=0.8\textwidth]{image.png}
    \caption{图片标题}
    \label{fig:my_label}
\end{figure}

7. 引用与交叉引用

\label{eq:myequation}  % 标记
\ref{eq:myequation}    % 引用编号
\pageref{eq:myequation} % 引用页码

引用文献:
\cite{key}             % 引用文献
\begin{thebibliography}{99}
    \bibitem{key} 作者. 标题. 出版社, 年份.
\end{thebibliography}

8. 定理与证明环境

\usepackage{amsthm}
\newtheorem{theorem}{定理}
\newtheorem{lemma}{引理}
\newtheorem{definition}{定义}

\begin{theorem}[勾股定理]
直角三角形斜边平方等于两直角边平方和。
\end{theorem}

\begin{proof}
证明内容...
\end{proof}

9. 代码环境

\usepackage{listings}
\begin{lstlisting}[language=Python]
def hello():
    print("Hello, LaTeX!")
\end{lstlisting}

10. 分栏与页面布局

% 双栏
\begin{multicols}{2}
    第一栏内容...
    \columnbreak
    第二栏内容...
\end{multicols}

% 页面设置
\usepackage{geometry}
\geometry{a4paper, margin=1in}  % 页边距

11. 颜色

\usepackage{xcolor}
\textcolor{red}{红色文字}
\colorbox{yellow}{黄色背景}
\definecolor{mycolor}{RGB}{255,0,0}

12. 超链接

\usepackage{hyperref}
\href{http://example.com}{链接文本}
\url{http://example.com}

13. 常用命令速查

% 注释:以 % 开头
\newcommand{\newcmd}{定义内容}  % 自定义命令
\renewcommand{\oldcmd}{新内容}  % 重定义命令

% 空格控制
\quad    % 1em空格
\qquad   % 2em空格
\,       % 小空格
\;       % 中空格
\!       % 负空格

% 特殊字符转义
\# \$ \% \& \{ \} \_ \~ \^ \textbackslash

14. 编译流程

# 基本编译
pdflatex document.tex

# 带参考文献
pdflatex document.tex
bibtex document
pdflatex document.tex
pdflatex document.tex

# 使用 latexmk 自动编译
latexmk -pdf document.tex

快速入门提示

  1. 所有命令以反斜杠 \ 开头
  2. 环境用 \begin{环境名} 和 \end{环境名}
  3. 可选参数用方括号 [],必需参数用花括号 {}
  4. 数学公式用 $...$ 或 [...]
  5. 注释用 %,注释掉整行或行尾