Latex from Ipython notebooks: The poor man’s solution

I have been using LaTeX for some time, in favour of traditional text editors such as MS Word or LibreOffice. The quality and versatility of LaTeX documents simply far exceeds that of its over-complicated competitors.

Image

More recently however, I have started using python, and by extension the Ipython notebook interface. What a pleasant, interactive and dynamic interface this turned out to be.

However, last week I found myself frustrated with the lack of integration between python and LaTeX. There exists a LaTeX package called python.sty which allows you to integrate python code and output into your pdf document. It has provided me with some headaches, and mostly was not what I was looking for, as it doesn’t play well with LaTeX editors.

Then I proceeded to try out a poor man’s solution to integrating LaTeX with Ipython notebooks such that you can run all cells and generate a decent looking pdf output, that remains fairly dynamic.

This was my first attempt, and it worked rather okay. As I was working on a new project today, I made some rather nice refinements. It remains an inadequate solution, but I get by. Below are the steps I followed, conceptually. You can view the notebook here.

The steps

Note ipython notebook was run in –pylab=inline mode

  1. Place a %%writefile magics command at the start of every section of relevant markdown code cell.
  2. Then proceed to go about construction your ipython notebook as usual.
  3. Create a bibliography for yout Ipynb in bibtex format and write to .bib format using the %%writefile magics.
  4. Write a LaTeX template which you will use to consolidate all the various images and sections into one document.
  5. Change the Markdown cells to code cells and run the %%writefile function to generate .txt files of the markdown code.
  6. Write additional latex section you which to insert, such as for images and abstract.
  7. Use the pandoc (http://johnmacfarlane.net/pandoc/README.html) command line tool from the %%bash magics to convert your markdown code to the appropriate .tex sections that will make is way into the final document.
  8. Add the appropriate .tex sections to your LaTeX document using the command rather than the command
  9. Run the bibtex command from bash to generate the bibliography.
  10. Run the pdflatex command from bash to generate your .pdf doc and display with xdg-open.

I am having fun today.