1

I am using the following setting to get bibtex entries from doi. There are two issues in my setting.

One is the entry name. For example, I can get the following entry from doi. The entry name becomes 2023. I want to change e.g., Lei_2023. That is, "First name of the first author+_ +year".

Another is about pages. The following entry yields a weird expression on-page information after the latex compilation. Once I delete the - and retype - on my setting utf-8, I can fix it. I want to remove the retype process.

https://www.anghyflawn.net/blog/2014/emacs-give-a-doi-get-a-bibtex-entry/

(defun get-bibtex-from-doi (doi)
 "Get a BibTeX entry from the DOI"
 (interactive "MDOI: ")
 (let ((url-mime-accept-string "text/bibliography;style=bibtex"))
   (with-current-buffer 
     (url-retrieve-synchronously 
       (format "http://dx.doi.org/%s" 
        (replace-regexp-in-string "http://dx.doi.org/" "" doi)))
     (switch-to-buffer (current-buffer))
     (goto-char (point-max))
     (setq bibtex-entry 
          (buffer-substring 
            (string-match "@" (buffer-string))
              (point)))
     (kill-buffer (current-buffer))))
 (insert (decode-coding-string bibtex-entry 'utf-8))
 (bibtex-fill-entry))
@article{2023,
  title =    {Co-benefits of carbon neutrality in enhancing and
                  stabilizing solar and wind energy},
  volume =   {13},
  ISSN =     {1758-6798},
  url =      {http://dx.doi.org/10.1038/s41558-023-01692-7},
  DOI =      {10.1038/s41558-023-01692-7},
  number =   {7},
  journal =  {Nature Climate Change},
  publisher =    {Springer Science and Business Media LLC},
  author =   {Lei, Yadong and Wang, Zhili and Wang, Deying and
                  Zhang, Xiaoye and Che, Huizheng and Yue, Xu and
                  Tian, Chenguang and Zhong, Junting and Guo, Lifeng
                  and Li, Lei and Zhou, Hao and Liu, Lin and Xu,
                  Yangyang},
  year =     {2023},
  month =    {Jun},
  pages =    {693–700}
}

BiBTex

hrkshr
  • 113
  • 8

1 Answers1

1

EDIT

from the answer here we find that we can also use url-insert-file-contents(-literally) to obtain the body of the response

Using this, we can simplify your function to

(defun get-bibtex-from-doi (doi)
  "Get a BibTeX entry from the DOI"
  (interactive "MDOI: ")
  (let* ((url-mime-accept-string "application/x-bibtex")
         (bibtex (with-temp-buffer (url-insert-file-contents-literally
                                    (concat "http://dx.doi.org/" doi))
                                   (buffer-string))))
    (insert bibtex)
    ;; optionally
    ;; (bibtex-fill-entry)
    ))

You can further modify the function to your needs of course.

END EDIT

Although this could be fixed with elisp, it is better to use the information in this article.

Just change the url-mime-accept-string to "application/x-bibtex"

(defun get-bibtex-from-doi (doi)
 "Get a BibTeX entry from the DOI"
 (interactive "MDOI: ")
 (let ((url-mime-accept-string "application/x-bibtex"))
   (with-current-buffer 
     (url-retrieve-synchronously 
       (format "http://dx.doi.org/%s" 
        (replace-regexp-in-string "http://dx.doi.org/" "" doi)))
     (switch-to-buffer (current-buffer))
     (goto-char (point-max))
     (setq bibtex-entry 
          (buffer-substring 
            (string-match "@" (buffer-string))
              (point)))
     (kill-buffer (current-buffer))))
 (insert (decode-coding-string bibtex-entry 'utf-8))
 (bibtex-fill-entry))

I guess this also fixes the 'latex compilation' pages issue.

dalanicolai
  • 6,108
  • 7
  • 23