You can use \let
to copy the original command and than you can redefine it:
\let \originalfrac=\frac
\renewcommand{\frac}[2]{\originalfrac{\,#1\,}{\,#2\,}}
EDIT: better:
\let \originalfrac=\frac
\DeclareRobustCommand{\myfrac}[2]{\originalfrac{\,#1\,}{\,#2\,}}
\let \frac=\myfrac
This differs from the first approach in 2 ways:
- It defines a newcommand instead of renewing an existing one. Replacing the command happens with
\let
instead of with \renewcommand
.
Regarding why this is better, see the example below.
- It makes the new command robust. As pointed out by Marco Daniel a simple
\newcommand
instead of \DeclareRobustCommand
would cause double spacing in the table of contents if using this command in a section title.
With the first approach you will run into trouble when dealing with commands which do not do the job itself but call another command which does it for them, like for example commands with an optional parameter or with a starred version.
Consider the following example:
\documentclass{article}
\newcommand{\test}[1][world]{hello #1}
\show\test
\expandafter\show\csname\string\test\endcsname
\let \originaltest=\test
\renewcommand{\test}[1][world]{\originaltest[\emph{#1}]}
\show\test
\show\originaltest
\expandafter\show\csname\string\test\endcsname
\begin{document}
\ifx \test \originaltest
true
\else
false
\fi
\end{document}
Here, the definition of \test
does not change because (leaving all of the protection stuff aside) it does nothing more than handling the optional parameter (which is the same in this example).
The really interesting stuff, that you intended to copy, however, was saved in another command called \\test
and was overridden by \renewcommand
.
If you would try to expand \test
now you would run into an infinite loop: \test
would somehow expand to \\test
which would expand to \originaltest[\emph{#1}]
which would somehow expand to \\test
again and so on.