[Tex/LaTex] biblatex: Is it possible to patch macros created with \newbibmacro


This question led to a new feature in a package:

The etoolbox package provides various tools that "are useful to hook into or modify existing code" (etoolbox manual, section 3.4). The most important one is the \patchcmd command with the syntax


which "extracts the replacement text of a <command>, replaces [the first occurence of] <search>
with <replace>, and reassembles the <command>"
. Other useful macros are, e.g., \apptocmd and \pretocmd which will append resp. prepend code to the replacement text of a command.

The biblatex package features the commands \newbibmacro, \renewbibmacro, and \providebibmacro (plus their starred counterparts). These commands resemble \newcommand/\renewcommand/\providecommand "except that [the command name] may contain characters such as numbers and punctuation marks and does not start with a backslash" (biblatex manual, section 4.6.4). The "bibmacros" defined this way may be executed via \usebibmacro{<command name>}.

Is it possible to define a \patchbibmacro command that extracts and modifies the replacement text of macros created with \newbibmacro the same way \patchcmd does for macros created with \newcommand?

Best Answer

  \expandafter#1\csname abx@macro@\detokenize{#2}\endcsname}

Now you have available

\patchbibmacro \pretobibmacro \apptobibmacro \showbibmacro


\newbibmacro{foo}[1]{-#1-} actually executes

\expandafter\newcommand\csname abx@macro@\detokenize{foo}\endcsname[1]{-#1-}

(I omit some gory details, but this is almost the truth).

This, of course, won't work for macros with optional arguments, but here's how:

  \expandafter#1\csname\expandafter\string\csname abx@macro@\detokenize{#2}\endcsname\endcsname}

With \patchbibmacroopt you can define biblatex macros defined for instance by


One has to remember that with \newcommand{\baz}[2][x]{-#1-#2-} the macro doing the real work is


that, of course, has to be accessed with


Easier solution

The question led to adding some features to the xpatch package, which now has


which work out of the box even with bibmacros defined having an optional argument; the syntax is the same as the one for \patchcmd, \pretocmd and \apptocmd:

\xpretobibmacro{<name>}{<code to prepend>}{<success>}{<failure>}
\xapptobibmacro{<name>}{<code to append>}{<success>}{<failure>}

So, if foo has been defined with \newbibmacro{foo}[1]{-#1-} one can say, for instance,


to change the first - into +.