Professional Documents
Culture Documents
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
A Gentle Introduction to PythonTEX
PythonTEX
Mathematics with
Sympy
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Web Services
Conclusions
Extra Examples
TEX
page.3
Python Overview
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Overview
A Question of Primes
I General purpose, high-level programming language
Introduction to
PythonTEX
Mathematics with
I Multi-paradigm: object-oriented, imperative, functional
Sympy
Conclusions
I Origins from late 1989
Extra Examples
I Free and open-source
page.4
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Web Services
Conclusions
Extra Examples
page.5
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
“I would like to write a LATEX script that produces all the prime
PythonTEX
numbers between the numbers n and m, where n < m. How can I do
Mathematics with
Sympy this? I feel it should not be that hard, but I cannot seem to program
Plots with matplotlib it.”
— Kevin†
Web Services
Conclusions
Extra Examples
† tex.stackexchange.com/questions/134305/how-to-produce-a-list-of-prime-
numbers-in-latex/134366#134366
page.6
Andrew Mertz,
William Slough
Overview
A Question of Primes The first 30 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
Introduction to 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
PythonTEX
109, and 113. You may not find this fact very startling; but you may
Mathematics with
Sympy be surprised to learn that the previous sentence was typeset by saying
Plots with matplotlib
The first thirty prime numbers are \primes{30}.
Web Services
Conclusions TEX did all the calculation by expanding the primes macro, so the
Extra Examples author is pretty sure that the list of prime numbers given above is
quite free of typographic errors.
page.7
Andrew Mertz,
William Slough
\newif\ifprime \newif\ifunknown % boolean variables
Overview
\newcount\n \newcount\p \newcount\d \newcount\a % integer variables
A Question of Primes \def\primes#1{2,~3% assume that #1 is at least 3
Introduction to
\n=#1 \advance\n by-2 % n more to go
PythonTEX \p=5 % odd primes starting with p
Mathematics with \loop\ifnum\n>0 \printifprime\advance\p by2 \repeat}
Sympy \def\printp{, % we will invoke \printp if p is prime
Plots with matplotlib \ifnum\n=1 and~\fi % and precedes the last value
Web Services
\number\p \advance\n by -1 }
\def\printifprime{\testprimality \ifprime\printp\fi}
Conclusions
\def\testprimality{{\d=3 \global\primetrue
Extra Examples
\loop\trialdivision \ifunknown\advance\d by2 \repeat}}
\def\trialdivision{\a=\p \divide\a by\d
\ifnum\a>\d \unknowntrue\else\unknownfalse\fi
\multiply\a by\d
\ifnum\a=\p \global\primefalse\unknownfalse\fi}
page.8
Andrew Mertz,
William Slough
Overview \makeatletter
A Question of Primes \def\primes#1#2{{%
Introduction to
\def\comma{\def\comma{, }}%
PythonTEX \count@\@ne\@tempcntb#2\relax\@curtab#1\relax
Mathematics with
\@primes}}
Sympy \def\@primes{\loop\advance\count@\@ne
Plots with matplotlib \expandafter\ifx\csname p-\the\count@\endcsname\relax
Web Services
\ifnum\@tempcntb<\count@\else
\ifnum\count@<\@curtab\else\comma\the\count@\fi\fi\else\repeat
Conclusions
\@tempcnta\count@\loop\advance\@tempcnta\count@
Extra Examples \expandafter\let\csname p-\the\@tempcnta\endcsname\@ne
\ifnum\@tempcnta<\@tempcntb\repeat
\ifnum\@tempcntb>\count@\expandafter\@primes\fi}
\makeatother
page.9
Andrew Mertz,
William Slough
A solution using the \pgfmathisprime macro provided by Alain
Matthes’ tkz-euclide package:
Overview
A Question of Primes
Introduction to
PythonTEX \usepackage{tkz-euclide}
Mathematics with
Sympy \newif\ifcomma
Plots with matplotlib
Web Services
\newcommand{\primes}[2]{%
\commafalse%
Conclusions
\foreach\numb in {#1,...,#2}{%
Extra Examples
\pgfmathisprime{\numb}%
\ifnum\pgfmathresult=1
\ifcomma, \numb\else\numb\global\commatrue\fi%
\fi%
}%
}
page.10
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
PythonTEX
Yes. . .
Mathematics with
Sympy
Web Services
Conclusions
Extra Examples
page.11
Andrew Mertz,
William Slough
Mathematics with
Sympy
Web Services
Did you know that $2^{65} = \py{2**65}$?
Conclusions
Extra Examples
Andrew Mertz,
William Slough
The macro \pyc{expression} evaluates a Python expression and
Overview
typesets anything that it prints.
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Did you know that $2^{65} = \pyc{print(2**65)}$?
Plots with matplotlib
Web Services
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Web Services
Conclusions
Extra Examples
\pyc{showGoogleMap("Tokyo", 11)}
page.14
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Web Services
Conclusions
Extra Examples
Andrew Mertz,
\begin{pycode}
William Slough print(r"\begin{tabular}{c|c}")
Overview print(r"$m$ & $2^m$ \\ \hline")
A Question of Primes print(r"%d & %d \\" % (1, 2**1))
Introduction to
PythonTEX
print(r"%d & %d \\" % (2, 2**2))
Mathematics with
print(r"%d & %d \\" % (3, 2**3))
Sympy
print(r"%d & %d \\" % (4, 2**4))
Plots with matplotlib
print(r"\end{tabular}")
Web Services
\end{pycode}
Conclusions
Extra Examples
\begin{tabular}{c|c}
$m$ & $2^m$ \\ \hline
1 & 2 \\
2 & 4 \\
3 & 8 \\
4 & 16 \\
\end{tabular}
page.16
Andrew Mertz,
\begin{pycode}
William Slough print(r"\begin{tabular}{c|c}")
Overview print(r"$m$ & $2^m$ \\ \hline")
A Question of Primes print(r"%d & %d \\" % (1, 2**1))
Introduction to
PythonTEX
print(r"%d & %d \\" % (2, 2**2))
Mathematics with
print(r"%d & %d \\" % (3, 2**3))
Sympy
print(r"%d & %d \\" % (4, 2**4))
Plots with matplotlib
print(r"\end{tabular}")
Web Services
\end{pycode}
Conclusions
Extra Examples
m 2m
1 2
2 4
3 8
4 16
page.17
Conclusions
Extra Examples m 2m
1 2
2 4
3 8
4 16
5 32
6 64
page.18
Defining a Function
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
\begin{pycode}
Overview
A Question of Primes
def fib(n): # nth Fibonacci value
Introduction to
a, b = 0, 1
PythonTEX
for i in range(n):
Mathematics with
Sympy
a, b = b, a + b
Plots with matplotlib
return a
Web Services \end{pycode}
Conclusions
Extra Examples
Sessions
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
py, pyc, and pycode all have an optional session argument.
Overview
A Question of Primes
Introduction to
PythonTEX This argument determines the name of the Python session in which
Mathematics with
Sympy
the code is executed.
Plots with matplotlib
Web Services
Introducing pythontexcustomcode
A Gentle
Introduction to
PythonTEX
Conclusions
The pythontexcustomcode environment evaluates the code block
Extra Examples
at the start of each “session” – which makes it a great place to
define well-tested functions.
\begin{pythontexcustomcode}{py}
python code block
\end{pythontexcustomcode}
page.21
2m
Andrew Mertz,
William Slough m
Overview
1 2
A Question of Primes 2 4
Introduction to 3 8
PythonTEX
4 16
Mathematics with
Sympy
Conclusions
m 2m
Extra Examples 4 16
5 32
6 64
7 128
8 256
9 512
10 1024
page.22
m Fm
7 13
8 21
9 34
10 55
11 89
page.23
Introduction to
PythonTEX
\begin{pythontexcustomcode}{py}
Mathematics with
import math
Sympy
\end{pythontexcustomcode}
Plots with matplotlib
Web Services
Conclusions
\pyc{makeTableFromFunction(30, 33, math.factorial, "$m!$")}
Extra Examples
m m!
30 265252859812191058636308480000000
31 8222838654177922817725562880000000
32 263130836933693530167218012160000000
33 8683317618811886495518194401280000000
page.24
A Question of Primes This can shadow other functions and should be used with care.
Introduction to
PythonTEX
\begin{pythontexcustomcode}{py}
Mathematics with
Sympy from math import factorial
Plots with matplotlib \end{pythontexcustomcode}
Web Services
Conclusions
\pyc{makeTableFromFunction(30, 33, factorial, "$m!$")}
Extra Examples
m m!
30 265252859812191058636308480000000
31 8222838654177922817725562880000000
32 263130836933693530167218012160000000
33 8683317618811886495518194401280000000
page.25
Remember Kevin?
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough \begin{pythontexcustomcode}{py}
Overview
from sympy import prime
A Question of Primes
Extra Examples
The first 30 primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, and
113.
page.26
Andrew Mertz,
William Slough
my.tex
Overview
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Web Services
1. pdfLATEX 2. pythonTEX 3. pdfLATEX my.pdf
Conclusions
Extra Examples
my.pytxcode pythontex-files-my
page.27
Overview
>>> var("x, y") # Define symbolic variables
A Question of Primes
(x, y)
Introduction to
PythonTEX
Web Services
>>> z # Display z
Conclusions
(x + y)**3
Extra Examples
>>> latex(expand(z))
’x^{3} + 3 x^{2} y + 3 x y^{2} + y^{3}’
page.28
Expanding Binomials
A Gentle
Introduction to
PythonTEX
\begin{pycode}
Andrew Mertz, from sympy import *
William Slough
var("x, y")
Overview
3
(x + y ) = x 3 + 3x 2 y + 3xy 2 + y 3
4
(x + y ) = x 4 + 4x 3 y + 6x 2 y 2 + 4xy 3 + y 4
5
(x + y ) = x 5 + 5x 4 y + 10x 3 y 2 + 10x 2 y 3 + 5xy 4 + y 5
page.29
Web Services
d
Conclusions
sin (x) = cos (x)
Extra Examples dx
d
cos (x) = − sin (x)
dx
d
tan (x) = tan2 (x) + 1
dx
page.30
Web Services Z
Conclusions
sin (x) dx = − cos (x)
Extra Examples
Z
cos (x) dx = sin (x)
Z
1
tan (x) dx = − log sin2 (x) − 1
2
page.31
Stirling’s Triangle
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Overview
Stirling’s Triangle for Subsets
A Question of Primes
n n n n n n n n n
n
Introduction to
PythonTEX
0 1 2 3 4 5 6 7 8
Mathematics with
Sympy
0 1
Plots with matplotlib 1 0 1
Web Services 2 0 1 1
Conclusions 3 0 1 3 1
Extra Examples 4 0 1 7 6 1
5 0 1 15 25 10 1
6 0 1 31 90 65 15 1
7 0 1 63 301 350 140 21 1
8 0 1 127 966 1701 1050 266 28 1
page.32
Andrew Mertz,
William Slough
Overview
A Question of Primes
Mathematics with
Sympy for n in range(numberOfRightHandColumns):
Plots with matplotlib print("%d" % n)
Web Services for k in range(n + 1):
Conclusions print("& %d" % stirling(n, k))
Extra Examples print(r"\\")
page.33
PythonTEX
Mathematics with
Sympy
0.2 y = cos(2πt)e−t
Plots with matplotlib 0.0
Web Services
−0.2
Conclusions
−0.4
Extra Examples
−0.6
−0.8
0 1 2 3 4 5
time (s)
Andrew Mertz,
William Slough # Generate the plot with annotations
plot(t, y)
Overview
title("Damped exponential decay")
A Question of Primes
text(3, 0.15, r"$y = \cos(2 \pi t) e^{-t}$")
Introduction to
PythonTEX xlabel("time (s)")
Mathematics with ylabel("voltage (mV)")
Sympy
Extra Examples
# Include the plot in the current LaTeX document
print(r"\begin{center}")
print(r"\includegraphics[width=0.85\textwidth]{myplot.pdf}")
print(r"\end{center}")
\end{pycode}
page.36
Andrew Mertz,
William Slough
Many powerful and freely available web services can be accessed
Overview
though the libraries of Python.
A Question of Primes
Introduction to
PythonTEX Python has excellent JSON, XML and networking libraries.
Mathematics with
Sympy
Plots with matplotlib The first web service we will use is Google’s Geocoding API.
Web Services
Conclusions
A Question of Primes
def findLatlong(address):
Introduction to
PythonTEX
# Build the data needed to call the Goggle API
Mathematics with
query = {"address": address, "sensor": "false"}
Sympy data = urlencode(query)
Plots with matplotlib url = "http://maps.googleapis.com/maps/api/geocode/json?"
Web Services url += data
Conclusions
Executing a Subprocess
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
Python can run other programs and use their output.
William Slough
Mathematics with
Sympy import subprocess
Plots with matplotlib
Andrew Mertz,
William Slough
Overview
Issues
A Gentle
Introduction to
PythonTEX
I PythonTEX adds significant processing time
Andrew Mertz,
William Slough
Appropriate use of sessions can reduce this time, but
there is still a large overhead.
Overview
A Question of Primes
I Debugging Python code within TEX is difficult
Introduction to
PythonTEX Test complex Python code outside of TEX first
Mathematics with
Sympy
I TEX macros that have arguments generated by Python fail on
Plots with matplotlib
first processing step
Web Services
Conclusions
Add such TEX macros from within Python
Extra Examples
I Use parentheses for print statements: print(x).
I Be clear of the differences between \py and \pyc.
I When using Beamer use the frame option
fragile=singleslide if able.
I Be skeptical of SymPy results.
I If all else fails delete the pythontex-files folder.
page.41
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Overview
A Question of Primes
Introduction to
PythonTEX
Mathematics with
Sympy
Conclusions
Extra Examples
page.42
References
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Python: python.org
Overview
A Question of Primes
Introduction to
PythonTEX
SciPy: scipy.org
Mathematics with
Sympy
Conclusions
Extra Examples
webkit2png: github.com/adamn/python-webkit2png
page.43
Mail Merge
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough
Mathematics with
Sympy
Web Services
to,name
Conclusions
js@example.com,John Smith
Extra Examples
mw@example.com,Mike White
tb@example.com,Tom Blue
page.45
Mail Merge
A Gentle
Introduction to
PythonTEX \begin{pythontexcustomcode}{py}
Andrew Mertz, import csv
William Slough
print(r"%s\vfill" % texcommand)
\end{pythontexcustomcode}
\newcommand{\mailBody}{
Address: \mailto\\
Hello \mailname, I just wanted to say hello.
}
page.46
Mail Merge
A Gentle
Introduction to
PythonTEX
Andrew Mertz,
William Slough \pyc{mailMerge("../data.csv", r"\mailBody")}
Overview
A Question of Primes
Introduction to
Address: js@example.com
PythonTEX Hello John Smith, I just wanted to say hello.
Mathematics with
Sympy
Web Services
Address: mw@example.com
Conclusions
Hello Mike White, I just wanted to say hello.
Extra Examples
Address: tb@example.com
Hello Tom Blue, I just wanted to say hello.