> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-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 Second 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}% This will not work
\begin{document}

\section{This will fail}
\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://ayakaleaf-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.
