LaTeX from Ipython

An update 2013-11-11

Philip A. Marais, CSIR 2013-11-13

The poor man's solution

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 ( 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 \input{} command rather than the \include{} 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.

%%writefile section1.txt


This is a section wehre you can go crazy in markdown.

Compute some integrals for dramatic effect:

$$\int 3ax^2+2bx+c\cdot dx = ax^3+bx^2+cx +C$$
In [11]:
from mpltools import style # found this online at [email protected]:tonysyu/mpltools.git

## You can just leave the lines above and below out to get to the quick fix.

style.use(['ggplot', 'pof']) #uses ggplot style.

x = arange(-pi,pi,0.1)
y1 = (sin((x*5))/10.0)+0.5
y2 = (sin(x**2))

title('Some title \n',fontsize=20)

savefig('figure.png') #save the figure in the appropriate folder and write a section of latex code to insert it into the final document.

Using python magics to write code to file

Add your bibtex entries like you would for any latex document.
In [2]:
%%writefile ref.bib

  Author         = {Perez, Fernando and Granger, Brian E.},
  Title          = {{IP}ython: a {S}ystem for {I}nteractive {S}cientific
  Journal        = {{C}omput. {S}ci. {E}ng.},
  Volume         = {9},
  Number         = {3},
  Pages          = {21-29},
  month          = may,
  year           = 2007,
  url            = "",
Overwriting ref.bib

You can write sections in latex code that can be added to you final document where you choose

In [9]:
%%writefile section2.tex

%This is the section that contains the image.


This is the second section of your new \LaTeX document.

We also plot the graphs of $\frac{sin(5x)}{10}+0.5$ and $sin(x^2)$ for no reason.

\includegraphics[width = \textwidth]{figure.png}
\caption{\emph{Here is your figure, all neat in \LaTeX.}}
Overwriting section2.tex
For example: your abstract
In [3]:
%%writefile abstract.tex

This is the poor man's solution  to integrating all of your shit into one dynamic document.
Overwriting abstract.tex

Produce the master LaTeX template you will be using for this project.

In [5]:
%%writefile test2.tex

\documentclass[a4paper,11pt]{article} %This is the standard Latex article template
\usepackage{graphicx}    % This is for the image, not sure if it is required, but its there none the less
\usepackage{float}       % This is for placing the image at a specific location

\title{\LaTeX from Ipython notebooks}
\author{PA Marais}





Overwriting test2.tex

This is the one-cell-script that produces the tex code from the markdown cell code, produces the bibliography, and compiles the pretty PDF document.

In [12]:

#use pandoc to convert section one to tex code from markdown
pandoc -f markdown -t latex section1.txt -o section1.tex 

#run the standard bibtex and pdflatex commands to generate your final pdf

bibtex test2
pdflatex test2.tex
xdg-open test2.pdf
This is BibTeX, Version 0.99c (TeX Live 2009/Debian)
The top-level auxiliary file: test2.aux
The style file: unsrt.bst
Database file #1: ref.bib
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, loaded.
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texmf-texlive/tex/xelatex/xetexconfig/geometry.cfg)) (./test2.aux)
[Loading MPS to PDF converter (version 2006.09.02).]
*geometry auto-detecting driver*
*geometry detected driver: pdftex*
(/usr/share/texmf/tex/latex/lm/omxlmex.fd) (./abstract.tex) (./section1.tex)
(./section2.tex <figure.png, id=1, 722.7pt x 361.35pt> <use figure.png>)
(./test2.bbl) [1{/var/lib/texmf/fonts/map/pdftex/updmap/} <./figure.p
ng>] (./test2.aux) ){/usr/share/texmf/fonts/enc/dvips/lm/lm-ec.enc}{/usr/share/
Output written on test2.pdf (1 page, 221423 bytes).
Transcript written on test2.log.