> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-cn/ming-ling/01-commands.md).

# 命令

## 引言

本文对……提供初步介绍 *命令插入边注*，它们是 LaTeX 排版能力的一个重要组成部分。大多数 LaTeX 命令都是由一个特殊字符（通常是 `\`，即反斜杠字符。让我们看一些示例：

```latex
\documentclass{article}
\begin{document}
在文档中，有不同类型的 \textbf{命令}
用于定义元素的显示方式。这
些命令可能会插入特殊元素：$\alpha \beta \Gamma$
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+commands+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIn+a+document+there+are+different+types+of+%5Ctextbf%7Bcommands%7D+%0Athat+define+the+way+the+elements+are+displayed.+This+%0Acommands+may+insert+special+elements%3A+%24%5Calpha+%5Cbeta+%5CGamma%24%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![使用 LaTeX 的基本文本格式化](/files/e561ae74f92cedaf9f163bcc9c0a1b53446bb49d)

在前面的示例中，有不同类型的命令；例如， `\textbf` 会将作为参数传入命令的文本加粗。在数学模式中，还有一些特殊命令，例如 `\alpha`, `\beta` 和 `\Gamma` 用于显示希腊字母。

## 命令

下面是一个使用命令创建项目符号列表的 LaTeX 代码示例：

```latex
\documentclass{article}
\begin{document}
列表示例：
\begin{itemize}
  \item[\S] 第一项
  \item 第二项
\end{itemize}
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+itemized+list+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AA+list+example%3A%0A%5Cbegin%7Bitemize%7D%0A++%5Citem%5B%5CS%5D+First+item%0A++%5Citem+Second+item%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![LaTeX 中排版的基本列表](/files/ad80ca739118839054ca8e750bc3867b0117b459)

命令 `\begin{itemize}` 开始一个 `itemize` *环境*——参见 [关于环境的文章](/latex/zh-cn/ming-ling/02-environments.md) 了解更多细节。环境声明下面是命令 `\item` 它告诉 LaTeX 开始一个新的列表项：为其格式化时，添加一个特殊标记（称为 bullet 的小黑点）并缩进该条目。

有些命令需要一个或多个 *参数*；例如， `\textbf` 命令（上面使用的），它接受一个参数——要以粗体显示的文本，写在花括号中，如下： `\textbf{把这段文字加粗}`.

还有 *可选的* 可以传递给命令以改变其行为的参数；这些可选参数必须放在方括号中： `[...]`。在上面的示例中，命令 `\item[\S]` 与……作用相同 `\item`，只是方括号中是 `\S`，它会用另一个字符替换条目文本前的小黑点。

## 定义新命令

尽管 LaTeX 自带大量命令，但为了简化工作、减少重复任务或执行某些复杂格式化操作，通常仍有必要定义自己的特殊命令。

### 简单命令

新命令通过以下方式定义： `\newcommand`；那么让我们看一个简单示例：

```latex
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\R}{\mathbb{R}}
实数集通常表示为
黑板粗体大写 R：\( \R \)。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Creating+a+simple+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%7B%5CR%7D%7B%5Cmathbb%7BR%7D%7D%0AThe+set+of+real+numbers+are+usually+represented+%0Aby+a+blackboard+bold+capital+R%3A+%5C%28+%5CR+%5C%29.%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![新命令的输出](/files/35f9a9c2472bf8c377988412631bdb428b0fab01)

该语句 `\newcommand{\R}{\mathbb{R}}` 有两个参数，用于定义一个新命令，其中：

* `\R`：是新命令的名称。
* `\mathbb{R}`：是新命令的功能。在这种情况下，字母 R 将以黑板粗体样式显示。使用 `\mathbb` 命令需要 `amssymb` 宏包，该宏包已添加到上面的示例中（在导言区）。

在定义该命令后，我们就可以在文本中使用它，如上所示。在这个示例中，新命令定义在使用它的段落之前；不过，将这类定义分散在文档各处并不是最佳实践。通常，新命令的定义会集中放在文档导言区，或者对于较大的集合，放在一个可被 [导入到主文档中的单独文件里](/latex/zh-cn/wen-dang-jie-gou/07-management-in-a-large-project.md#inputting-and-including-files).

### 带参数的命令

也可以创建接受一些参数的新命令；例如：

```latex
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\bb}[1]{\mathbb{#1}}
其他数制系统也有类似的表示法。
复数 \( \bb{C} \)，有理
数 \( \bb{Q} \) 以及整数 \( \bb{Z} \)。
\end{document}
```

[在 Overleaf 中打开此示例](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+command+with+1+parameter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%7B%5Cbb%7D%5B1%5D%7B%5Cmathbb%7B%231%7D%7D%0AOther+numerical+systems+have+similar+notations.+%0AThe+complex+numbers+%5C%28+%5Cbb%7BC%7D+%5C%29%2C+the+rational+%0Anumbers+%5C%28+%5Cbb%7BQ%7D+%5C%29+and+the+integer+numbers+%5C%28+%5Cbb%7BZ%7D+%5C%29.%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![带参数的 LaTeX 命令输出](/files/d3c97cfed892bd3461387585e29fd6a2664e7a51)

这一行 `\newcommand{\bb}[1]{\mathbb{#1}}` 定义了一个接受一个参数的新命令，其中：

* `\bb` 是新命令的名称。
* `[1]` 是新命令将接受的参数数量。
* `\mathbb{#1}` 是命令实际执行的操作——也就是它的定义。

在这种情况下，参数被引用为 `#1`，将以黑板粗体字符显示。如果一个命令需要多个参数，你可以使用 `#1`, `#2` 等等来引用每个参数。最多支持 9 个参数。

### 带可选参数的命令

用户自定义命令可以比目前展示的示例更加灵活：你可以定义接受 *可选的* 个参数的命令：

```latex
\documentclass{article}
\begin{document}

在编写许多带幂的表达式时，我们可以通过定义一个合适的命令来简化任务并节省时间：

\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}

我们可以这样使用：\[ \plusbinomial{x}{y} \]

甚至指数也可以更改：

\[ \plusbinomial[4]{a}{b} \]
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+command+with+optional+parameters\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%0AWhen+writing+many+expressions+with+exponents+we+can+simplify+our+task%2C+and+save+time%2C+by+defining+a+suitable+command%3A%0A%0A%5Cnewcommand%7B%5Cplusbinomial%7D%5B3%5D%5B2%5D%7B%28%232+%2B+%233%29%5E%231%7D%0A%0AWe+can+use+it+like+this%3A+%5C%5B+%5Cplusbinomial%7Bx%7D%7By%7D+%5C%5D%0A%0AAnd+even+the+exponent+can+be+changed%3A%0A%0A%5C%5B+%5Cplusbinomial%5B4%5D%7Ba%7D%7Bb%7D+%5C%5D%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![带可选参数的命令输出](/files/368a9ce2e69e24016d824e5987a087f0b2043b9a)

让我们分析这一行的语法 `\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}`:

* `\plusbinomial` 是新命令的名称。
* `[3]` 是该命令将接受的参数数量，在本例中为 3。
* `[2]` 是第一个参数的默认值。这就是第一个参数成为可选参数的原因；如果未传入，则使用此默认值。
* `(#2 + #3)^#1` 是命令的功能。在这种情况下，它会把第二和第三个参数以“二项式格式”放到由第一个参数表示的幂次上。

## 覆盖现有命令

如果你试图定义一个与某个 *已存在的* LaTeX 命令同名的新命令，编译将会失败并显示错误信息。下面的示例演示了这一点，它试图定义（已存在的） `\textbf` 命令设置为特定值：

```latex
\documentclass{article}
\newcommand{\textbf}[1]{#1}% 这将不起作用
\begin{document}

\section{这将失败}
\end{document}
```

[在 Overleaf 中打开这个（**会产生错误的**）示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Duplicate+command+name\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cnewcommand%7B%5Ctextbf%7D%5B1%5D%7B%231%7D%25+This+will+not+work%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%7BThis+will+fail%7D%0A%5Cend%7Bdocument%7D)

编译此示例会产生以下输出：

![由重复的 LaTeX 命令定义产生的错误](/files/5265a0e4168cdd537251b3819f4cc5b47677b51b)

如截图所示，我们尝试定义 `\textbf` 已失败：LaTeX 生成了错误信息 **LaTeX 错误：命令 \textbf 已经定义**.

如果你真的想覆盖一个现有命令，可以通过 `\renewcommand`来实现，它使用与 `\newcommand`:

```latex
\documentclass{article}
\usepackage{amssymb}
\begin{document}

\renewcommand{\S}{\mathbb{S}}

黎曼球面（复数加上 $\infty$）通常
有时用 \( \S \) 表示。
\end{document}
```

[在 Overleaf 中打开此示例。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Redefine+existing+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Crenewcommand%7B%5CS%7D%7B%5Cmathbb%7BS%7D%7D%0A%0AThe+Riemann+sphere+%28the+complex+numbers+plus+%24%5Cinfty%24%29+is+%0Asometimes+represented+by+%5C%28+%5CS+%5C%29.%0A%5Cend%7Bdocument%7D)

此示例生成以下输出：

![重新定义的 LaTeX 命令输出](/files/d76eeb05a3208095e29d704a34d0094e174916bd)

在这个示例中，命令 `\S` （参见 [命令插入边注](#commands) 部分中的示例）被覆盖为输出黑板粗体 S。

## 进一步阅读

更多信息请参见：

* [环境](/latex/zh-cn/ming-ling/02-environments.md)
* [理解宏包和类文件](/latex/zh-cn/lei-wen-jian/01-understanding-packages-and-class-files.md)
* [编写你自己的宏包](/latex/zh-cn/lei-wen-jian/03-writing-your-own-package.md)
* [编写你自己的类](/latex/zh-cn/lei-wen-jian/04-writing-your-own-class.md)
* [LaTeX 中的长度](/latex/zh-cn/ge-shi-hua/01-lengths-in-latex.md)
* [在 LaTeX 中使用颜色](/latex/zh-cn/ge-shi-hua/13-using-colors-in-latex.md)
* [页面大小和页边距](/latex/zh-cn/ge-shi-hua/07-page-size-and-margins.md)
* [宏包和类文件列表](/latex/zh-cn/lei-wen-jian/02-overleaf-and-tex-live.md)
* [LaTeX2ε 不那么简短的介绍](http://www.ctan.org/tex-archive/info/lshort/)
* [WikiBooks 上的 LaTeX/Creating\_Packages](http://en.wikibooks.org/wiki/LaTeX/Creating_Packages)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-cn/ming-ling/01-commands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
