\newcommand \renewcommand

\newcommand{cmd}[args][opt]{def}
\renewcommand{cmd}[args][opt]{def}
\providecommand{cmd}[args][opt]{def} -- LaTeX2e

These commands define (or redefine) a command.

Examples

\newcommand{\water}{H$_2$O}

This would allow one to write, e.g.,

  The formula for water is \water. 
or
  \water\ is the formula for water.
Note, in the second case, the trailing \ followed by a blank is required to ensure a blank space after the H2O; LaTeX ignores the blank following a command, so the space has to be specifically inserted with the \<space>.

As a second example consider

\newcommand{\hypotenuse}{$a^{2}+b^{2}$}

Note that this will produce the desired formula in text (paragraph) mode because of the $...$ in the definition. In math mode, however, the first $ in the definition will cause LaTeX to leave math mode, causing problems.

In LaTeX 2.09 a standard trick for getting around this is to put the math-mode expression in an \mbox, viz.,

  \newcommand{\hypotenuse}{\mbox{$a^{2}+b^{2}$}}

In LaTeX2e the \ensuremath command has been provided to alleviate this problem. The argument of the \ensuremath command is always processed in math mode, regardless of the current mode. Using this mechanism the above could be written as

  \newcommand{\hypotenuse}{\ensuremath{a^{2}+y^{2}}}

See also Commands
Go to LaTeX Table of Contents

Revised: Sheldon Green, 23 Jun 1995.