2

I want to use hyperref with natbib for the elsarticle class, including correct DOIs but I get strange errors or output no matter what I try:

(All attempts compiled with bibtex and pdflatex)

Attempt 0: Success, without hyperref

\documentclass{elsarticle}
\usepackage{filecontents}
\begin{filecontents}{mwe.bib}
  @article{Ashburner2005,
    author = {Ashburner, John and Friston, Karl J},
    doi = {10.1016/j.neuroimage.2005.02.018},
    journal = {NeuroImage},
    title = {{Unified segmentation.}},
    year = {2005}
  }
\end{filecontents}
\begin{document}
  \cite{Ashburner2005}
  \bibliographystyle{elsarticle-num-names}
  \bibliography{mwe}
\end{document}

Output:

enter image description here

Attempt 1: Death by hyperref

\usepackage{hyperref}                 % <-- added this to preamble

Output:

ERROR: Undefined control sequence. \doi{\bibinfo{doi}{10.1016/j.neuroimage.2005.02.018}}.

enter image description here

Attempt 2: uri package

\usepackage[doipre={DOI:~}]{uri}      % <-- added this to preamble

Output:

No errors, but DOI is wrong.

enter image description here

Attempt 3-A: user-defined \doi command:

As suggested in another post, I tried:

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

Output:

ERROR: Undefined control sequence. \doi{\bibinfo{doi}{10.1016/j.neuroimage.2005.02.018}}.

Compile failed

Attempt 3-B: user-defined \doi command, updated:

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

Output:

No errors, but DOI is wrong again, in a different way.

enter image description here

In every case where the DOI link is wrong, the link also breaks.

Any help is appreciated.

jessexknight
  • 2,732

1 Answers1

5

This seems to work, making \bibinfo work by expansion.

enter image description here

\documentclass{elsarticle}
\usepackage{filecontents}
\begin{filecontents}{mwe.bib}
  @article{Ashburner2005,
    author = {Ashburner, John and Friston, Karl J},
    doi = {10.1016/j.neuroimage.2005.02.018},
    journal = {NeuroImage},
    title = {{Unified segmentation.}},
    year = {2005}
  }
\end{filecontents}
\usepackage{hyperref}
\newcommand*{\doi}[1]{DOI \href{https://doi.org/#1}{\texttt{#1}}}
\makeatletter
% expandable version of ...
\def\bibinfo#1{%
  \@ifundefined{bibinfo@X@#1}%
    {\@firstofone}
    {\csname bibinfo@X@#1\endcsname}}
\makeatother
\begin{document}
  \cite{Ashburner2005}
  \bibliographystyle{elsarticle-num-names}
  \bibliography{mwe}
\end{document}
David Carlisle
  • 757,742