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 (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 \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

Introduction

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))

figsize(10,5)
plot(x,y1,linewidth=2)
plot(x,y2,linewidth=2)
title('Some title \n',fontsize=20)
ylabel('y-label',fontsize=16)
xlabel('x-label',fontsize=16)

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

@Article{PER-GRA_2007,
  Author         = {Perez, Fernando and Granger, Brian E.},
  Title          = {{IP}ython: a {S}ystem for {I}nteractive {S}cientific
                 {C}omputing},
  Journal        = {{C}omput. {S}ci. {E}ng.},
  Volume         = {9},
  Number         = {3},
  Pages          = {21-29},
  month          = may,
  year           = 2007,
  url            = "http://ipython.org",
}
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.

\subsection{Part2}

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.

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

\begin{abstract}
This is the poor man's solution  to integrating all of your shit into one dynamic document.
\end{abstract}
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[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\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
\usepackage[left=1.2cm,right=1.2cm,top=0cm,bottom=0cm]{geometry}

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

\begin{document}

\maketitle

\input{abstract}
\input{section1}
\input{section2}

\nocite{PER-GRA_2007}
\bibliographystyle{unsrt}
\bibliography{ref}

\end{document}
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]:
%%bash

#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
(./test2.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, loaded.
(/usr/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/share/texmf-texlive/tex/latex/base/size11.clo))
(/usr/share/texmf-texlive/tex/latex/base/fontenc.sty
(/usr/share/texmf-texlive/tex/latex/base/t1enc.def))
(/usr/share/texmf-texlive/tex/latex/base/inputenc.sty
(/usr/share/texmf-texlive/tex/latex/base/utf8.def
(/usr/share/texmf-texlive/tex/latex/base/t1enc.dfu)
(/usr/share/texmf-texlive/tex/latex/base/ot1enc.dfu)
(/usr/share/texmf-texlive/tex/latex/base/omsenc.dfu)))
(/usr/share/texmf/tex/latex/lm/lmodern.sty)
(/usr/share/texmf-texlive/tex/latex/graphics/graphicx.sty
(/usr/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-texlive/tex/latex/graphics/graphics.sty
(/usr/share/texmf-texlive/tex/latex/graphics/trig.sty)
(/etc/texmf/tex/latex/config/graphics.cfg)
(/usr/share/texmf-texlive/tex/latex/pdftex-def/pdftex.def)))
(/usr/share/texmf-texlive/tex/latex/float/float.sty)
(/usr/share/texmf-texlive/tex/latex/geometry/geometry.sty
(/usr/share/texmf-texlive/tex/generic/oberdiek/ifpdf.sty)
(/usr/share/texmf-texlive/tex/generic/oberdiek/ifvtex.sty)
(/usr/share/texmf-texlive/tex/xelatex/xetexconfig/geometry.cfg)) (./test2.aux)
(/usr/share/texmf/tex/latex/lm/t1lmr.fd)
(/usr/share/texmf-texlive/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
)
*geometry auto-detecting driver*
*geometry detected driver: pdftex*
(/usr/share/texmf/tex/latex/lm/ot1lmr.fd)
(/usr/share/texmf/tex/latex/lm/omllmm.fd)
(/usr/share/texmf/tex/latex/lm/omslmsy.fd)
(/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/pdftex.map} <./figure.p
ng>] (./test2.aux) ){/usr/share/texmf/fonts/enc/dvips/lm/lm-ec.enc}{/usr/share/
texmf/fonts/enc/dvips/lm/lm-mathit.enc}{/usr/share/texmf/fonts/enc/dvips/lm/lm-
mathsy.enc}{/usr/share/texmf/fonts/enc/dvips/lm/lm-rm.enc}{/usr/share/texmf/fon
ts/enc/dvips/lm/lm-mathex.enc}</usr/share/texmf/fonts/type1/public/lm/lmbx10.pf
b></usr/share/texmf/fonts/type1/public/lm/lmbx12.pfb></usr/share/texmf/fonts/ty
pe1/public/lm/lmex10.pfb></usr/share/texmf/fonts/type1/public/lm/lmmi10.pfb></u
sr/share/texmf/fonts/type1/public/lm/lmmi8.pfb></usr/share/texmf/fonts/type1/pu
blic/lm/lmr10.pfb></usr/share/texmf/fonts/type1/public/lm/lmr12.pfb></usr/share
/texmf/fonts/type1/public/lm/lmr17.pfb></usr/share/texmf/fonts/type1/public/lm/
lmr8.pfb></usr/share/texmf/fonts/type1/public/lm/lmri10.pfb></usr/share/texmf/f
onts/type1/public/lm/lmri8.pfb></usr/share/texmf/fonts/type1/public/lm/lmsy10.p
fb>
Output written on test2.pdf (1 page, 221423 bytes).
Transcript written on test2.log.