Skip to content

Instantly share code, notes, and snippets.

@Cadair
Last active August 29, 2015 14:13
Show Gist options
  • Save Cadair/5f66da0c4d14055836b2 to your computer and use it in GitHub Desktop.
Save Cadair/5f66da0c4d14055836b2 to your computer and use it in GitHub Desktop.
The start of a astropy + pythontex primer.
\documentclass[]{article}
\usepackage{pythontex}
\usepackage{graphicx}
\usepackage{pgf}
\usepackage{float}
%opening
\title{An Example of Astropy and PythonTeX Integration}
\author{Stuart Mumford}
\begin{pythontexcustomcode}{py}
import sys
import numpy as np
import matplotlib
matplotlib.use('pgf')
matplotlib.rc('text', usetex=True)
matplotlib.rc('font', family='serif')
matplotlib.rc('font', size=14.0)
matplotlib.rc('font', weight='normal')
matplotlib.rc('legend', fontsize=14.0)
matplotlib.rc("pgf", texsystem="pdflatex")
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
\end{pythontexcustomcode}
\begin{document}
\maketitle
\section{Basic PythonTeX}
The basic PythonTeX environments are \verb|\begin{pycode}| and \verb|\py|. \verb|pycode| is a block environment for including Python code, \verb|\py| is a inline output environment, for displaying output.
A simple example of both is to create a variable in a \verb|pycode| environment and then inline it in the text using \verb|\py|:
\begin{pyverbatim}
\begin{pycode}
import astropy.units as u
myquantity = 100 * u.kg/u.m**2
\end{pycode}
\end{pyverbatim}
\begin{pycode}
import astropy.units as u
myquantity = 100 * u.kg/u.m**2
\end{pycode}
You can then include this variable in a sentence using \verb~\py|myquantity|~. The density of Astropy is \py|myquantity|. Quantity has a little feature which is useful here: \verb~\py|myquantity_repr_latex_()|~ gives \py|myquantity._repr_latex_()|.
\section{Configuring PythonTeX}
It is possible to execute python code in PythonTeX that affects all environments throughout the document. This is commonly used for global imports and configuration.
It can be achieved using the \verb|\begin{pythontexcustomcode}| environment.
This block is especially useful for configuring matplotlib to use settings compatible with PythonTeX.
The matplotlib pgf backend can be configured to use LaTeX for typesetting, and the fonts configured to match that of your LaTeX document.
An example of this configuration is shown below, and used in this document.
\begin{pyverbatim}
\begin{pythontexcustomcode}{py}
import sys
import numpy as np
import matplotlib
matplotlib.use('pgf')
matplotlib.rc('text', usetex=True)
matplotlib.rc('font', family='serif')
matplotlib.rc('font', size=14.0)
matplotlib.rc('font', weight='normal')
matplotlib.rc('legend', fontsize=14.0)
matplotlib.rc("pgf", texsystem="pdflatex")
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
\end{pythontexcustomcode}
\end{pyverbatim}
This configuration can be used to generate a plot:
\begin{pyblock}
import sunpy.map
aiamap = sunpy.map.Map(sunpy.AIA_171_IMAGE)
fig = plt.figure()
im = aiamap.plot()
fig.savefig('myplot.pdf')
\end{pyblock}
which can be included with the normal LaTeX figure environment:\footnote{The pgf package is required.}
\begin{verbatim}
\begin{figure}[H]
\pgfimage[width=\columnwidth]{myplot}
\end{figure}
\end{verbatim}
\begin{figure}[H]
\pgfimage[width=0.7\columnwidth]{myplot}
\end{figure}
\section{Astropy Table}
We can make use of this integration between LaTeX and Python to make the use of tools with LaTeX representation support built in even easier.
The Astropy Table class has a very powerful TeX formatter, which we will use to inline a Table in this document.
\begin{pyblock}
from astropy.table import Table
t = Table.read("ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/snrs.dat",
readme="ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/ReadMe",
format="ascii.cds")
# Crop the table down a bit
t = t[t.colnames[0:10]][0:50]
\end{pyblock}
Currently this is the best way I can think of to do this, however, there may be better ways.
\py|t.write(sys.stdout, format='latex')|
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment