### 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 patch or patchcmd systems.

Patch gives you an ingenious (and difficult to understand) mechanism, and comes as an old-style LaTeX documented macro file. Sadly the old-style doc macros are no longer available, but the file (patch.doc) may be input directly, and the documentation may be read (un-typeset). Roughly speaking, one gives the command a set of instructions analagous to sed substitutions, and it regenerates the command thus amended. The author of this FAQ has (slightly reluctantly) given up using patch...

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, if `\``b` contains "`b`", then `\``patchcommand``\``b{a}{c}` will produce a new version of `\``b` that contains "`abc`".

`patch.doc`
  macros/generic/patch.doc
`patchcommand.sty`
  macros/latex/contrib/patchcmd (zip, browse)

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