I'm trying to write a command that prints appropriately hyperlinked DOIs for a bibliography system. Here was my first attempt


This works very well for most DOIs. However, some DOIs have pesky non-LaTeX-friendly characters in them like _. It turns out that if you butcher the DOI by transforming the _ to a \_, then everything works! (Even the LINK in the \href is correct.)

I'm not happy with this solution, because it means that we have to include butchered DOIs in our BibTeX database, which could confuse other users. I would like a solution that permits me to use legitimate DOIs as input.

Well, I just learned about the \detokenize command. With this, I can update the command above to


which works very well for the hyperlink part; however, underscores naturally confuse typesetting of the second argument. Is there a simple solution? I had thought to use the verbatim environment here, but it does not seem to be robust enough to be used in a function this way.

Any help much appreciated. A few more constraints:

  • I would like to minimize the number (and complexity) of included packages since this all takes place inside a style file for a journal.
  • It would be good enough for a small piece of code that transformed every _ to a \_; is such a thing easy to come by?

All the best, Alex

  • Why not adding \detokenize also to the second part? Can you give an example of a problematic DOI? – egreg Mar 30 '12 at 0:10
  • Dear egreg, If I typeset \detokenize{10.1007/978-3-540-70575-8_10} in regular paragraph mode, the _ turns into a superscript dot. I'm using \usepackage{mathptmx} to set the fonts in this case. – acr Mar 30 '12 at 1:04
  • Do you know the package uri? – Speravir Mar 30 '12 at 2:16
  • Dear Speravir, No--I'll look into it. Thanks for the pointer. – acr Mar 30 '12 at 11:37
  • Dear Speravir, Hey--uri looks good! Unlike the doi package, it seems that you can make calls to uri from other macros, which I need. This solves the major part of the problem--thanks! My remaining issue is that I would like to avoid changing the font--uri typesets in monospace and I would like to maintain the font of the narrative. Thanks again! If you turn this into an "answer," I can give you credit for the solution. – acr Mar 30 '12 at 12:20

Beside Harish’s solution with doi you can use the package uri, which uses facilities of the package url to define some commands, one of them is \doi again.

But if you want to keep the font family, you need to define this command yourself. I show you both. Comment out the line with \usepackage{uri} and remove the comment sign before \DeclareUrlCommand to see the effects.

\usepackage{hyperref} % loads package "url", provides linking facilities
\usepackage[doipre={DOI:~}]{uri} % loads "url", if not already loaded
%\DeclareUrlCommand\doi{\def\UrlLeft##1\UrlRight{DOI\nobreakspace\href{http://dx.doi.org/##1}{##1}}\urlstyle{rm}} % works only, if "url" is loaded, but not "uri"

Note, that you can’t change the font family with the options for uri (‘doipre’ and ‘doipost’) – at least here at me the computer freezed. The syntaxes for \DeclareUrlCommand and \urlstyle are described in the documentation for url.

Update: Added a even more weird DOI given by matth in comments.


In my previous answer, there was some confusion between - and _. Well, here is a solution that takes _ as well. I used the doi package.

\renewcommand\doitext{Doi: }

enter image description here

Hope this will be useful.

  • Dear Harish, Thanks for your response. However, this doesn't seem to work with DOIs that contain underscores, like 10.1007/978-3-540-70575-8_10 . Perhaps I am misunderstanding your suggestion? – acr Mar 30 '12 at 1:25
  • @AlexanderRussell Sorry for the confusion earlier. I have updated the answer. Hope it will be useful. – user11232 Mar 30 '12 at 5:40
  • Dear Harish, The doi package looks great; I'm embarrassed that I didn't find it by a web search. However, it seems that you cannot use the \doi command inside another macro. Is my situation hopeless? – acr Mar 30 '12 at 11:36
  • I don't understand. Why you want to use it inside another macro? http://dx.doi.org/ is automatically added by the package itself. You have to just type the numbers. – user11232 Mar 30 '12 at 11:42
  • Dear Harish, In my application, the DOI body is passed to my code after it is extracted from a bibliography record. It could be possible to rewrite the bibliography-handling code, but I would like to avoid that if I can. – acr Mar 30 '12 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.