217

I would like to add to all my bibliography items a DOI (Digital Object Identifier) link or a link to a download location.

How can I do this with the natbib / BibTeX / hyperref combination? Currently I am using the plainnat style, but I'm willing to change that.

8
  • Duplicate of tex.stackexchange.com/questions/3039/… (which comes up as the second hit when you search this site for DOI)
    – Seamus
    Commented Oct 6, 2010 at 9:28
  • 8
    @Seamus No, that question is about solving a problem with doi's, while I could not figure out how to it completely. Related: yes, dupliate: no.
    – Peter Smit
    Commented Oct 6, 2010 at 9:31
  • 1) I still don't think the questions are different. 2) Your first move should be to try and google/search tex SE for an answer, and you would have found one
    – Seamus
    Commented Oct 6, 2010 at 9:47
  • 6
    @Seamus Check for example google.com/… It is not clear for me what the solution is. And no, on this website there was no clear answer for my question.
    – Peter Smit
    Commented Oct 6, 2010 at 9:51
  • 12
    I'd agree with Peter: the question is not a duplicate (the previous one is about a very specific issue), although the relation between the two is clear.
    – Joseph Wright
    Commented Oct 6, 2010 at 10:23

11 Answers 11

123

Include your DOIs in the BibTeX database under the doi field and include the URLs under the url field; for example:

\begin{filecontents*}{test.bib}
@article{foo2010,
  author = "Foo Bar",
  journal = "J.P.B.",
  year = 2010,
  title = "Where the wild things are.",
  doi = {10.1.1/jpb001},
  url = {http://dx.doi.org/10.1.1/jpb001}
}
\end{filecontents*}

\documentclass{article}
\usepackage{natbib,hyperref}
\begin{document}
test \citet{foo2010}
\bibliographystyle{plainnat}
\bibliography{test}
\end{document}

If you wish to hyperlink the DOI, I believe that loading the doi package will perform this automatically.

5
  • 9
    Worked great for me, I just had to download doi.sty from: ctan.org/tex-archive/macros/latex/contrib/doi Commented Jan 5, 2012 at 6:50
  • @Will Robertson the font of the doi numbers differs from the rest of the text. Can I fix its font (or, if necessary, the size) to be coherent with the references?
    – Gagik
    Commented Dec 21, 2022 at 11:02
  • The font of the doi numbers differs from the rest of the text. Can I change the font style, size, color, etc. of the doi only?
    – Gagik
    Commented Dec 22, 2022 at 10:34
  • I seem to have a conflict between the DOI package and the verse and hyperref packages. Loading the DOI package after each one does not seem to address the conflict. Anyone else have a similar issue? Commented Feb 4, 2023 at 20:00
  • \usepackage{doi} solved it for me in Overleaf with natbib, abbrevnat using pdfLatex compiler. Thanks
    – Paloha
    Commented Apr 13, 2023 at 12:02
54

A minimal change would be to use the plainurl style instead of plainnat.

You could also continue to use plainnat and give a suitable definition of \doi (to override the non-hyperlinked version \provided by plainnat), eg:

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

In both cases, just use a doi field in your .bib file.

4
39

I came across this thread recently while solving a variant of Peter's question.

Instead of adding a hyperlink DOI to each of the bibliography items, you may want not to write the DOI explicitly but to make another field of the bibliographic item clickable with an hyperlink to the download location. In some journals, the hyperlink is associated to the group "Journal Name, volume, page number" for instance. You may find some existing bibliography style files doing that, but sometimes you need to add this feature to a personal bibliography style. In this case, none of the above solutions work. The hack I came up with is to define the following function in the .bst file:

FUNCTION {doilink}
{ duplicate$ empty$
{ pop$ "" }
{ doi empty$
    { skip$ }
    { "\href{http://dx.doi.org/" doi * "}{" * swap$ * "}" * }
  if$
}
if$
}

Here is an example of how to call the function:

FUNCTION {format.vol.num.pages}
{ volume field.or.null
  boldface
  pages empty$
    'skip$
    { duplicate$ empty$
    { pop$ format.pages }
    {  ", " * pages first.page.number * }
      if$
    }
  if$
  doilink
}

In this case the volume and pages will be hyperlinks. In general the hyperlink will be associated to the item on the top of the stack when the function doilink is called. You also need to make sure that the doi is declared as a possible field for bibliographic entries. As a minimal example:

ENTRY
{ author
  doi
  journal
  key
  pages
  title
  volume
  year
} 

This may not be the most robust solution but it solved my problem. I thought it might be useful to some people here.

EDIT

Following @laclaro 's follow-up question, I add an example of a .tex file calling the modified .bst file:

\documentclass{article}

\usepackage{natbib}

\usepackage{color}
\definecolor{darkblue}{rgb}{0.,0.,0.4}
\definecolor{darkred}{rgb}{0.5,0.,0.}

\usepackage[pdftex,colorlinks=true,linkcolor=darkblue,citecolor=darkred,urlcolor=blue]{hyperref}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@article{Brune1996,
    Author = {Brune, M  and Hagley, E and Dreyer, J and Maître, X and  Maali, A and Wunderlich, C and Raimond,J.M. and Haroche,S },
    Title = {Observing the Progressive Decoherence of the “Meter” in a Quantum Measurement},
    Year = {1996},
    Journal = {Phys. Rev. Lett.},
    volume = {77},
    pages = {4887},
    doi = {10.1103/PhysRevLett.77.4887}}
\end{filecontents}


\begin{document}

\cite{Brune1996}

\bibliographystyle{mystyle}
\bibliography{\jobname}

\end{document}

and a screenshot of what it looks like:

enter image description here

Here, clicking on the volume or page number opens the doi link. To adapt this so that the hyperlink is on the journal, you would need to modify the function FUNCTION {format.journal} in the .bst style file rather than the FUNCTION {format.vol.num.pages} as was done here.

2
30

Will Robertson showed how to do it with natbib and hyperref (as requested by the original poster). The most important point is of course to have a doi field in your bib file.
Another solution would be to use biblatex instead of natbib. My experience is that biblatexis at the same time very flexible and very stable, especially when some fileds (e.g name or doi) contain strange/special characters. Here is a MWE that should compile fine as it is:

\documentclass[english]{article}

\usepackage[autostyle]{csquotes}
\usepackage[
    backend=biber,
    style=authoryear,
    natbib=true,
    sortlocale=en_US,
    url=false, 
    doi=true,
    eprint=false
]{biblatex}
\addbibresource{biblatex-examples.bib}

\usepackage{hyperref}
\hypersetup{colorlinks=true}

\begin{document}
Lorem ipsum dolor sit amet~\citep{kastenholz}.
At vero eos et accusam et justo~\citet{sigfridsson}.
\printbibliography
\end{document}
5
  • Thanks for the answer! I have a further question, is it possible to have the doi field point to the address specified by url? Because prepending dx.doi.org would point you to an address which would likely redirect you again, then it makes sense for us to put the actual address in url and let doi link directly to its value.
    – zyy
    Commented Jan 13, 2020 at 5:37
  • 1
    The URL might change, the DOI is a permanent identifier. So, please always use the DOI where possible. The redirect is not a problem, it is part of the solution.
    – matth
    Commented Jan 13, 2020 at 8:24
  • Thanks, that make sense!
    – zyy
    Commented Jan 13, 2020 at 14:59
  • @matth Note that both crossref and datacite explicitly recommend displaying DOIs using complete URLs (see data cite link for reasons). I am also interested in learning how to get biblatex to do this (i.e. add the https://doi.org/ prefix when displaying the doi field). Commented Aug 12, 2021 at 12:11
  • 1
    @leopold.talirz See this issue. You can just add \DeclareFieldFormat{doi}{\url{https://doi.org/#1}}.
    – Heisenbugs
    Commented Jul 31, 2022 at 10:10
26

@Lev: that worked, although DOIs can contain special latex characters like underscores, such as:

https://doi.org/10.1007/3-540-08755-9_9

So I modified your hack to:

\newcommand*{\doi}[1]{\href{https://doi.org/\detokenize{#1}}{doi: \detokenize{#1}}}
3
12

My reference section requires to output doi's also in the bibliography. Inspired from Corentin's solution, I come up with the following hack in the .bst file:

I add a doi field in the ENTRY section, if it does not contain:

ENTRY
{ 
    address
    ...
    year
    doi
}

Next, I define a function output.doi

FUNCTION {output.doi}
{ 
    doi empty$
        { skip$ }
        { "\href{http://dx.doi.org/" doi * "}{doi:" * doi * "}" * output }
    if$
}

Then, in the entries that I want to report doi field, I call this function, after a new block. In my case, it is required at the end, before the notes:

FUNCTION {article}
{ 
    ...
    new.block
    output.doi
    new.block
    note output
    fin.entry
}

I added the doi field to article, inproceedings and incollection types.

For a bib entry with doi = {10.1007/978-3-642-20946-8\_2}, this solution displays a doi:10.1007/978-3-642-20946-8_2 reference at the bibliography, and when clicked it links to http://dx.doi.org/10.1007/978-3-642-20946-8_2 address.

6

Just adding my piece, I have myself revised the .bst style I wanted (apalike) to include ISBN, ISSN, DOI, and URL. I did not find any "apalikeurl" version so far, so I did it myself. It builds links for DOIs and ISBNs, and for ISBNs it is even capable of managing several IDs in the same field (i.e. one link for each).

https://github.com/matthieu-vergne/LaTeX/blob/master/apalike-refs.bst

Now I wonder if there is a way to use options for the style to say which IDs to accept for the display. I made all that just yesterday and did not have the time to investigate further. Comments and fixes are also welcome.

2

For anyone interested in unsrt style, i used the code here and modified a line with the help from a reply here and it worked perfectly as I wanted.

The modified part of the code:

FUNCTION {article}
{ output.bibitem
format.authors "author" output.check
new.block
format.title "title" output.check
new.block
crossref missing$
{ journal emphasize "journal" output.check
    format.vol.num.pages output
    format.date "year" output.check
}
{ format.article.crossref output.nonnull
    format.pages output
}
if$
format.doi output
new.block
output.doi
note output
fin.entry
}

Save the code to your directory as "unsrtDOI.bst" and at the end of your latex file:

\bibliography{b.bib}
\bibliographystyle{unsrtDOI}
0
0

A possible workaround is to include them in the notes field of your .bib. Some bibliography management tools do that when exporting, like the PMID to bib converter https://www.bioinformatics.org/texmed/

It automatically provides a notes field with the appropriate content:

\href{<Link to PUBMED>}{<PMID #>}
0

You can add the url as an additional note in the following way

@article{Foi,

Author = {ABC},

 ...
 ....

 note={\url{https://doi.org/10.1006/s00454-016-00156-0}},
  
-1

For IEEE style numerical citation, import package natlib with option numbers:

\usepackage[numbers]{natlib}

Source: More Options,page 5

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .