Download BibTeX entry for an article using DOI

I like to use BibTeX as a format for the bibliography in both articles and my own research / notes. Such records are not always consistent across journals, which is why I tend to pull them from CrossRef, which gives them a rather format, with a minimal set of fields. Unfortunately, CrossRef has an IP filter that can block you if you pull too many records, even if you are registered with your OrcID and just use the standard web interface. This also means that the Emacs library biblio also gets blocked, as well as the routines I built for them and ebib.

Until I fix this, here is a function I cooked from a StackExchange recipe. You can call it as M-x doi-to-bib. It will ask you for a DOI and will download it using curl, which you can install using scoop, if you work in Windows. Hope it helps you.

(defun doi-to-bib (doi)
  (interactive "sEnter the doi to expand to bibtex via crossref: ")
  (dolist (p '("doi:" "" "" ""))
	(setq doi (string-remove-prefix p doi)))
  (setq doi (concat "" doi))
	 (unless (zerop
			  (call-process (executable-find "curl")
							nil t t
							"Accept: text/bibliography; style=bibtex"
	   (error "Downloading doi"))
	 (goto-char 0)
	 (while (re-search-forward "^\\([ \t][ \t]*\\)" nil t)
	   (replace-match "\n"))
	 (goto-char 0)
	 (while (re-search-forward ", \\([a-zA-Z]*=\\)" nil t)
	   (replace-match ",\n  \\1"))
	 (buffer-substring-no-properties 1 (point-max)))))