summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/pythontex/pythontex_gallery.tex
blob: ded2b17747c285bea64bb75423dc6cf6895108d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
\documentclass[11pt]{article}

% Engine-specific settings
% Detect pdftex/xetex/luatex, and load appropriate font packages.
% This is inspired by the approach in the iftex package.
% pdftex:
\ifx\pdfmatch\undefined
\else
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
\fi
% xetex:
\ifx\XeTeXinterchartoks\undefined
\else
    \usepackage{fontspec}
    \defaultfontfeatures{Ligatures=TeX}
\fi
% luatex:
\ifx\directlua\undefined
\else
    \usepackage{fontspec}
\fi
% End engine-specific settings

\usepackage{amsmath,amssymb}
\usepackage{fullpage}
\usepackage{graphicx}
\usepackage[svgnames]{xcolor}
\usepackage{url}
\urlstyle{same}

\usepackage[makestderr]{pythontex}
\restartpythontexsession{\thesection}


\usepackage[framemethod=TikZ]{mdframed}

\newcommand{\pytex}{Python\TeX}
\renewcommand*{\thefootnote}{\fnsymbol{footnote}}


\title{\pytex\ Gallery}
\author{Geoffrey M.\ Poore}


\begin{document}


\maketitle


\begin{abstract}
\pytex\ allows you to run Python code from within \LaTeX\ documents and automatically include the output.  This document serves as an example of what is possible with \pytex.\footnote{Since \pytex\ runs Python code (and potentially other code) on your computer, documents using \pytex\ have a greater potential for security risks than do standard \LaTeX\ documents.  You should only compile \pytex\ documents from sources you trust.}
\end{abstract}



\section{General Python interaction}

We can typeset code that is passed to Python, and bring back the results.

This can be simple.  For example, \pyb{print('Python says hi!')} returns the following:
\begin{quote}
\printpythontex
\end{quote}

Or we could access the printed content verbatim (it might contain special characters):
\begin{quote}
\printpythontex[verb]
\end{quote}

Python interaction can also be more complex.  \pyb{print(str(2**2**2) + r'\endinput')} returns \printpythontex.  In this case, the printed result includes \LaTeX\ code, which is correctly interpreted by \LaTeX\ to ensure that there is not an extra space after the 16.  Printed output is saved to a file and brought back in via \verb|\input|, and the \verb|\endinput| command stops input immediately, before \LaTeX\ gets to the end of the line and inserts a space character there, after the 16.

Printing works, but as the last example demonstrates, you have to be careful about spacing if you have text immediately after the printed content.  In that case, it's usually best to assemble text within a \pytex\ environment and store the text in a variable.  Then you can bring in the text later, using the \pygment{latex}{\py} command.  The \pygment{latex}{\py} command brings in a string representation of its argument.  First we create the text.
\begin{pyblock}
mytext = '$1 + 1 = {0}$'.format(1 + 1)
\end{pyblock}
Then we bring it in:  \py{mytext}.  The \pygment{latex}{\py} command can even bring in verbatim content.

We don't have to typeset the code we're executing.  It can be hidden.\pyc{mystring=r'\textbf{This is a message from Python}'} And then we can access it later:  \py{mystring}.

It is also possible to perform variable substitution or string interpolation.  The earlier result could be recreated:  \pys{$1 + 1 = !{1+1}$}.


\section{Pygments highlighting}

\pytex\ supports syntax highlighting via Pygments.  Any language supported by Pygments can be highlighted.  Unicode is supported.  Consider this snippet copied and pasted from a Python 3 interactive session.  (Using random strings of Unicode for variable names is probably not a good idea, but \pytex\ will happily highlight it for you.)

\begin{pygments}{pycon}
>>> âæéöø = 123
>>> ßçñðŠ = 456
>>> âæéöø + ßçñðŠ
579
\end{pygments}

There is also a Pygments command for inline use:  \pygment{latex}{\pygment}.


\section{Python console environment}

\pytex\ includes an environment that emulates a Python interactive session.  Commands are entered within the environment, each line is treated as input to an interactive session, and the result is typeset.

\begin{pyconsole}[][frame=single]
x = 123
y = 345
z = x + y
z
def f(expr):
    return(expr**4)

f(x)
print('Python says hi from the console!')
\end{pyconsole}

It is possible to refer to the values of console variables later on in inline contexts, using the \pygment{latex}{\pycon} command.  For example, the value of $z$ was \pycon{z}.


\section{Basic SymPy interaction}

\pytex\ allows us to perform algebraic manipulations with SymPy and then properly typeset the results.

We create three variables, and define $z$ in terms of the other two.

\begin{sympyblock}
var('x, y, z')
z = x + y
\end{sympyblock}

Now we can access what $z$ is equal to:

\[z=\sympy{z}\]

Many things are possible, including some very nice calculus.

\begin{sympyblock}
f = x**3 + cos(x)**5
g = Integral(f, x)
\end{sympyblock}

\[\sympy{g}=\sympy{g.doit()}\]

It's easy to use arbitrary symbols in equations.

\begin{sympyblock}
phi = Symbol(r'\phi')
h = Integral(exp(-phi**2), (phi, 0, oo))
\end{sympyblock}

\[\sympy{h}=\sympy{h.doit()}\]



\section{Plots with matplotlib}

We can create plots with matplotlib, perfectly matching the plot fonts with the document fonts.  No more searching for the code that created a figure!

It is possible to pass page dimensions and similar contextual information from the \LaTeX\ side to the Python side.  If you want your figures to be, for example, a particular fraction of the page width, you can pass the value of \pygment{latex}{\textwidth} to the Python side, and use it in creating your figures.  See \pygment{latex}{\setpythontexcontext} in the main documentation for details.

You may want to use matplotlib's PGF backend when creating plots.

\begin{pylabblock}
rc('text', usetex=True)
rc('font', family='serif')
rc('font', size=10.0)
rc('legend', fontsize=10.0)
rc('font', weight='normal')
x = linspace(0, 10)
figure(figsize=(4, 2.5))
plot(x, sin(x), label='$\sin(x)$')
xlabel(r'$x\mathrm{-axis}$')
ylabel(r'$y\mathrm{-axis}$')
legend(loc='lower right')
savefig('myplot.pdf', bbox_inches='tight')
\end{pylabblock}

\begin{center}
\includegraphics{myplot.pdf}
\end{center}


\section{Basic pylab interaction}

\begin{pylabblock}
from scipy.integrate import quad
myintegral = quad(lambda x: e**-x**2, 0, inf)[0]
\end{pylabblock}

\[ \int_0^\infty e^{-x^2}\,dx = \pylab{myintegral} \]


\section{An automated derivative and integral table}

\pytex\ allows some amazing document automation, such as this derivative and integral table.  Try typing that by hand, fast!

\begin{sympyblock}[][numbers=left,frame=single,framesep=5mm,label=An Automated Derivative and Integral Table]
from re import sub

var('x')

# Create a list of functions to include in the table
funcs = ['sin(x)', 'cos(x)', 'tan(x)', 
         'sin(x)**2', 'cos(x)**2', 'tan(x)**2', 
         'asin(x)', 'acos(x)', 'atan(x)', 
         'sinh(x)', 'cosh(x)', 'tanh(x)']

print(r'\begin{align*}')

for func in funcs:
    # Put in some vertical space when switching to arc and hyperbolic funcs
    if func == 'asin(x)' or func == 'sinh(x)':
        print(r'&\\')
    myderiv = 'Derivative(' + func + ', x)'
    myint = 'Integral(' + func + ', x)'
    print(latex(eval(myderiv)) + '&=' +
            latex(eval(myderiv + '.doit()')) + r'\quad & \quad')
    print(latex(eval(myint)) + '&=' +
            latex(eval(myint+'.doit()')) + r'\\')
print(r'\end{align*}')
\end{sympyblock}

\printpythontex


\section{Step-by-step solutions}

Using SymPy, it is possible to typeset step-by-step solutions.  In this particular case, we also use the \verb|mdframed| package to place a colored background behind our code.

\begin{mdframed}[linecolor=Green,innerrightmargin=30pt,innerleftmargin=30pt,leftmargin=-30pt,rightmargin=-30pt,backgroundcolor=Black!5,skipabove=10pt,skipbelow=10pt,roundcorner=5pt,frametitle={Step-by-Step Integral Evaluation},frametitlealignment=\center,splitbottomskip=6pt,splittopskip=12pt]
\begin{sympyblock}[][numbers=left]
x, y, z = symbols('x,y,z')
f = Symbol('f(x,y,z)')

# Define limits of integration
x_llim = 0
x_ulim = 2
y_llim = 0
y_ulim = 3
z_llim = 0
z_ulim = 4

print(r'\begin{align*}')

# Notice how I define f as a symbol, then later as an actual function
left = Integral(f, (x, x_llim, x_ulim), (y, y_llim, y_ulim), (z, z_llim, z_ulim))
f = x*y + y*sin(z) + cos(x+y)
right = Integral(f, (x, x_llim, x_ulim), (y, y_llim, y_ulim), (z, z_llim, z_ulim))
print(latex(left) + '&=' + latex(right) + r'\\')

# For each step, I move limits from an outer integral to an inner, evaluated 
# integral until the outer integral is no longer needed
right = Integral(Integral(f, (z, z_llim, z_ulim)).doit(), (x, x_llim, x_ulim),
                 (y, y_llim, y_ulim))
print('&=' + latex(right) + r'\\')

right = Integral(Integral(f, (z, z_llim, z_ulim), (y, y_llim, y_ulim)).doit(),
                 (x, x_llim, x_ulim))
print('&=' + latex(right) + r'\\')

right = Integral(f, (z, z_llim, z_ulim), (y, y_llim, y_ulim), 
                 (x, x_llim, x_ulim)).doit()
print('&=' + latex(right) + r'\\')

print('&=' + latex(N(right)) + r'\\')

print(r'\end{align*}')
\end{sympyblock}
\end{mdframed}

\printpythontex


\section{Including stderr}

\pytex\ allows code to be typset next to the stderr it produces.  This requires the package option \verb|makestderr|.

\begin{pyblock}[errorsession][numbers=left]
x = 123
y = 345
z = x + y +
\end{pyblock}

This code causes a syntax error:

\stderrpythontex[verbatim][frame=single]

The package option \verb|stderrfilename| allows the file name that appears in the error message to be customized.




\end{document}