latex
Written by Daniel Herber on March 23, 2017 (updated on February 13, 2019 with arXiv command).
The code for doiCmd is available at [link](blogs/latex/post_6/doiCmd.tex).
---
### Description
#### DOI
Good citations typically include the [doi](http://www.apastyle.org/learn/faqs/what-is-doi.aspx) for the particular reference.
> A digital object identifier (doi) is a unique alphanumeric string assigned by a registration agency (the International DOI Foundation) to identify content and provide a persistent link to its location on the Internet.
Only some [bibtex bibliography styles](https://www.sharelatex.com/learn/Bibtex_bibliography_styles) support the `doi` field but most support the `note` field. For example, [IEEEtran](https://www.ctan.org/tex-archive/macros/latex/contrib/IEEEtran/bibtex) does not natively support the `doi` field.
The custom `\doi` command can be used in the `note` (or similar field) to place a hyperlinked doi in your bibliography. For example, if your `doi` is `10.2514/1.J052182`, then use:
```tex
note = {\doi{10.2514/1.J052182}},
```
to include the doi for the reference in your bib file.
#### arXiv
With the recent update, a command that supports [arXiv](https://arxiv.org/) links is available which are used in many fields.
> arXiv is an e-print service...
The custom `\arxiv` command can be used in the note (or similar field) to place a hyperlinked arxiv reference in your bibliography. For example, if your arxiv number is 0706.1234, then use:
```tex
note = {\arxiv{0706.1234}},
```
to include the arxiv number for the reference in your bib file.
#### Input
To include this command, simply include doiCmd.tex in your preamble (or copy the source code):
```tex
\input{doiCmd}
```
The command removes the trailing full stop if present (see the example below).
---
### Example
The code in this section demonstrates the commands `\doi` and `\arxiv`. A working example is available on Overleaf at [link](https://www.overleaf.com/read/cpspgtfqmjbv). Note that the trailing full stop has been removed in the citation.
PDF: [example](blogs/latex/post_6/post_6_example.pdf)
PNG:

Code for the example:
```tex
\documentclass{article}
% custom doi command
\input{doiCmd}
% for the local bib file
\usepackage{filecontents}
% local bib file
\begin{filecontents}{\jobname.bib}
@article{Allison2014a,
author = {James T Allison and Daniel R Herber},
title = {Multidisciplinary Design Optimization of Dynamic Engineering Systems},
journal = {AIAA Journal},
year = {2014},
volume = {52},
number = {4},
pages = {691--710},
month = apr,
note = {\doi{10.2514/1.J052182}},
}
@incollection{Marti2003a,
author = {Rafael Mart\'{i}},
title = {Multi-Start Methods},
booktitle = {Handbook of Metaheuristics},
editor = {F Glover and G A Kochenberger},
publisher = {Springer},
year = {2003},
volume = {57},
pages = {355--368},
note = {\doi{10.1007/0-306-48056-5_12}},
}
@misc{Antezana2007a,
author = {Jorge Antezana and Enrique Pujals and Demetrio Stojanoff},
title = {Convergence of iterated Aluthge transform sequence for diagonalizable matrices {II}: $\lambda$-{Aluthge} transform},
year = {2007},
howpublished = {arXiv Preprint},
month = jun,
note = {\arxiv{0706.1234}},
}
\end{filecontents}
% blue links
\hypersetup{colorlinks=true, allcolors=blue}
\begin{document}
Lorem ipsum~\cite{Allison2014a, Marti2003a, Antezana2007a}.
\bibliographystyle{IEEEtran} % bib style
\bibliography{\jobname} % print the bibliography
\end{document}
```
---
### Source Code
Below is the source code for [doiCmd](blogs/latex/post_6/doiCmd.tex). Some of the code is based on the answers at [link](http://tex.stackexchange.com/questions/72827).
```tex
% DOI and ARXIV Commands for Bib Files
% Written by Daniel Herber
% -----------------------------------------------
% one option is to use the 'note' field with this command
% -----------------------------------------------
% for example, if your doi is 10.2514/1.J052182
% then for the citation for the reference in your bib file, use
% note = "\doi{10.2514/1.J052182}",
% -----------------------------------------------
% for example, if your arxiv number is 0706.1234
% then for the citation for the reference in your bib file, use
% note = "\arxiv{0706.1234}",
% requires hyperref package for \href command
\usepackage{hyperref}
% doi command (use in bib file)
\newcommand{\doi}[1]{{doi:~\href{https://doi.org/#1}{\nolinkurl{#1}}}\rmFullStop}
% arXiv command (use in bib file)
\newcommand{\arxiv}[1]{{arXiv:\href{https://arxiv.org/abs/#1}{#1}}\rmFullStop}
% command to remove full stop if the next character
\newcommand*{\rmFullStop}{\rmifnextchar{.}{}{}}
% command to check the next character and replace if present
% \rmifnextchar{X}{[removed text]}{[no X text]}
% if X is the next character, then it is removed and [removed text] is inserted
% otherwise, the character is not removed and [no X text] is inserted
% based on http://tex.stackexchange.com/questions/72827
\makeatletter
\newcommand{\rmifnextchar}[3]{%
\begingroup
\ltx@LocToksA{\endgroup#2}%
\ltx@LocToksB{\endgroup#3}%
\ltx@ifnextchar{#1}{%
\def\next{\the\ltx@LocToksA}%
\afterassignment\next
\let\scratch= %
}{%
\the\ltx@LocToksB
}%
}
\makeatother
```