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

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

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

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

\begin{document}

\maketitle

\begin{abstract}
This is the poor mans solution  to integrating all of you shit into one dynamic document.
\end{abstract}

\section{Section}

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

\bibliographystyle{unsrt}
\bibliography{references}

\end{document}
Overwriting test1.tex
In [23]:
%%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

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]:
%%bash

pdflatex test1.tex
xdg-open test1.pdf
This is pdfTeX, Version 3.1415926-1.40.10 (TeX Live 2009/Debian)
entering extended mode
(./test1.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) (./test1.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).]
) (/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)
<figure.png, id=1, 722.7pt x 361.35pt> <use figure.png> (./test1.bbl) [1{/var/l
ib/texmf/fonts/map/pdftex/updmap/pdftex.map} <./figure.png>] (./test1.aux) ){/u
sr/share/texmf/fonts/enc/dvips/lm/lm-ec.enc}</usr/share/texmf/fonts/type1/publi
c/lm/lmbx10.pfb></usr/share/texmf/fonts/type1/public/lm/lmbx12.pfb></usr/share/
texmf/fonts/type1/public/lm/lmr10.pfb></usr/share/texmf/fonts/type1/public/lm/l
mr12.pfb></usr/share/texmf/fonts/type1/public/lm/lmr17.pfb></usr/share/texmf/fo
nts/type1/public/lm/lmri10.pfb></usr/share/texmf/fonts/type1/public/lm/lmri8.pf
b>
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]:
%%bash 

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