> 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-tw/zhi-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/98c45a2d8c1075adfcb09c0cc1aaac967ac71de0)

在前一個範例中，有不同類型的指令；例如， `\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/2ccf34ee26885c8e14a0cc73344222ca16feb46b)

指令 `\begin{itemize}` 開始一個 `itemize` *環境*——請參見 [關於環境的文章](/latex/zh-tw/zhi-ling/02-environments.md) 以了解更多細節。環境宣告下方是指令 `\item` 它告訴 LaTeX 開始一個新的清單項目：為了格式化它，加入一個特殊標記（稱為 bullet 的小黑點）並縮排該項目。

有些指令需要一個或多個 *參數*；例如， `\textbf` 指令（上面已使用），它接受一個參數——要以粗體顯示的文字，寫在大括號內，如下所示： `\textbf{make this bold}`.

此外還有 *選用的* 參數，可以傳遞給指令以改變其行為；這些選用參數必須放在方括號中： `[...]`。在上面的範例中，指令 `\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/723ea03708e40aff8cdb2c6c571718a4c212cc57)

敘述 `\newcommand{\R}{\mathbb{R}}` 有兩個參數，用來定義一個新指令，其中：

* `\R`：是新指令的名稱。
* `\mathbb{R}`：是新指令的作用。在這個例子中，字母 R 會以黑板粗體樣式書寫。使用 `\mathbb` 指令需要 `amssymb` 套件，已在上面的範例中加入（於前言區）。

在定義好指令之後，我們就可以在文字中使用它，如上所示。在這個例子中，新指令是在使用它的段落前立即定義的；然而，將這類定義零散地放在文件各處並不是最佳做法。一般而言，新指令的定義會集中放在文件前言區，或者對於較大的集合，放入可被 [匯入到你的主文件中](/latex/zh-tw/wen-jian-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/ba950e6cc83e8bbe74b9adc2c3e8e6adb6268b4f)

這一行 `\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/ef0242dc4dd0c92211d9d54eb576bb99f710ee26)

讓我們分析這一行的語法： `\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/b4bc37ead4f2674efb1dd4fd361733acb4b09c48)

如螢幕截圖所示，我們嘗試定義 `\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/2f1c94982094409e6da92f949e665f3a4815fb8a)

在這個範例中，指令 `\S` （請參見 [命令](#commands) 章節中的範例）被覆寫為輸出黑板粗體 S。

## 延伸閱讀

更多資訊請參見：

* [環境](/latex/zh-tw/zhi-ling/02-environments.md)
* [了解套件與類別檔案](/latex/zh-tw/lei-bie-dang/01-understanding-packages-and-class-files.md)
* [撰寫你自己的套件](/latex/zh-tw/lei-bie-dang/03-writing-your-own-package.md)
* [撰寫你自己的類別](/latex/zh-tw/lei-bie-dang/04-writing-your-own-class.md)
* [LaTeX 中的長度](/latex/zh-tw/ge-shi-hua/01-lengths-in-latex.md)
* [在 LaTeX 中使用顏色](/latex/zh-tw/ge-shi-hua/13-using-colors-in-latex.md)
* [頁面大小與邊界](/latex/zh-tw/ge-shi-hua/07-page-size-and-margins.md)
* [套件與類別檔案清單](/latex/zh-tw/lei-bie-dang/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-tw/zhi-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.
