# Defining commands

In LaTeX, you can define a new command, override an existing command or, in LaTeX2e only, provide a new command if it doesn’t already exist, using the following commands:

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

The arguments of these are as follows:

cmd
args
An integer from 1 to 9 denoting the number of arguments of the command being defined. The default is for the command to have no arguments.
opt (LaTeX2e only)
If present, then the first of the number of arguments specified by args is optional with a default value of opt; if absent, then all of the arguments are required.
def
The text to be substituted for every occurrence of cmd; a parameter of the form #n in cmd is replaced by the text of the nth argument when this substitution takes place.

## Example

% Define a new command:
\newcommand{\super}{supercalifragilisticexpialidocious}

Renewing the command overwrites the behaviour of a previously defined command. If you attempt to renew a command that hasn’t been defined yet, you will get an error.

% Redefine the command:
\renewcommand{\super}{superman}

If you provide a command, the command is created if it doesn’t already exist. If the command does already exist, there will be no effect. The original command’s behaviour will not be affected.

% Define a command only if it's missing:
\providecommand{\super}{supercalifragilisticexpialidocious}

Here’s the full document and its output:

\documentclass[a4paper,12pt]{article}

\begin{document}

\newcommand{\super}{supercalifragilisticexpialidocious}
\noindent\super

\renewcommand{\super}{superman}
\noindent\super

\providecommand{\super}{supercalifragilisticexpialidocious}
\noindent\super

\end{document}

This site uses Akismet to reduce spam. Learn how your comment data is processed.