5

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

mafp
  • 19,096
  • 1
    maybe this helps: http://shortdoi.org/10.1002/%28SICI%291097-0037%28199712%2930:4%3C263::AID-NET4%3E3.0.CO;2-H – matth May 12 '15 at 11:37

2 Answers2

5

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

\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}
yo'
  • 51,322
  • 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 09 '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 09 '13 at 14:17
2

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}\\
Version 2: Link Text \mydoi[LinkText]{DOI2}
\end{document}

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

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

Moriambar
  • 11,466
Ronny
  • 6,110
  • This breaks, e.g., for 10.1007/978-3-540-39658-1_67. – mafp Jan 08 '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 08 '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 08 '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 08 '13 at 21:27
  • Uff, I haven't found a way up to now to escape the input for href, such that would work. I'm also working with a lot of DOIs; my Library (Papers2/Mac) escapes the DOIs when exporting them to the .bib file. Maybe extend your starting post with that information. If I'm getting an idea how to escape that, i.e. taking your DOIs as #2 and escape them befor output into \href i'll give you a note. – Ronny Jan 08 '13 at 21:34
  • I extended my answer, which I noticed yesterday and which should work the same as you noted in the original post. Though on my computer, the link is not clickable – Ronny Jan 09 '13 at 07:01
  • 1
    @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 09 '13 at 08:13
  • @Ronny And notice that you do not use {ifthen}, since \ifx is a TeX primitive: http://www.tug.org/utilities/plain/cseq.html#ifx-rp – yo' Jan 09 '13 at 08:16
  • Ups, well i copied that from a bigger project and in striping down the commands ... well sometimes - thanks for the hint :) – Ronny Jan 09 '13 at 10:03