6

Context:

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

\newcommand{\doi}[1]{\href{http://dx.doi.org/#1}{doi:#1}}

Problem:

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:

http://dx.doi.org/10.1007/1-84628-249-7{_}13

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

\href{http://dx.doi.org/10.1007/1-84628-249-7_13}{text}

(use of or hint to additional packages is welcome)

Background:

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.

MWE:

\documentclass{article}
\usepackage{hyperref}
\newcommand{\doi}[1]{\href{http://dx.doi.org/#1}{doi:#1}}

\begin{document}
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}
\end{document}
4
  • When I load the hyperref package, the syntax \doi{1234{\textunderscore }5} works fine for me. In contrast, \doi{1234_5} does not compile. Commented 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). Commented Aug 19, 2015 at 11:53
  • Thanks for adding the MWE. It helps to understand the issue. Commented 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
    Commented Aug 19, 2015 at 15:11

1 Answer 1

2

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

\documentclass{article}
\usepackage{hyperref}
\usepackage{regexpatch}

\newcommand*{\doi}[1]{%
  \def\tmpdoi{#1}%
  \xpatchcmd*\tmpdoi{{\textunderscore}}{_}{}{}%
  \href{http://dx.doi.org/\tmpdoi}{doi:#1}%
}

\begin{document}
  DOI is clickable:
  \doi{10.1007/1-84628-249-7{\textunderscore}13}
\end{document}
2
  • 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. Commented 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. Commented 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 .