Exporting citations with biblatex

| categories: org-mode | tags:

Table of Contents

This post illustrates that org-ref works with biblatex. We create a simple document and export it to pdf, and HTML for this post. We also explore how to modify the export behavior of a link. You should look at the org-source at the bottom to see the whole setup; it does not all export to either format.

We need a simple export type with no default packages to avoid the natbib packages I have setup in my default list. Here is the setup. Just run C-c C-c in the block to temporarily add this to your setup.

(add-to-list 'org-latex-classes
	     '("article-biblatex"
	       "\\documentclass{article}
 [NO-DEFAULT-PACKAGES]
 [PACKAGES]
 [EXTRA]"
	       ("\\section{%s}" . "\\section*{%s}")
	       ("\\subsection{%s}" . "\\subsection*a{%s}")
	       ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
	       ("\\paragraph{%s}" . "\\paragraph*{%s}")
	       ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

Add some citations andriotis-2014-infor,armiento-2014-high,biskup-2014-insul-ferrom-films,chemelewski-2014-amorp-feooh,chen-2014-inter-effec and then a single citation chen-2014-inter-effec.

and a complicated latex \cite[pre text][post text]{chen-2014-inter-effec}. Note this one will export to LaTeX fine, but not to HTML.

I would like to create a citation link that exports that way. We will do it by using a parseable syntax in the description of a link. We will have to temporarily define a new format function to achieve this. Here it is, just for the autocite command.

(defun org-ref-format-autocite (keyword desc format)
  (when (eq format 'latex)
    (concat "\\autocite"
	    (cond
	     ((string-match "::" desc)
	      (format "[%s][%s]" (car (setq results (split-string desc "::"))) (cadr results)))
	     (desc (format "[%s]" desc)))
	    (format "{%s}" keyword))))

This is the syntax:

a citation with post text: [[autocite:armiento-2014-high][post text]]

a citation with pre and post text:  [[autocite:andriotis-2014-infor][pre text::post text]]

a citation with post text: armiento-2014-high (the post text is not rendered in html).

a citation with pre and post text: andriotis-2014-infor (the pre/post text is not rendered in html).

The links in org-mode are no longer that readable when they are collapsed as descriptive links, but they are not too bad as literal links.

Here is the file and exporting-with-biblatex.pdf . One of those links is for the pdf, and one is for the HTML file.

1 Summary

org-ref seems to work pretty well with biblatex now.

We use a printbibliography link here. This exports to the latex command, or an html bibliography.

Bibliography

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.6

Discuss on Twitter

Creating bibliographies in other formats with org-ref

| categories: org-mode, bibtex | tags:

org-ref automatically generates bibliographies in LaTeX export, and it does a reasonable job automatically generating HTML bibliographies (ox-bibtex probably does this better, but it relies on an external program, whereas this approach is all elisp). Here we illustrate how to generate other formats, e.g. plain text, or org-mode formatted.

org-ref provides a convenient function that generates a bibliography entry for a key formatted according to the variable org-ref-bibliography-entry-format. This variable is a string that uses the reftex percent escapes to create an entry. The default is setup for an HTML entry like this:

  "%a, %t, <i>%j</i>, <b>%v(%n)</b>, %p (%y). <a href=\"%U\">link</a>. <a href=\"https://doi.org/%D\">doi</a>."

We can redefine it temporarily to get other formats. Here is an example of getting an org-formatted entry with some italics and bold text.

(let ((org-ref-bibliography-entry-format "%a, %t, /%j/, *%v(%n)*, %p (%y). [[%U][link]]. [[https://doi.org/%D][doi]]."))
(org-ref-get-bibtex-entry-citation "andriotis-2014-infor"))

"Andriotis, Mpourmpakis, , Broderick, Rajan, Datta, Somnath, Sunkara \& Menon, Informatics guided discovery of surface structure-chemistry relationships in catalytic nanoparticles, The Journal of Chemical Physics, 140(9), 094705 (2014). link . doi .

Now, we put some citations of various types in for water splitting mccrory-2013-bench-heter, CO2 capture alesi-2012-evaluat-primar, and microfluidic devices voicu-2014-microf-studies. We will convert these links to a bibliography shortly.

Next, we generate an org-formatted bibliography. We will create a bracketed label at the beginning of the entry, and the org-format after that. This is a functional enough bibliography to be useful I think, and it illustrates the ideas. We will do some light transforming to replace escaped & with regular & in the bibliography.

;; temorarily redefine the format
(let ((org-ref-bibliography-entry-format "%a, %t, /%j/, *%v(%n)*, %p (%y). [[%U][link]]. [[https://doi.org/%D][doi]]."))

  (mapconcat
   (lambda (key)
     (format "[%s] %s" key
	     (replace-regexp-in-string
	      "\\\\&"
	      "&" (org-ref-get-bibtex-entry-citation key))))
   (org-ref-get-bibtex-keys) "\n\n"))

[alesi-2012-evaluat-primar] Alesi & Kitchin, Evaluation of a Primary Amine-Functionalized Ion-Exchange Resin for \ce{CO_2} Capture, Industrial & Engineering Chemistry Research, 51(19), 6907-6915 (2012). link . doi .

[mccrory-2013-bench-heter] McCrory, Jung, Peters, Jonas & Jaramillo, Benchmarking Heterogeneous Electrocatalysts for the Oxygen Evolution Reaction, J. Am. Chem. Soc., 135(45), 16977–16987 (2013). link . doi .

[voicu-2014-microf-studies] Voicu, Abolhasani, Choueiri, Rachelle, Lestari, Seiler, , Menard, Greener, Guenther, Axel, Stephan & Kumacheva, Microfluidic Studies of \ce{CO_2} Sequestration by Frustrated {L}ewis Pairs, Journal of the American Chemical Society, 0(0), null (2014). [[][link]]. doi .

You can see some minor issues with the formatting, e.g. sometimes the link is empty, if there is no url in the bibtex entry. There is no easy way to fix that. The 0 and null values in the last entry are because that is an ASAP article, and that is what is in the bibtex entry. I do not try to expand the latex code, and do not plan to do that. I do not know why there appears to be a blank author in the last entry, or why the author full names are not used. Those are reftex issues and low priority to fix for me. They do not exist in the LaTeX export. The main point here is to get a reasonably useful bibliography that you can adapt as you want.

Bibliography

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.6

Discuss on Twitter

Professor Kitchin selected for a Simon Seed grant

| categories: news | tags:

http://www.cmu.edu/proseed/awards-recipients/

This grant will support "Reproducible Research: Computing in Science and Engineering" - Developing a new undergraduate course that integrates professional tools so engineering students can learn and practice modern methods of reproducible research. (John Kitchin)

This will be a project that integrates emacs, org-mode and computing into the creation of literate technical documents.

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.6

Discuss on Twitter

Exporting citations in html

| categories: org-mode | tags:

Now that org-ref works well for LaTeX, I want to explore a simple approach to exporting citations with a bibliography in html. This will enable me to put citations in blogposts, like this one armiento-2014-high and these ones daza-2014-carbon-dioxid,mehta-2014-ident-poten,suntivich-2014-estim-hybrid. We should be able to have the same citation in more than one place like this armiento-2014-high, but only have one entry in the bibliography. The bibliography should be sorted if we ask for it alesi-2012-evaluat-primar. I am curious to see this book citation: day-1995-scien-englis.

The first thing we need is a list of bibtex keys cited in this buffer.

(defun org-ref-get-bibtex-keys ()
  "return a list of unique keys in the buffer"
  (interactive)
  (let ((keys '()))
    (org-element-map (org-element-parse-buffer) 'link
      (lambda (link)
        (let ((plist (nth 1 link)))
          (when (-contains? org-ref-cite-types (plist-get plist ':type))
            (dolist
                (key
                 (org-ref-split-and-strip-string (plist-get plist ':path)))
              (when (not (-contains? keys key))
                (setq keys (append keys (list key)))))))))
    keys))

(org-ref-get-bibtex-keys)
armiento-2014-high daza-2014-carbon-dioxid mehta-2014-ident-poten suntivich-2014-estim-hybrid alesi-2012-evaluat-primar day-1995-scien-englis

Good. Now, we need to create an HTML string for the bibliography. For each key, we will create an unordered list of simple citations. Each citation will be a named anchor in html. Let us start with a function that takes a key, and generates the HTML for that entry.

(defun org-ref-get-bibtex-entry-html (key)
(interactive)

 (let ((org-ref-bibliography-files (org-ref-find-bibliography))
       (cb (current-buffer))
       (file) (entry))

   (setq file (catch 'result
                (loop for file in org-ref-bibliography-files do
                      (message "looking for %s in %s" key file)
                      (if (org-ref-key-in-file-p key (file-truename file))
                          (throw 'result file)
                        (message "%s not found in %s" key (file-truename file))))))
   (set-buffer (find-file-noselect file))
   (prog1
       (bibtex-search-entry key nil 0)
     (setq entry (org-ref-bib-citation))
     (set-buffer cb))

   (format "<li><a name=\"#%s\">[%s] %s<\\a><li>" key key entry)))

(org-ref-get-bibtex-entry-html "mehta-2014-ident-poten")
<li><a name="#mehta-2014-ident-poten">[mehta-2014-ident-poten] Mehta, Prateek and Salvador, Paul A. and Kitchin,  John R., "Identifying Potential \ce{BO_2} Oxide Polymorphs for  Epitaxial Growth Candidates", ACS Applied Materials \& Interfaces, 0:null (2014)<\a><li>

That looks excellent. Now we simply map that function over the list of keys.

(defun org-ref-get-html-bibliography ()
(interactive)
(let ((keys (org-ref-get-bibtex-keys)))
(when keys
(concat "<h1>Bibliography</h1>
<ul>"
(mapconcat (lambda (x) (org-ref-get-bibtex-entry-html x)) keys "\n")
"\n</ul>"))))

(org-ref-get-html-bibliography)

<h1>Bibliography</h1> <ul><li><a name="#armiento-2014-high">[armiento-2014-high] Armiento, R. and Kozinsky, B. and Hautier, G. and Fornari, M. and Ceder, G., "High-throughput screening of perovskite alloys for piezoelectric performance and thermodynamic stability", Phys. Rev. B, 89:134103 (2014)<\a><li> <li><a name="#daza-2014-carbon-dioxid">[daza-2014-carbon-dioxid] Daza, Yolanda A. and Kent, Ryan A. and Yung, Matthew M. and Kuhn, John N., "Carbon Dioxide Conversion by Reverse Water-Gas Shift Chemical Looping on Perovskite-Type Oxides", Industrial \& Engineering Chemistry Research, 53:5828-5837 (2014)<\a><li> <li><a name="#mehta-2014-ident-poten">[mehta-2014-ident-poten] Mehta, Prateek and Salvador, Paul A. and Kitchin, John R., "Identifying Potential \ce{BO_2} Oxide Polymorphs for Epitaxial Growth Candidates", ACS Applied Materials \& Interfaces, 0:null (2014)<\a><li> <li><a name="#suntivich-2014-estim-hybrid">[suntivich-2014-estim-hybrid] Suntivich, Jin and Hong, Wesley T. and Lee, Yueh-Lin and Rondinelli, James M. and Yang, Wanli and Goodenough, John B. and Dabrowski, Bogdan and Freeland, John W. and Shao-Horn, Yang, "Estimating Hybridization of Transition Metal and Oxygen States in Perovskites from O K-edge X-ray Absorption Spectroscopy", The Journal of Physical Chemistry C, 118:1856-1863 (2014)<\a><li> <li><a name="#alesi-2012-evaluat-primar">[alesi-2012-evaluat-primar] Alesi, W. Richard and Kitchin, John R., "Evaluation of a Primary Amine-Functionalized Ion-Exchange Resin for \ce{CO_2} Capture", Industrial \& Engineering Chemistry Research, 51:6907-6915 (2012)<\a><li> <li><a name="#day-1995-scien-englis">[day-1995-scien-englis] Robert A. Day, "Scientific English: A Guide for Scientists and Other Profesionals", , : (1995)<\a><li> </ul> <h1>Bibliography</h1> <ul><li><a name="#armiento-2014-high">[armiento-2014-high] Armiento, R. and Kozinsky, B. and Hautier, G. and Fornari, M. and Ceder, G., "High-throughput screening of perovskite alloys for piezoelectric performance and thermodynamic stability", Phys. Rev. B, 89:134103 (2014)<\a><li> <li><a name="#daza-2014-carbon-dioxid">[daza-2014-carbon-dioxid] Daza, Yolanda A. and Kent, Ryan A. and Yung, Matthew M. and Kuhn, John N., "Carbon Dioxide Conversion by Reverse Water-Gas Shift Chemical Looping on Perovskite-Type Oxides", Industrial \& Engineering Chemistry Research, 53:5828-5837 (2014)<\a><li> <li><a name="#mehta-2014-ident-poten">[mehta-2014-ident-poten] Mehta, Prateek and Salvador, Paul A. and Kitchin, John R., "Identifying Potential \ce{BO_2} Oxide Polymorphs for Epitaxial Growth Candidates", ACS Applied Materials \& Interfaces, 0:null (2014)<\a><li> <li><a name="#suntivich-2014-estim-hybrid">[suntivich-2014-estim-hybrid] Suntivich, Jin and Hong, Wesley T. and Lee, Yueh-Lin and Rondinelli, James M. and Yang, Wanli and Goodenough, John B. and Dabrowski, Bogdan and Freeland, John W. and Shao-Horn, Yang, "Estimating Hybridization of Transition Metal and Oxygen States in Perovskites from O K-edge X-ray Absorption Spectroscopy", The Journal of Physical Chemistry C, 118:1856-1863 (2014)<\a><li> </ul>

That is basically all we need. The citation links will export as hrefs to these named targets, so they should work fine. All we need to do is modify the blogofile code a bit to use this, and add those functions to org-ref, and we should get a bibliography in our blogposts.

Bibliography

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.6

Discuss on Twitter

Using org-ref to keep your bibtex files in order

| categories: emacs, bibtex | tags:

Maintaining an accurate, useful bibliography of references is critical for scientific writing. It is also not trivial. While it is easy to download and copy bibliographic entries to your database, these entries are often incomplete, not consistently formatted, and can contain invalid characters. org-ref provides several utility functions to help with this.

1 "cleaning" a bibtex entry

Consider this bibtex entry from http://pubs.acs.org/action/showCitFormats?doi=10.1021%2Fie500588j .

@article{doi:10.1021/ie500588j,
author = {Okada, Tomohiko and Ozono, Shoya and Okamoto, Masami and Takeda, Yohei and Minamisawa, Hikari M. and Haeiwa, Tetsuji and Sakai, Toshio and Mishima, Shozi},
title = {Magnetic Rattle-Type Core–Shell Particles Containing Iron Compounds with Acid Tolerance by Dense Silica},
journal = {Industrial & Engineering Chemistry Research},
volume = {0},
number = {0},
pages = {null},
year = {0},
doi = {10.1021/ie500588j},

URL = {http://pubs.acs.org/doi/abs/10.1021/ie500588j},
eprint = {http://pubs.acs.org/doi/pdf/10.1021/ie500588j}
}

On the surface it looks fine, but there are the following issues with it:

  1. The bibtex key is hard to remember. I like systematically named keys.
  2. There is a bare & in the journal title, which is not legal in LaTeX.
  3. There is no year entry, even though it is a 2014 entry. The pages, volume, and number are also problematic, but this is an ASAP article and the reference does not have those yet.
  4. It is hard to see, but the dash between core and shell is a non-ascii character, which can cause problems in LaTeX.
  5. The entry is not very nicely aligned or indented.

You can fix these problems by putting your cursor on the bibtex entry, and typing M-x org-ref-clean-bibtex-entry. This will fix the bibtex key to a standard form, align and indent the entry, escape the & so it is legal syntax, prompt you for a year, and show you the non-ascii characters so you can replace them. The resulting, nicely formatted entry is shown below.

@article{okada-2014-magnet-rattl,
  author =	 {Okada, Tomohiko and Ozono, Shoya and Okamoto, Masami
                  and Takeda, Yohei and Minamisawa, Hikari M. and
                  Haeiwa, Tetsuji and Sakai, Toshio and Mishima,
                  Shozi},
  title =	 {Magnetic Rattle-Type Core-Shell Particles Containing
                  Iron Compounds with Acid Tolerance by Dense Silica},
  journal =	 {Industrial \& Engineering Chemistry Research},
  volume =	 0,
  pages =	 {null},
  year =	 2014,
  doi =		 {10.1021/ie500588j},
  number =	 0,
  url =		 {http://pubs.acs.org/doi/abs/10.1021/ie500588j},
  eprint =	 {http://pubs.acs.org/doi/pdf/10.1021/ie500588j},
}

The key formatting comes from these definitions:

;; variables that control bibtex key format for auto-generation
;; I want firstauthor-year-title-words
;; this usually makes a legitimate filename to store pdfs under.
(setq bibtex-autokey-year-length 4
      bibtex-autokey-name-year-separator "-"
      bibtex-autokey-year-title-separator "-"
      bibtex-autokey-titleword-separator "-"
      bibtex-autokey-titlewords 2
      bibtex-autokey-titlewords-stretch 1
      bibtex-autokey-titleword-length 5)

You should develop a discipline to clean each entry as you add them, and before you cite them. It is a pain to change the key, and then find and change all the places you used that key before. Now that you have a systematic key, go ahead and download the pdf for the article, and save it in your pdf directory by that key name. Set the variable org-ref-pdf-directory to this directory, and later when you click on citations you will be able to open the pdf easily.

2 Validating your bibliography

elisp:bibtex-validate
will check your bibliography for valid syntax. This is a bibtex command.

org-bib.bib

3 Sorting your bibtex file

It is a good idea to keep your bibtex file sorted. This will facilitate finding duplicate entries, and will make it easier to find things. I usually add entries to the top of the file, and then clean them. Then run the command

elisp:bibtex-sort-buffer
. This will sort the entries for you. This is also a bibtex command.

org-bib.bib

4 Make a full bibliography pdf

A good way to check your bibliography for duplicates, spelling errors, and invalid formats is to make a pdf containing all the entries. Open your bibtex file, and run

elisp:org-ref-build-full-bibliography
. If all goes well, you will get a pdf of your bibliography that you can check for accuracy. If there are errors, you will have to fix them until the pdf is generated.

Try it out: org-bib.bib

5 Finding bad citation links

Sometimes you will get bad citation links in your document. Maybe there is no corresponding entry, maybe you typed in the wrong key, maybe you changed the key. Either way, you need to find them and fix them. Run the command

elisp:org-ref-find-bad-citations
to find them.
cite:test

6 Extracting citations entries

You will often work from your default bibliography for your own work. Eventually you will need to extract the entries cited so you can send them to someone. The command

elisp:org-ref-extract-bibtex-entries
will do that for you. If I have cited something
cite:calle-vallejo-2010-trend-stabil
.

7 Summary

You can see a screen cast of this post here: http://screencast.com/t/yZCOdO6kJ

8 References

9 Bibtex entries

#+BEGINSRC: text :tangle extract-bib7108tYg.bib @article{calle-vallejo-2010-trend-stabil, author = {Calle-Vallejo, F. and Martinez, J. I. and Garcia- Lastra, J. M. and Mogensen, M. and Rossmeisl, J.}, title = {Trends in Stability of Perovskite Oxides}, journal = "Angewandte Chemie-International Edition", volume = 49, number = 42, pages = {7699-7701}, year = 2010, doi = {10.1002/anie.201002301}, keyword = {density functional calculations heats of formation perovskites thermochemistry transition-metals catalysts ferroelectricity}, } #+ENDSRC

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.6

Discuss on Twitter
« Previous Page -- Next Page »