# Macro for writing DOI with optional link text

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}.

-

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.

\documentclass{article}

\usepackage{hyperref}
\usepackage{doi}

\makeatletter
\let\x@doi\@doi
\def\@doi{\@ifnextchar[\xx@doi\x@doi}
\def\xx@doi[#1]#2{%
\let\#\relax \let\_\relax \let\textless\relax \let\textgreater\relax
\edef\x{\toks0={{#1}}}\x
\edef\#{\@percentchar 23}\edef\_{_}%
\edef\textless{\@percentchar 3C}\edef\textgreater{\@percentchar 3E}%
\edef\x{\toks1={\noexpand\href{http://dx.doi.org/#2}}}\x
\edef\x{\endgroup\doitext\the\toks1 \the\toks0}\x
}
\makeatother

\begin{document}

\doi{1000.00/01/23/45_153#111<abc>xyz}

\doi[My]{1000.00/01/23/45_153#111<abc>xyz}

\end{document}

-
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 :) –  tohecz 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

\documentclass[a4paper,12pt]{scrartcl}
\usepackage{hyperref,ifthen}
\newcommand{\mydoi}[2][\empty]{%
\ifx\empty#1%
\href{http://dx.doi.org/#2}{doi: #2}%
\else%
\href{http://dx.doi.org/#2}{#1}%
\fi%
}%
\begin{document}
Version 1: DOI \mydoi{DOI1}\\
\end{document}


Which produces something like

(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

\documentclass[a4paper,12pt]{scrartcl}
\usepackage{hyperref,url,ifthen,listings}

\newcommand{\mydoi}[2][\empty]{%
\ifx\empty#1%
\href{http://dx.doi.org/#2}{doi: \lstinline!#2!}%
\else%
\href{http://dx.doi.org/#2}{#1}%
\fi%
}%
\begin{document}
\mydoi{10.1002/(SICI)1097-0037(199712)30:4<263::AID-NET4>3.0.CO;2-H}
\end{document}


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

-
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. –  tohecz Jan 9 '13 at 8:13