\newcommand
,
\renewcommand
and
\providecommand
all have the same syntax. I'll try to explain them. This is the purpose of the commands:
\newcommand
: create new command. Issue error if command already exists.
\renewcommand
: change definition of existing command. Issue error if the command deosn't exist yet
\providecommand
: create new command only if it doesn't exist. Do nothing if it already exists.
I'll pick
\newcommand
to explain the syntax:
Code: Select all
\newcommand*{csname}[number of args][value of optional arg]{definition}
The star and the
args arguments are optional, so the basic use is:
Code: Select all
\newcommand\mycommand{do something cool}
(a command without argument),
Code: Select all
\newcommand\mycommand[1]{do something cool with #1}
(a command with one mandatory argument to be used like
\mycommand{stuff}
),
Code: Select all
\newcommand\mycommand[1][stuff]{do something cool with #1}
(a command with an optional argument to be used like
\mycommand[other stuff]
or
\mycommand[stuff]
) or
Code: Select all
\newcommand\mycommand[2][stuff]{do something cool with #1 and #2}
(a command with one mandatory argument an an optional argument to be used like
\mycommand{further stuff}
or
\mycommand[otherstuff]{further stuff}
).
The
[number of args]
can anything between
1
and
9
.
The star will make arguments of
\mycommand
short (as opposed to
long), i.e., they may
not contain
\par
, neither explicitly nor implicitly. This is useful for debugging. Suppose you forgot to add the closing brace. If the command is short TeX will stop looking for it at the next occurrence of
\par
otherwise at the end of the file. The log entry may be more useful in the short version, so it is generally a good idea to use the starred version unless you need to be able to insert more than one paragraph as argument. The argument of
\textbf{}
, for example, is long.