\newcommand{\name}[no_of_parameters]{defn_of_command}
One can have a macro whose first argument is optional (note: when the first
argument is specified, it has to be enclosed in square brackets):
\newcommand{\name}[no_of_parameters][default_1st_variable]{defn_of_command}
When the macro is invoked (with all its parameters specified), it is replaced by its definition, after the parameters are substituted with the specified values.
If the number of parameters is zero, the argument
[no_of_parameters]
can be omitted, or used as
[0]
.
There can be at most 9 parameters. They are referred to in the definition
of command by #1
, #2
,..., #9
.
To redefine an already existing command, one has to use
\renewcommand
.
The command \providecommand
defines the command only if it
does not already exist; otherwise, it does not change its meaning.
Here are a few examples:
\newcommand{\R}{{\mathbb R}}
\renewcommand{\vector}[1]{(x_1,x_2,\ldots,x_{#1})}
\newcommand{\avector}[2]{(#1_1,#1_2,\ldots,#1_{#2})}
\newcommand{\aDEFvector}[2][a]{(#1_1,#1_2,\ldots,#1_{#2})}
Remarks:
\R
as index etc., it does not hurt
to put one extra pair of curly brackets around it (the outer pair is part
of the \newcommand
syntax, and will be stripped when the
replacement of \R
by its meaning takes place).
\vector
is already defined in LaTeX, and
using \newcommand{\vector}...
generates an error message.
Therefore we have to use \renewcommand
to redefine it.
and the approximate way it would look:\documentclass{article} \usepackage{amssymb,amsmath} \newcommand{\R}{{\mathbb R}} \renewcommand{\vector}[1]{(x_1,x_2,\ldots,x_{#1})} \newcommand{\avector}[2]{(#1_1,#1_2,\ldots,#1_{#2})} \newcommand{\aDEFvector}[2][a]{(#1_1,#1_2,\ldots,#1_{#2})} \begin{document} Consider the following vectors with entries in $\R$: $\mathbf x=\vector{6}$, $\mathbf u=\avector{u}{7}$, $\mathbf a=\aDEFvector{8}$, $\mathbf w=\aDEFvector[w]{n+1}$. \end{document}
Consider the following vectors with entries in R: x=(x1, x2, ... ,x6), u=(u1, u2, ... , u7), a=(a1, a2, ... , a8), w=(w1, w2, ... , wn+1).