TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The doi package has a neat \doi command, that transforms a doi string into \href{http://dx.doi.org/<the doi>}{doi:<the doi>}. The uri package has something similar with the help of the url package.

I would like to have an extended version of that command that takes an arbitrary link text as an optional argument, like \mydoi[Paper title]{doi}. I tried that myself with the help of this, but the weird characters in DOIs destroy that.

Take, e.g., the DOI 10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H. I checked manually that \href{http://dx.doi.org/10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H}{doi:10.1002/(SICI)1097-0037(199712)30:4\textless263::AID-NET4\textgreater3.0.CO;2-H} works, I just want a macro that produces that sequence from, say \mydoi{10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H}, or \mydoi[Title]{10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H}.

share|improve this question
maybe this helps: shortdoi.org/10.1002/… – matth May 12 '15 at 11:37
up vote 5 down vote accepted

This works fluently, no need to escape any chars. Our macro \xx@doi is only a modification of original \@doi to treat the optional argument. In \x@doi we store the original \@doi so that we can redefine \@doi.

enter image description here



  \let\#\relax \let\_\relax \let\textless\relax \let\textgreater\relax
  \edef\#{\@percentchar 23}\edef\_{_}%
    \edef\textless{\@percentchar 3C}\edef\textgreater{\@percentchar 3E}%
  \edef\x{\endgroup\doitext\the\toks1 \the\toks0}\x




share|improve this answer
Usually a space is needed between \the\toks1 and \the\toks0 (as in doi.sty). Also, note that the odd-numbered token registers 1, 3, 5, 7, 9 are for global assignments only, while the even-numbered token registers 0, 2, 4, 6, 8 are for local assignments only (thanks to Heiko). – Ahmed Musa Jan 9 '13 at 11:37
@AhmedMusa Thanks for the space. Considering the second issue, you should address the doi package maintainer, I only copy-paste-modified his code :) – yo' Jan 9 '13 at 14:17

Well you could try this one using the ifthen package, that defines the first argument to be optional (indeed its set to \empty as default and the command displays one or the other version depenting on that default value

        \href{http://dx.doi.org/#2}{doi: #2}%
Version 1: DOI \mydoi{DOI1}\\
Version 2: Link Text \mydoi[LinkText]{DOI2}

Which produces something like

DOI Example

(where of course both links link to the corresponding DOI which is nonsense in this example)

I also tested that with some DOIs I had available, though I'm not shure, what your “weird characters” are. For . / and such this works.

Update 1 The OP requested, that also the DOI 10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H should work and noted, that escapting just the second argument of \href works. Encapsulating the second argument in a code environment escapes the noted characters, so one way would be to use the listings package as in


        \href{http://dx.doi.org/#2}{doi: \lstinline!#2!}%

Which compiles fine. Though - at least on my system (MacTeX 2012) clicking on the link does not open the link in a browser.

share|improve this answer
This breaks, e.g., for 10.1007/978-3-540-39658-1_67. – mafp Jan 8 '13 at 18:38
Because than you would have to escape the _ to \_, that's why i asked :) I'll think about that and extend my answer, if i found something. – Ronny Jan 8 '13 at 18:40
You can (and should) just escape that yourself, i.e. using \mydoi{10.1007/978-3-540-39658-1\_67} or \mydoi[Flow Approximation]{10.1007/978-3-540-39658-1\_67} works fine, i.e. the document compiled and the link works. The same of course for other sepcial characters as & and stuff in URLs. – Ronny Jan 8 '13 at 18:49
This does not help much. For one, I don't want to escape all DOIs by hand (I have a lot), but even if I would, there is stuff like \mydoi{10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H}, which does not get better with \mydoi{10.1002/(SICI)1097-0037(199712)30:4\textless263::AID-NET4\textgreater3.0‌​.CO;2-H}. – mafp Jan 8 '13 at 21:27
@Ronny I recommend you to look up the definitions of \doi and \@doi in the file doi.sty to see how you have to define a macro that reads #_<> as strings and not as TeX special characters. – yo' Jan 9 '13 at 8:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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