Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle citations in Ipython Notebook?

What is the best way to take care of citations in Ipython Notebook? Ideally, I would like to have a bibtex file, and then, as in latex, have a list of shorthands in Ipython markdown cells, with the full references at the end of the notebook.

The relevant material I found is this: http://nbviewer.ipython.org/github/ipython/nbconvert-examples/blob/master/citations/Tutorial.ipynb

But I couldn't follow the documentation very well. Can anyone explain it? Thanks so much!!

like image 577
user3698176 Avatar asked May 05 '15 19:05

user3698176


People also ask

How do you add citations in Jupyter Notebook?

You can add citations and bibliographies using references that are stored in a bibtex file that is in your book's folder. You can then add a citation in-line in your Markdown with the {cite} role, and include the bibliography from your bibtex file with the {bibliography} directive.

How do I write text in IPython notebook?

Simply Enter Esc and type m it will convert to text cell.

What is %% Writefile in Jupyter Notebook?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on. The list sys. argv contains the command-line arguments that a program was run with.

How do you cite Markdown?

Inserting Citations. You insert citations by either using the Insert -> Citation command or by using markdown syntax directly (e.g. [@cite] or @cite ) . Citations go inside square brackets and are separated by semicolons.


1 Answers

Summary

This solution is largely based on Sylvain Deville's excellent blog post. It allows you to simply write [@citation_key] in markdown cells. The references will be formatted after document conversion. The only requirements are LaTeX and pandoc, which are both widely supported. While there is never a guarantee, this approach should therefore still work in many years time.

Step-by-Step Guide

In addition to a working installation of jupyter you need:

  1. LaTeX (installation guide).

  2. Pandoc (installation guide).

  3. A citation style language. Download a citation style, e.g., APA. Save the .csl file (e.g., apa.csl) into the same folder as your jupyter notebook (or specify the path to the .csl file later).

  4. A .bib file with your references. I am using a sample bib file list.bib. Save to the same folder as your jupyter notebook (or specify the path to the .bib file later).

Once you completed these steps, the rest is easy:

  1. Use markdown syntax for references in markdown cells in your jupyter notebook. E.g., [@Sh:1] where the syntax works like this: ([@citationkey_in_bib_file]). I much prefer this syntax over other solutions because it is so fast to type [@something].

  2. At the end of your ipython notebook, create a code cell with the following syntax to automatically convert your document (note that this is R code, use an equivalent command to system() for python):

     #automatic document conversion to markdown and then to word  #first convert the ipython notebook paper.ipynb to markdown  system("jupyter nbconvert --to markdown paper.ipynb")  #next convert markdown to ms word  conversion <- paste0("pandoc -s paper.md -t docx -o paper.docx",                 " --filter pandoc-citeproc",                 " --bibliography="listb.bib",                 " --csl="apa.csl")  system(conversion) 

    Run this cell (or simply run all cells). Note that the 2nd system call is simply pandoc -s paper.md -t docx -o paper.docx --filter pandoc-citeproc --bibliography=listb.bib --csl=apa.csl. I merely used paste0() to be able to spread this over multiple lines and make it nicer to read.

    The output is a word document. If you prefer another document, check out this guide for alternative syntax.


#Extras

  1. If you do not like that your converted document includes the syntax for the document conversion, insert a markdown cell above and below the code cell with the syntax for the conversion. In the cell above, enter <!-- and in the cell below enter -->. This is a regular HTML command for a comment, so the syntax will in between these two cells will be evaluated but not printed.

  2. You can also include a yaml header in your first cell. E.g.,

     ---  title: This is a great title.   author: Author Name  abstract: This is a great abstract  --- 
like image 62
Flo Avatar answered Sep 28 '22 02:09

Flo