I used to maintain a couple BibTeX files for my research. Although there are tools to manage them, I still found it pretty painful. Because ADS has almost all astrophysics papers, it may be a good idea to give up my own BibTeX database and generate them from ADS whenever I need them.

LaTeX generates *.aux files for labels and citations. For each citation, I want to get the corresponding BibTeX entry from ADS. This turns out to be a relatively easy job. I employ the ADS “bibkey” such as 2009ApJ...701..521C in my LaTeX manuscripts. After LaTeX-ing, I can simply run the following script to generate my BibTeX file:


$aux = ($#ARGV >= 0) ? "$ARGV[0]" : "ms";
$bib = ($#ARGV >= 1) ? "$ARGV[1]" : $aux;

open AUX, "< $aux.aux";
while() {
    if ($_ =~ m/\\citation\{/i) {
	foreach (split(',', substr($_, 10, -2))) {
	    push @code, $_
		if $_ =~ m/(\d{4}\D\S{13}[A-Z.:])/i;
close AUX;

$server = 'adsabs.harvard.edu';
$script = 'cgi-bin/nph-bib_query';
$opt{data_type}   = 'BIBTEX';
$opt{db_key}      = 'ALL';
$opt{nocookieset} = '1';

$url  = "http://$server/$script?";
$url .= "$_=$opt{$_}&" foreach keys %opt;
$url .= "bibcode=";
$url .= "$_&" foreach @code;

use LWP::Simple;
$all = get $url;
die "ERROR: Couldn't get bibTeX data\n" unless defined $all;
@line = split('\n', $all);

open BIB, "> $bib.bib";
$head = '% ';
foreach (@line) {
    $_ =~ s/\ \&/\ \\and/g;
    print BIB "$head$_\n";
    $head = '' if $_ =~ m/^Retrieved/;
close BIB;

To use it for your own research, simply download the code from GitHub,

$ curl -O https://raw.githubusercontent.com/chanchikwan/adsbib/master/adsbib
$ chmod 755 adsbib

Suppose your LaTeX document is called ms.tex, this is how you use it:

$ ls -lh
-rwxr-xr-x  1 ckchan  staff   864B Apr 26  2010 adsbib
-rw-r--r--  1 ckchan  staff    53K Oct 22 17:21 ms.tex
$ latex ms; ls
ms.aux ms.dvi ms.log ms.tex # no BibTeX bibliography file
$ ./adsbib ms; ls
ms.aux ms.bib ms.dvi ms.log ms.tex # BibTeX file is created
$ bibtex ms
$ latex ms

That’s it. Enjoy and happy paper writing!


Leave a Reply

Your email address will not be published. Required fields are marked *

Set your Twitter account name in your settings to use the TwitterBar Section.