LaTeX from Ipython

Philip A. Marais, CSIR 2013-11-13

The poor man's solution

This is the standard Ipython notebook, where you can make comments and generate code.

In [21]:
from mpltools import style # found this online at [email protected]:tonysyu/mpltools.git

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)


Use python magics to generate tex code instead of python and save as foo.tex

In [22]:
%%writefile test1.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}



This is the poor mans solution  to integrating all of you shit into one dynamic document.


\includegraphics[width = \textwidth]{figure.png}
\caption{\emph{Here is you figure, all neat in \LaTeX.}}


Overwriting test1.tex
In [23]:
%%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

Then use the python magics to gain access to bash

Here I use the defaul latex editor to create the document, as Ipython does not support tex syntax. Yet.

When I am done, I just change the cell to raw text, so that I can run all cells and get the pdf output.

%%bash xdg-open test1.tex #use you defaul tex editor to open the document from the console

Once the document looks good, then copy & paste, for now, until I get better educated.

And then as I change the graphs etc, I just run the bash script below, which runs the pdflatex command and opens the pdf.

In [24]:

pdflatex test1.tex
xdg-open test1.pdf
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/latex/float/float.sty) (./test1.aux)
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/share/texmf/tex/latex/lm/ot1lmr.fd)
<figure.png, id=1, 722.7pt x 361.35pt> <use figure.png> (./test1.bbl) [1{/var/l
ib/texmf/fonts/map/pdftex/updmap/} <./figure.png>] (./test1.aux) ){/u
Output written on test1.pdf (1 page, 177312 bytes).
Transcript written on test1.log.

And below is code for creating the HTML version of this notebook and displaying it in your default browser.

In [25]:

ipython nbconvert LaTeX_from_Ipython_experiment.ipynb
xdg-open LaTeX_from_Ipython_experiment.html
Created new window in existing browser session.
[NbConvertApp] Using existing profile dir: u'/home/pmarais/.config/ipython/profile_default'
[NbConvertApp] Converting notebook LaTeX_from_Ipython_experiment.ipynb to html
[NbConvertApp] Support files will be in LaTeX_from_Ipython_experiment_files/
[NbConvertApp] Loaded template html_full.tpl
[NbConvertApp] Writing 256629 bytes to LaTeX_from_Ipython_experiment.html