Latex Tips

Latex Tips

Difference between \providecommand and \newcommand in LaTeX

These two commands are similar but have one important difference:

Example usage:

\newcommand{\mycommand}{This is my command}
% If \mycommand is already defined, this will give an error

\providecommand{\myothercommand}{This is my other command}
% If \myothercommand is already defined, this will be ignored silently

When to use which:

The \providecommand command is particularly useful in package development to offer default definitions that can be overridden by the user.

Here are some tags that could be generated from the note [[Latex Tips]]: