Patching existing commands

In the general case (possibly sticking something in the middle of an existing command) this is difficult. However, the common requirement, to add some code at the beginning or the end of an existing command, is conceptually quite easy. Suppose we want to define a version of a command that does some small extension of its original definition: we would naturally write:

\renewcommand{\splat}{\mumble\splat}

However, this would not work: a call to \splat would execute \mumble, and the call the redefined \splat again; this is an infinite recursive loop, that will quickly exhaust TeX’s memory.

Fortunately, the TeX primitive \let command comes to our rescue; it allows us to take a “snapshot” of the current state of a command, which we can then use in the redefinition of the command. So:

\let\OldSmooth\smooth
\renewcommand{\smooth}{\mumble\OldSmooth}

effects the required patch, safely. Adding things at the end of a command works similarly. If \smooth takes arguments, one must pass them on:

\renewcommand{\smooth}[2]{\mumble\OldSmooth{#1}{#2}}

The general case may be achieved in two ways. First, one can use the LaTeX command \CheckCommand; this compares an existing command with the definition you give it, and issues a warning if two don’t match. Use is therefore:

\CheckCommand{\complex}{<original definition>}
\renewcommand{\complex}{<new definition>}

This technique is obviously somewhat laborious, but if the original command comes from a source that’s liable to change under the control of someone else, it does at least warn you that your patch is in danger of going wrong.

Otherwise, LaTeX users may use one of the patchcmd, ted or etoolbox packages.

The patchcmd package addresses a slightly simpler task, by restricting the set of commands that you may patch; you mayn’t patch any command that has an optional argument, though it does deal with the case of commands defined with \DeclareRobustCommand. The package defines a \patchcommand command, that takes three arguments: the command to patch, stuff to stick at the front of its definition, and stuff to stick on the end of its definition. So, after

\def\b{b}
\patchcmd\b{a}{c}

we will have a new version of \b defined as “abc”.

The ted package is a ‘token list editor’, and provides a command \Substitute* which will patch the contents of a macro, putting the result in a token-list, or (optionally) using the result to (re)define a macro. The package may also be used as a powerful debugging tool.

The etoolbox (which provides user access to e-TeX’s programming facilities) provides a command \patchcmd which will perform a “string substitution” in a macro’s definition. The package also provides commands that prepend (\pretocmd) or append (\apptocmd) to the definition of a command.

Finally, we’ll briefly consider a package that is (just about) usable, but not really recommendable. Patch gives you an ingenious (and difficult to understand) mechanism, and comes as an old-style LaTeX documented macro file, which can no longer be processed (as in generating documentation for a new package). Fortunately, the file (patch.doc) may be input directly, and “documentation” may found by reading the source of the package. Roughly speaking, one gives the command a set of instructions analogous to sed substitutions, and it regenerates the command thus amended. Unless you can’t do your job any other way, you had probably best avoid Patch.

etoolbox.sty
macros/latex/contrib/etoolbox (gzipped tar, browse)
patch.doc
macros/generic/misc/patch.doc
patchcommand.sty
macros/latex/contrib/patchcmd (gzipped tar, browse)
ted.sty
macros/latex/contrib/ted (gzipped tar, browse)

This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=patch