# Typesetting ADS references in BibLaTeX bibliographies

The NASA Astrophysical Data System (ADS) is a database of pre-print and peer-reviewed papers spanning physics and astronomy. In particular, the ADS entry links together the pre-print and peer-reviewed papers, so including the ADS entry in a printed BibLaTeX bibliography can be a big convenience to readers of your paper.

## Introduction¶

The NASA ADS is a wonderful resource for astronomers and cosmologists because. First, it connects pre-print papers published on arXiv with the peer-reviewed papers on publisher websites into a single entry. For instance, the entry for one of the Bicep/Keck Array papers — BK-VII: Matrix Based E/B Separation applied to Bicep2 and the Keck Array — has clear links to both the pre-print via the arXiv number (1603.05976) and the published paper in The Astrophysical Journal via its DOI number (10.3847/0004-637X/825/1/66).

Furthermore, the ADS provides the ability to export a BibTeX citation for each reference. Many publishers do as well, but the format varies with each publisher, so always retrieving citations from the ADS means your bibligraphy database is much more consistent.

For these two reasons, I wanted to include the ADS link directly in the bibliography of my thesis. The default BibLaTeX styles only typeset preprint and DOI links, though, so this is how I managed to typeset the ADS entries as well.

## LaTeX Macros¶

The following requires use of the BibLaTeX package with biber used as the backend processor.

We’ll use the following citation for the BK-VII paper (with the author list truncated for brevity) exported from ADS as a prototypical example:

@article{2016ApJ...825...66B,
author = {{The BICEP2 and Keck Array Collaborations}},
title = "{BICEP2/Keck Array. VII. Matrix Based E/B Separation Applied to Bicep2 and the Keck Array}",
journal = {\apj},
year = 2016,
month = jul,
volume = {825},
number = {1},
eid = {66},
pages = {66},
doi = {10.3847/0004-637X/825/1/66},
archivePrefix = {arXiv},
eprint = {1603.05976},
primaryClass = {astro-ph.IM},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}


The critical feature is that the ADS includes the adsurl field as a way to link back to the original ADS entry. To make use of this, BibLaTeX has to have the field added to its internal data model. The first two lines tell BibLaTeX to process the field, and the third is used to strip away the URL prefix and retain only the unique ID (here, 2016ApJ...825...66B).

\DeclareDatamodelFields[type=field,datatype=verbatim]{adsurl}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
replace={}]
}
}
}


(The ADS recently revamped their website, so the regular expression is setup to match both the prior and current forms of URLs.)

We then format the ADS field during typesetting using the following macro definition. It prefixes the ID with the string “ads:" and then hyperlinks the ID if the hyperref package has been loaded.

\DeclareFieldFormat{adsurl}{%
\ifhyperref
}


Finally, no citation format will actually print the adsurl field, so we must override or patch the bibliography printing macros. The texdef utility is particularly useful for situations like this since you can quickly descend through macro definitions, and paired with inspecting the source files themselves, you can identify the correct patch point.

Doing so, I found that bibliography macro doi+eprint+url is used by all relevant entry types, so it’s a good candidate for modifying. Rather than copy-paste-editting the definition of the macro, I decided to dynamically patch it using xpatch. The following inserts the ADS entry before any generic URL is printed.

\xpatchbibmacro{doi+eprint+url}
{\iftoggle{bbx:url}}
{}
{}


Patching the command rather than replacing it has the advantage that specifying options like doi=false while loading the package continues to work as expected.

The \DeclareDatamodel* macros are special and cannot be specified within the document preamble after biblatex has already been loaded. The BibLaTeX documentation suggests they must be included via a datamodel (*.dbx) file, but I found that they can also be written into a biblatex.cfg configuration file in the same directory as the document. This has the advantage that the \DeclareFieldFormat and \xpatchbibmacro commands can live in the same place.

## Example¶

Putting these pieces all together, the following code will produce this example bibliography:

example.bib:

@article{2016ApJ...825...66B,
author = {{The \bicep2 and \keckarray\ Collaborations}},
title = "{\bicep/\keckarray\ VII. Matrix Based $E$/$B$ Separation Applied to \bicep2 and the \keckarray}",
journal = {\apj},
year = 2016,
month = jul,
volume = {825},
number = {1},
eid = {66},
pages = {66},
doi = {10.3847/0004-637X/825/1/66},
archivePrefix = {arXiv},
eprint = {1603.05976},
primaryClass = {astro-ph.IM},
adsnote = {Provided by the SAO/NASA Astrophysics Data System}
}


biblatex.cfg:

\DeclareDatamodelFields[type=field,datatype=verbatim]{adsurl}
\DeclareSourcemap{
\maps[datatype=bibtex]{
\map{
replace={}]
}
}
}
\ifhyperref
}
\xpatchbibmacro{doi+eprint+url}
{\iftoggle{bbx:url}}
{}
{}


example.tex:

\documentclass[12pt,preview=true,margin=2pt]{standalone}
\usepackage[margin=1in]{geometry}
\usepackage{xpatch} % must be before biblatex to provide \xpatchbibmacro
\usepackage[backend=biber,style=numeric,doi=false,eprint=false]{biblatex}
\usepackage{hyperref}