LaTeX中的命令

命令

原  文:Commands
译  者:Xovee
翻译时间:2023年4月3日

介绍

本文将介绍LaTeX中的命令(Commands),包括如何创建命令和修改已有的命令。命令是LaTeX系统中非常重要的一个部分,通常由一个特殊符号(一般是\)和单词组成。例如:

\documentclass{article}
\begin{document}
In a document there are different types of \textbf{commands} 
that define the way the elements are displayed. This 
commands may insert special elements: $\alpha \beta \Gamma$
\end{document}

这个例子的输出是:
在这里插入图片描述
在这个例子中,我们使用不同类型的命令。例如,\textbf的作用是加粗文字。在数学环境中,我们使用三个特殊的命令来展示希腊字母(\alpha\betaGamma)。

命令

下面是创建无序列表的命令:

\documentclass{article}
\begin{document}
A list example:
\begin{itemize}
  \item[\S] First item
  \item Second item
\end{itemize}
\end{document}

在这里插入图片描述
命令\begin{itemize}创建了itemize环境。在这个环境中,我们使用\item命令来创建列表项。

有些命令接受一个或者多个必选参数。例如,\textbf命令接收一个参数,即要加粗的文字内容:\textbf{make this bold}

有些时候,我们可以给命令指定一些可选参数,来更细致地控制命令的行为。可选命令通常位于方括号中。在上面的例子中,命令\item[\S]接收了一个可选参数\S,这让输出的列表标记从黑点变为了另外一个我们指定的字符。

定义新的命令

LaTeX自身有着非常多的内置命令,但有些时候我们也需要创建一些新的命令来满足我们的个性化需求。定义新的命令可以帮助我们减少重复性工作,或者执行一些复杂的任务。

简单的命令

我们使用\newcommand命令来定义新的命令,它的使用非常简单:

扫描二维码关注公众号,回复: 16432718 查看本文章
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\R}{\mathbb{R}}
The set of real numbers are usually represented 
by a blackboard bold capital R: \( \R \).
\end{document}

这个例子的输出是:
在这里插入图片描述
代码\newcommand{\R}{\mathbb{R}}有两个参数:

  • 第一个参数\R是新定义的命令的名字
  • 第二个参数\mathbb{R}定义了新命令的作用。在这个例子中,字母R会被输出为数学环境中的粗体。\mathbb命令的使用需要我们提前导入asmsymb包。

在定义新命令之后,我们就可以使用它了。但是,在一个较大的文档中,随处定义命令并不是一个好的习惯,一般来说,我们在文档的开头部分(preamble)或在一个独立的文件中集中定义命令。

定义接收参数的命令

我们还可以定义接收参数的新命令。例如:

\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\bb}[1]{\mathbb{#1}}
Other numerical systems have similar notations. 
The complex numbers \( \bb{C} \), the rational 
numbers \( \bb{Q} \) and the integer numbers \( \bb{Z} \).
\end{document}

输出:
在这里插入图片描述
代码\newcommand{\bb}[1]{\mathbb{#1}}定义了一个接收单个参数的新命令:

  • \bb是新命令的名字
  • [1]是新命令所接收参数的数量
  • \mathbb{#1}是新命令的行为

在这个例子中,我们使用#1来代表参数的具体内容,即#1会被展示为数学环境下的黑板字体(blackboard)。如果一个命令需要多个参数,你可以使用#1#2等来引用每个参数。命令最多支持九个参数。

定义接收可选参数的命令

我们还可以定义接收可选参数的命令:

\documentclass{article}
\begin{document}

When writing many expressions with exponents we can simplify our task, and save time, by defining a suitable command:

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

We can use it like this: \[ \plusbinomial{x}{y} \]

And even the exponent can be changed:

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

这个例子的输出是:
在这里插入图片描述
接下来我们分析代码\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}的作用:

  • \plusbinomial是新命令的名字
  • [3]是命令所接收参数的数量,即接收三个参数
  • [2]是第一个参数的默认值。这使第一个参数变成一个可选参数,如果用户没有指定第一个参数的具体值,命令就会使用2作为第一个参数的值。
  • (#2 + #3)^#1是命令的具体功能。在这个例子中,它的作用是一个二项式公式,参数2和参数3相加,然后计算和的#1次方。

修改已有的命令

如果你定义的新命令的名字与已有的命令重名,那么LaTeX编译将会出错。例如:

\documentclass{article}
\newcommand{\textbf}[1]{#1}% This will not work
\begin{document}

\section{This will fail}
\end{document}

在这里插入图片描述
如上图所示,我们试图定义\textbf的行为失败了,LaTeX输出了一个错误消息:LaTeX Error: Command \textbf already defined

如果我们想去修改一个已有的命令,我们可以使用\renewcommand。这个命令的语法与newcommand相同:

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

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

The Riemann sphere (the complex numbers plus $\infty$) is 
sometimes represented by \( \S \).
\end{document}

在这里插入图片描述
在这个例子中,命令\S被修改为输出一个数学环境下的黑板字体S。

猜你喜欢

转载自blog.csdn.net/xovee/article/details/129922866