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!!
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.
Simply Enter Esc and type m it will convert to text cell.
%%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.
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.
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.
In addition to a working installation of jupyter you need:
LaTeX (installation guide).
Pandoc (installation guide).
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).
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:
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]
.
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
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.
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 ---
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With