为什么在这里使用 \renewcommand 时会出现错误?

为什么在这里使用 \renewcommand 时会出现错误?

我尝试输入\renewcommand{\mr}{\mathrm},这样我就可以\mr在我的文本中使用 而不是\mathrm。我收到错误\mr Undefined。发生了什么事?

答案1

TeX 使用宏替换作为其基本概念。当你说

\newcommand{\foo}{<whatever>}

你指示程序用 替换每次出现的\foo<whatever>为了使它\newcommand工作,命令在执行\foo时必须是未定义的。\newcommand

因此

\newcommand{\mr}{\mathrm}

您基本上是在为创建一个别名:在输入流中\mathrm找到后,TeX 将用替换,然后用其定义替换,依此类推,直到只剩下“纯可执行”命令,TeX 会将其传递到通常称为“胃”的地方进行进一步处理。\mr\mr\mathrm\mathrm

如果命令\foo已经有定义,则需要使用\renewcommand(但要始终害怕使用它)。

第二个参数\newcommand不必只是一个标记:你也可以定义

\newcommand{\independence}{When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.}

一旦找到\independence,TeX 就会用该文本替换它。

您可以在第八章中找到更多信息“适合完全新手的 LaTeX”

相关内容