Python and $\LaTeX$


In [ ]:
%matplotlib inline

import sympy as sp
import numpy as np
import matplotlib.pyplot as plt

Python uses the $\LaTeX$ language to typeset equations.

Most LaTeX commands are prefixed with a "\". For example \pi is the command to produce the lower case Greek letter pi. The characters # $ % & ~ _ ^ \ { } are special characters in LaTeX. If you want to typeset them you need to put a \ in front of them. For example \$ will typeset the symbol $ % - comment. Everything is ignored after a % $ - Math mode. Start and Stop math mode. $\pi$ ^ - Superscript in Math mode. $2^2$ _ - Subscript in Math mode. $2_2$

Use a single set of $ to make your $\LaTeX$ inline and a double set $$ to center

$$ \int \cos(x)\ dx = \sin(x) $$

This code will produce the output:

$$ \int \cos(x)\ dx = \sin(x) $$

Use can use $\LaTeX$ in plots:


In [ ]:
plt.style.use('ggplot')

x = np.linspace(0,2*np.pi,100)
y = np.sin(5*x) * np.exp(-x)

plt.plot(x,y)
plt.title("The function $y\ =\ \sin(5x)\ e^{-x}$")
plt.xlabel("This is in units of 2$\pi$")

plt.text(2.0, 0.4, '$\Delta t = \gamma\, \Delta t$', color='green', fontsize=36)

Use can use sympy to make $\LaTeX$ equations for you!


In [ ]:
z = sp.symbols('z')

In [ ]:
a =  1/( (z+2)*(z+1) )

print(sp.latex(a))
$$ \frac{1}{\left(z + 1\right) \left(z + 2\right)} $$

In [ ]:
print(sp.latex(sp.Integral(z**2,z)))
$$ \int z^{2}\, dz $$

Astropy can output $\LaTeX$ tables


In [ ]:
from astropy.io import ascii
from astropy.table import QTable

In [ ]:
T = QTable.read('Zodiac.csv', format='ascii.csv')

In [ ]:
T[0:3]

In [ ]:
ascii.write(T, format='latex')

Some websites to open up for class:


Assignment for Week 8

----------------------------------------------------------------------------- LaTeX homework - Create a LaTeX document with references. ----------------------------------------------------------------------------- Start with the file: Example.tex Minimum required elements: * Between 2 and 4 pages in length (pages > 4 will be ignored). * At least two paragraphs of text (the text should be coherent). * At least 5 references from ADS * http://adsabs.harvard.edu/abstract_service.html * Make sure to \cite{} the references in your paper * The equation on the back of the handout * One (or more) equation(s) of your choice. * One Compulsory plot generated with python (png format) * Start with t = np.linspace(0,12*np.pi,2000) * generate a butterfly plot: http://en.wikipedia.org/wiki/Butterfly_curve_(transcendental) * I have started it for you im the last cell of this notebook * One other plot/image (do not reuse and old one!) * One table of at least 4 columns and 4 rows. * Bonus points given for interesting content! ----------------------------------------------------------------------------- Create a PDF file: Save the file as FirstLast.pdf (i.e. TobySmith.pdf) Upload the PDF to the class canvas site ----------------------------------------------------------------------------- Deadline: Tuesday Nov 29 - 5pm (-5 pts for each 30 minutes late) -----------------------------------------------------------------------------

In [ ]:
t = np.linspace(0,12*np.pi,2000)

fig,ax = plt.subplots(1,1)                    # One window
fig.set_size_inches(11,8.5)                   # (width,height) - letter paper landscape

fig.tight_layout()                            # Make better use of space on plot