In order to create DOI links I define a command\doi{<enter doi here>} as



However this breaks with stupid input™ like 10.1007/1-84628-249-7{\textunderscore }13. Because it seems to interpret the underscore, but doesn't remove the curly braces:


How would one define a command

\hrefcleanbefore{http://dx.doi.org/10.1007/1-84628-249-7{\textunderscore }13}{text}

to replace the {\textunderscore } with a real underscore (works by default) and removing the braces resulting in a working


(use of or hint to additional packages is welcome)


I have a great bibliographic tool which creates my .bib files but has stupid escapes for my doi field

doi = {10.1007/1-84628-249-7{\textunderscore }13}

I tackled with different options of preprocessing the .bib-file but they all lack the convenience of fitting in the build process. I don't think this is a complex tasks if one knows all low-level TeX commands, but all my tests seem to be inferior or conflict with the \href implementation.



DOI not clickable: \doi{10.1007/1-84628-249-7{\textunderscore }13}

HREF with \{\} not clickable:  \href{http://dx.doi.org/10.1007/1-84628-249-7{_}13}{doi:10.1007/1-84628-249-7_13}

HREF correct and clickable:  \href{http://dx.doi.org/10.1007/1-84628-249-7_13}{doi:10.1007/1-84628-249-7_13}
  • When I load the hyperref package, the syntax \doi{1234{\textunderscore }5} works fine for me. In contrast, \doi{1234_5} does not compile. Aug 19, 2015 at 11:12
  • I don't expect \doi{1234_5} to compile, I expect doi to always be entered as \doi{1234{\textunderscore }5}, just to supply \href with a different version. (I think that is the main reason the software doesn't use a plain underscore). Aug 19, 2015 at 11:53
  • Thanks for adding the MWE. It helps to understand the issue. Aug 19, 2015 at 12:11
  • Now that you have got an answer you will forgive me for saying that the real answer is either making your bibliography tool work properly or ditching it.
    – moewe
    Aug 19, 2015 at 15:11

1 Answer 1


Package regexpatch can be used to replace a string inside the definition text of a macro multiple times:



  DOI is clickable:
  • Thank you! Read a bit into the package doc: (a) yes it is quite a multi-purpose command, but this is nicely creative: I never thought about the more general "definition text" when having the very special "argument" in focus; (b) note: the sibling package xpatch can't be used, although the doc doesn't mention \xpatchcmd being different from xpatch. Aug 19, 2015 at 13:15
  • A DOI can contain more than one underscore. Therefore I am using the star form of \xpatchcmd of package regexpatch to replace all underscores in one macro call. Otherwise a loop needs to be programmed, which continues with replacing until \xpatchcmd of xpatch fails to replace something. Aug 19, 2015 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .