用 \newcommand 定義自己的指令
在導言區寫下 \newcommand{\name}{replacement},用 [n] 指定參數 #1 到 #n;之後 \name 就會被它的定義取代,在文字和數學中都一樣。
沒有參數的指令
指令可以少打很多字,也能讓符號寫法保持一致。在導言區定義一次,就能在任何地方使用。
集合和產品名稱的捷徑
\documentclass{article}
\usepackage{amssymb}
\newcommand{\R}{\mathbb{R}}
\newcommand{\product}{Scissor LaTeX}
\begin{document}
Every $x \in \R$ has a square $x^2 \ge 0$.
This page was typeset by \product.
\end{document}參數和預設值
名稱後面的 [n] 表示指令接收幾個參數;在定義中,它們分別是 #1、#2,以此類推。第二個方括號為第一個參數提供預設值,讓它變成選用參數。
參數和選用的第一個參數
\documentclass{article}
\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\norm}[1]{\left\lVert #1 \right\rVert}
\newcommand{\greet}[2][Hello]{#1, #2!}
\begin{document}
The length of $\vect{v}$ is $\norm{\vect{v}}$.
\greet{Erika} \greet[Good morning]{John}
\end{document}修改既有的指令
\renewcommand 會取代已經存在的指令,\providecommand 則只在指令還不存在時才定義它。在 TeX 安裝環境中,對已被使用的名稱寫 \newcommand 會產生錯誤;這時請改用 \renewcommand。
\renewcommand 和 \providecommand
\documentclass{article}
\renewcommand{\emph}[1]{\textbf{#1}}
\providecommand{\R}{\mathbb{R}}
\begin{document}
This is \emph{emphasised} in bold now, and $\R$ is defined.
\end{document}運算子名稱
sin 和 log 這類函數名稱會以直立字體排版,並帶有正確的間距。\DeclareMathOperator(amsmath)可以建立你自己的運算子;加星號的形式會在獨立式中把上下限放在下方,就像 \max 一樣。
\DeclareMathOperator 和它的星號形式
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\Var}{Var}
\DeclareMathOperator*{\argmax}{arg\,max}
\begin{document}
\[
\Var(X) = \mathbb{E}\left[(X - \mu)^2\right],
\qquad
\hat{\theta} = \argmax_{\theta} L(\theta)
\]
\end{document}自己的環境
\newenvironment{name}[n]{before}{after} 會定義一個環境:「before」程式碼在 \begin{name} 時執行,「after」程式碼在 \end{name} 時執行。參數可以在「before」部分中使用。
帶有參數的環境
\documentclass{article}
\newenvironment{task}[1]
{\par\noindent\textbf{Task #1.}\ }
{\par}
\begin{document}
\begin{task}{44}
Measure the rainfall every morning.
\end{task}
\end{document}支援情況
| 指令 | 在 Scissor LaTeX 中 | 說明 |
|---|---|---|
\newcommand、\renewcommand、\providecommand | 可用 | 最多 9 個參數,第一個參數可以是帶預設值的選用參數。星號形式也可用。 |
\DeclareRobustCommand | 可用 | 與 \newcommand 相同。 |
\newenvironment、\renewenvironment | 可用 | 支援參數和預設值。 |
\DeclareMathOperator、\DeclareMathOperator* | 可用 | |
\def\name{...}、\gdef、\edef、\xdef | 部分可用 | 簡單的定義和 #1 形式的參數可以使用;\def\pt(#1,#2){...} 這類帶分隔符的參數無法比對,會產生錯亂的輸出。 |
\let\new\old | 可用 | |
\NewDocumentCommand、\RenewDocumentCommand(xparse) | 不支援 | 定義會被捨棄,使用該指令的地方會被回報為未知指令。 |
\DeclarePairedDelimiter (mathtools) | 可用 | |
\makeatletter、\makeatother | 接受 | 名稱中含有 @ 的指令不會執行。 |
定義會在排版之前展開,所以指令可以用在文字、數學、圖表標題和章節標題中。為了避免無窮迴圈,展開到達安全上限時就會停止,並在「問題」分頁中顯示訊息。
xparse 指令
xparse 的 \NewDocumentCommand 不會被讀取。定義會被捨棄;在使用該指令的地方,它的名稱會被略過並顯示訊息,參數則留在文字中。
不支援 \NewDocumentCommand
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\pair}{m m}{(#1, #2)}
\begin{document}
The pair \pair{a}{b} loses its parentheses and comma.
\end{document}「問題」分頁: Skipped 1 unknown command: \pair
要修正這個問題,請用 \newcommand{\pair}[2]{(#1, #2)} 寫出同樣的指令。至於選用參數,帶預設值的 \newcommand 就能涵蓋常見的情況。
與 TeX 安裝環境的差異
xparse 和 mathtools 的使用手冊位於 CTAN:xparse、mathtools。關於運算子和函數名稱,另請參見總和、積分和函數名稱。
常見問題
怎麼在 LaTeX 中定義新指令?
在導言區寫下 \newcommand{\name}{text}。如果要兩個參數,就在名稱後面加上 [2],並在文字中使用 #1 和 #2。
\newcommand 和 \def 有什麼不同?
\newcommand 會檢查名稱是否尚未被使用,並支援選用參數。\def 是底層的 TeX 指令,會直接覆寫而不先詢問。建議使用 \newcommand 和 \renewcommand。
\NewDocumentCommand 在 Scissor LaTeX 中能用嗎?
不能。請用 \newcommand 改寫這類指令,它可以處理必要參數和一個選用參數。
本頁的每個範例都在建置網站時由 Scissor LaTeX 排版。 最後更新:2026-11-07