CERN Accelerating science

This website is no longer maintained. Its content may be obsolete. Please visit http://home.cern/ for current CERN information.



next up previous
Next: Defining New Environments Up: LaTeXeAn Overview Previous: Defining new commands

Defining commands

Commands are defined or redefined in LaTeX with:

\newcommandmycom[narg][default]%  
     command definition  [1mm]
\renewcommandmycom[narg][default]%  
     command definition  [1mm]
\providecommandmycom[narg][default]%  
     command definition

The first and second commands show enhancements with respect to LaTeX 2.09 by providing the possibility to have an optional argument when (re)defining a user command. The presence of such an optional parameter is flagged by the presence of the second optional argument default, which specifies the default value of that argument when it is not specified explicitly when the command is used. The last form is useful for general purpose files that are included in a document and over which the user does not always have control (eg. databases). If \mycom is not yet defined, the \providecommand will act as \newcommand and define it, otherwise the existing definition is kept.

The number of arguments, which includes the optional argument, is in the range 0\(\le\)\Larg{narg ( )9}. If the command has no arguments, then the [0] can be omitted. Inside the command definition part, the arguments are referenced as #1 to #\Larg{narg}, the optional argument, if present, being the first one.

For example, compare the following commands, with no, one mandatory, one optional, and one optional and one mandatory argument, allowing the user more freedom in each case.

\newcommand{\seq}{x_{0},\ldots\,x_{n}}
\newcommand{\seqm}[1]{#1_{0},\ldots\,#1_{n}}
\newcommand{\seqo}[1][k]{x_{0},\ldots\,x_{#1}}
\newcommand{\seqom}[2][k]{#2_{0},\ldots\,#2_{#1}}
$$\seq\quad\seqm{z}$$
$$\seqo\quad\seqo[l]$$
$$\seqom{y}\quad\seqom[i]{q}$$

If a command should work both in math and in text mode, special care should be taken in its definition. In LaTeXe you have the following command:

\ensuremath{math code}

As its name implies ensuremath ensures that its argument is always typeset in math mode by surrounding it if necessary with $ signs. For instance, the above can be rewritten as:

\renewcommand{\seq}{\ensuremath{x_{0},\ldots\,x_{n}}}
\renewcommand{\seqm}[1]{%
                  \ensuremath{#1_{0},\ldots\,#1_{n}}}
\seq,\quad\seqm{z} or $\seq,\quad\seqm{z}$



Janne Saarela
Wed May 17 14:38:58 METDST 1995