# LaTeX Macros

One can create new "commands" for combinations that are used often.
These are called macros. The format is as follows (the brackets are essential):
 \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:

Remarks:
• Since we might use \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).
• WARNING: redefining commands should be done with great care: since a given command might be used in various LaTeX constructions, the effect can exceed our expectations.

Here is a piece of LaTeX code using them:

\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})}
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}$.