forked from gpoore/pythontex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythontex_gallery.html
More file actions
242 lines (230 loc) · 20.7 KB
/
pythontex_gallery.html
File metadata and controls
242 lines (230 loc) · 20.7 KB
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Geoffrey M. Poore" />
<title>PythonTeX Gallery</title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML-full" type="text/javascript"></script>
</head>
<body>
<div id="header">
<h1 class="title"><span>PythonTeX</span> Gallery</h1>
<h2 class="author">Geoffrey M. Poore</h2>
</div>
<h1 id="general-python-interaction">General Python interaction</h1>
<p>We can typeset code that is passed to Python, and bring back the results.</p>
<p>This can be simple. For example, <code>print('Python says hi!')</code> returns the following:</p>
<blockquote>
<p>Python says hi!</p>
</blockquote>
<p>Or we could access the printed content verbatim (it might contain special characters):</p>
<blockquote>
<p><code>Python says hi!</code></p>
</blockquote>
<p>Python interaction can also be more complex. <code>print(str(2**2**2) + r'\endinput')</code> returns 16. 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 <code>\input</code>, and the <code>\endinput</code> command stops input immediately, before LaTeX gets to the end of the line and inserts a space character there, after the 16.</p>
<p>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 <span>PythonTeX</span> environment and store the text in a variable. Then you can bring in the text later, using the <code>\py</code> command. The <code>\py</code> command brings in a string representation of its argument. First we create the text.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">mytext <span class="op">=</span> <span class="st">'$1 + 1 = </span><span class="sc">{0}</span><span class="st">$'</span>.<span class="bu">format</span>(<span class="dv">1</span> <span class="op">+</span> <span class="dv">1</span>)</code></pre></div>
<p>Then we bring it in: <span class="math inline">\(1 + 1 = 2\)</span>. The <code>\py</code> command can even bring in verbatim content.</p>
<p>We don’t have to typeset the code we’re executing. It can be hidden. And then we can access it later: <strong>This is a message from Python</strong>.</p>
<p>It is also possible to perform variable substitution or string interpolation. The earlier result could be recreated: <span class="math inline">\(1 + 1 = 2\)</span>.</p>
<h1 id="pygments-highlighting">Pygments highlighting</h1>
<p><span>PythonTeX</span> 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 <span>PythonTeX</span> will happily highlight it for you.)</p>
<pre class="pycon"><code>>>> âæéöø = 123
>>> ßçñðŠ = 456
>>> âæéöø + ßçñðŠ
579</code></pre>
<p>There is also a Pygments command for inline use: <code>\pygment</code>.</p>
<h1 id="python-console-environment">Python console environment</h1>
<p><span>PythonTeX</span> 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.</p>
<pre class="pycon"><code>>>> x = 123
>>> y = 345
>>> z = x + y
>>> z
468
>>> def f(expr):
... return(expr**4)
...
>>> f(x)
228886641
>>> print('Python says hi from the console!')
Python says hi from the console!</code></pre>
<p>It is possible to refer to the values of console variables later on in inline contexts, using the <code>\pycon</code> command. For example, the value of <span class="math inline">\(z\)</span> was <code>468</code>.</p>
<h1 id="basic-sympy-interaction">Basic SymPy interaction</h1>
<p><span>PythonTeX</span> allows us to perform algebraic manipulations with SymPy and then properly typeset the results.</p>
<p>We create three variables, and define <span class="math inline">\(z\)</span> in terms of the other two.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">var(<span class="st">'x, y, z'</span>)
z <span class="op">=</span> x <span class="op">+</span> y</code></pre></div>
<p>Now we can access what <span class="math inline">\(z\)</span> is equal to:</p>
<p><span class="math display">\[z=x + y\]</span></p>
<p>Many things are possible, including some very nice calculus.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">f <span class="op">=</span> x<span class="op">**</span><span class="dv">3</span> <span class="op">+</span> cos(x)<span class="op">**</span><span class="dv">5</span>
g <span class="op">=</span> Integral(f, x)</code></pre></div>
<p><span class="math display">\[\int x^{3} + \cos^{5}{\left (x \right )}\, dx=\frac{x^{4}}{4} + \frac{1}{5} \sin^{5}{\left (x \right )} - \frac{2}{3} \sin^{3}{\left (x \right )} + \sin{\left (x \right )}\]</span></p>
<p>It’s easy to use arbitrary symbols in equations.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">phi <span class="op">=</span> Symbol(<span class="vs">r'\phi'</span>)
h <span class="op">=</span> Integral(exp(<span class="op">-</span>phi<span class="op">**</span><span class="dv">2</span>), (phi, <span class="dv">0</span>, oo))</code></pre></div>
<p><span class="math display">\[\int_{0}^{\infty} e^{- \phi^{2}}\, d\phi=\frac{\sqrt{\pi}}{2}\]</span></p>
<h1 id="plots-with-matplotlib">Plots with matplotlib</h1>
<p>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!</p>
<p>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 <code>\textwidth</code> to the Python side, and use it in creating your figures. See <code>\setpythontexcontext</code> in the main documentation for details.</p>
<p>You may want to use matplotlib’s PGF backend when creating plots.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">rc(<span class="st">'text'</span>, usetex<span class="op">=</span><span class="va">True</span>)
rc(<span class="st">'font'</span>, family<span class="op">=</span><span class="st">'serif'</span>)
rc(<span class="st">'font'</span>, size<span class="op">=</span><span class="fl">10.0</span>)
rc(<span class="st">'legend'</span>, fontsize<span class="op">=</span><span class="fl">10.0</span>)
rc(<span class="st">'font'</span>, weight<span class="op">=</span><span class="st">'normal'</span>)
x <span class="op">=</span> linspace(<span class="dv">0</span>, <span class="dv">10</span>)
figure(figsize<span class="op">=</span>(<span class="dv">4</span>, <span class="fl">2.5</span>))
plot(x, sin(x), label<span class="op">=</span><span class="st">'$\sin(x)$'</span>)
xlabel(<span class="vs">r'$x\mathrm{-axis}$'</span>)
ylabel(<span class="vs">r'$y\mathrm{-axis}$'</span>)
legend(loc<span class="op">=</span><span class="st">'lower right'</span>)
savefig(<span class="st">'myplot.png'</span>, bbox_inches<span class="op">=</span><span class="st">'tight'</span>)</code></pre></div>
<p><img src="myplot.png" alt="image" /></p>
<h1 id="basic-pylab-interaction">Basic pylab interaction</h1>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> scipy.integrate <span class="im">import</span> quad
myintegral <span class="op">=</span> quad(<span class="kw">lambda</span> x: e<span class="op">**-</span>x<span class="op">**</span><span class="dv">2</span>, <span class="dv">0</span>, inf)[<span class="dv">0</span>]</code></pre></div>
<p><span class="math display">\[\int_0^\infty e^{-x^2}\,dx = 0.886226925452758\]</span></p>
<h1 id="an-automated-derivative-and-integral-table">An automated derivative and integral table</h1>
<p><span>PythonTeX</span> allows some amazing document automation, such as this derivative and integral table. Try typing that by hand, fast!</p>
<div class="sourceCode" linenos="" startFrom="1"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> re <span class="im">import</span> sub
var(<span class="st">'x'</span>)
<span class="co"># Create a list of functions to include in the table</span>
funcs <span class="op">=</span> [<span class="st">'sin(x)'</span>, <span class="st">'cos(x)'</span>, <span class="st">'tan(x)'</span>,
<span class="st">'sin(x)**2'</span>, <span class="st">'cos(x)**2'</span>, <span class="st">'tan(x)**2'</span>,
<span class="st">'asin(x)'</span>, <span class="st">'acos(x)'</span>, <span class="st">'atan(x)'</span>,
<span class="st">'sinh(x)'</span>, <span class="st">'cosh(x)'</span>, <span class="st">'tanh(x)'</span>]
<span class="bu">print</span>(<span class="vs">r'\begin{align*}'</span>)
<span class="cf">for</span> func <span class="kw">in</span> funcs:
<span class="co"># Put in some vertical space when switching to arc and hyperbolic funcs</span>
<span class="cf">if</span> func <span class="op">==</span> <span class="st">'asin(x)'</span> <span class="kw">or</span> func <span class="op">==</span> <span class="st">'sinh(x)'</span>:
<span class="bu">print</span>(<span class="vs">r'&\\'</span>)
myderiv <span class="op">=</span> <span class="st">'Derivative('</span> <span class="op">+</span> func <span class="op">+</span> <span class="st">', x)'</span>
myint <span class="op">=</span> <span class="st">'Integral('</span> <span class="op">+</span> func <span class="op">+</span> <span class="st">', x)'</span>
<span class="bu">print</span>(latex(<span class="bu">eval</span>(myderiv)) <span class="op">+</span> <span class="st">'&='</span> <span class="op">+</span>
latex(<span class="bu">eval</span>(myderiv <span class="op">+</span> <span class="st">'.doit()'</span>)) <span class="op">+</span> <span class="vs">r'\quad & \quad'</span>)
<span class="bu">print</span>(latex(<span class="bu">eval</span>(myint)) <span class="op">+</span> <span class="st">'&='</span> <span class="op">+</span>
latex(<span class="bu">eval</span>(myint<span class="op">+</span><span class="st">'.doit()'</span>)) <span class="op">+</span> <span class="vs">r'\\'</span>)
<span class="bu">print</span>(<span class="vs">r'\end{align*}'</span>)</code></pre></div>
<p><span class="math display">\[\begin{aligned}
\frac{d}{d x} \sin{\left (x \right )}&=\cos{\left (x \right )}\quad & \quad
\int \sin{\left (x \right )}\, dx&=- \cos{\left (x \right )}\\
\frac{d}{d x} \cos{\left (x \right )}&=- \sin{\left (x \right )}\quad & \quad
\int \cos{\left (x \right )}\, dx&=\sin{\left (x \right )}\\
\frac{d}{d x} \tan{\left (x \right )}&=\tan^{2}{\left (x \right )} + 1\quad & \quad
\int \tan{\left (x \right )}\, dx&=- \frac{1}{2} \log{\left (\sin^{2}{\left (x \right )} - 1 \right )}\\
\frac{d}{d x} \sin^{2}{\left (x \right )}&=2 \sin{\left (x \right )} \cos{\left (x \right )}\quad & \quad
\int \sin^{2}{\left (x \right )}\, dx&=\frac{x}{2} - \frac{1}{2} \sin{\left (x \right )} \cos{\left (x \right )}\\
\frac{d}{d x} \cos^{2}{\left (x \right )}&=- 2 \sin{\left (x \right )} \cos{\left (x \right )}\quad & \quad
\int \cos^{2}{\left (x \right )}\, dx&=\frac{x}{2} + \frac{1}{2} \sin{\left (x \right )} \cos{\left (x \right )}\\
\frac{d}{d x} \tan^{2}{\left (x \right )}&=\left(2 \tan^{2}{\left (x \right )} + 2\right) \tan{\left (x \right )}\quad & \quad
\int \tan^{2}{\left (x \right )}\, dx&=- x + \frac{\sin{\left (x \right )}}{\cos{\left (x \right )}}\\
&\\
\frac{d}{d x} \operatorname{asin}{\left (x \right )}&=\frac{1}{\sqrt{- x^{2} + 1}}\quad & \quad
\int \operatorname{asin}{\left (x \right )}\, dx&=x \operatorname{asin}{\left (x \right )} + \sqrt{- x^{2} + 1}\\
\frac{d}{d x} \operatorname{acos}{\left (x \right )}&=- \frac{1}{\sqrt{- x^{2} + 1}}\quad & \quad
\int \operatorname{acos}{\left (x \right )}\, dx&=x \operatorname{acos}{\left (x \right )} - \sqrt{- x^{2} + 1}\\
\frac{d}{d x} \operatorname{atan}{\left (x \right )}&=\frac{1}{x^{2} + 1}\quad & \quad
\int \operatorname{atan}{\left (x \right )}\, dx&=x \operatorname{atan}{\left (x \right )} - \frac{1}{2} \log{\left (x^{2} + 1 \right )}\\
&\\
\frac{d}{d x} \sinh{\left (x \right )}&=\cosh{\left (x \right )}\quad & \quad
\int \sinh{\left (x \right )}\, dx&=\cosh{\left (x \right )}\\
\frac{d}{d x} \cosh{\left (x \right )}&=\sinh{\left (x \right )}\quad & \quad
\int \cosh{\left (x \right )}\, dx&=\sinh{\left (x \right )}\\
\frac{d}{d x} \tanh{\left (x \right )}&=- \tanh^{2}{\left (x \right )} + 1\quad & \quad
\int \tanh{\left (x \right )}\, dx&=x - \log{\left (\tanh{\left (x \right )} + 1 \right )}\\\end{aligned}\]</span></p>
<h1 id="step-by-step-solutions">Step-by-step solutions</h1>
<p>Using SymPy, it is possible to typeset step-by-step solutions. In this particular case, we also use the <code>mdframed</code> package to place a colored background behind our code.</p>
<div class="sourceCode" linenos="" startFrom="1"><pre class="sourceCode python"><code class="sourceCode python">x, y, z <span class="op">=</span> symbols(<span class="st">'x,y,z'</span>)
f <span class="op">=</span> Symbol(<span class="st">'f(x,y,z)'</span>)
<span class="co"># Define limits of integration</span>
x_llim <span class="op">=</span> <span class="dv">0</span>
x_ulim <span class="op">=</span> <span class="dv">2</span>
y_llim <span class="op">=</span> <span class="dv">0</span>
y_ulim <span class="op">=</span> <span class="dv">3</span>
z_llim <span class="op">=</span> <span class="dv">0</span>
z_ulim <span class="op">=</span> <span class="dv">4</span>
<span class="bu">print</span>(<span class="vs">r'\begin{align*}'</span>)
<span class="co"># Notice how I define f as a symbol, then later as an actual function</span>
left <span class="op">=</span> Integral(f, (x, x_llim, x_ulim), (y, y_llim, y_ulim), (z, z_llim, z_ulim))
f <span class="op">=</span> x<span class="op">*</span>y <span class="op">+</span> y<span class="op">*</span>sin(z) <span class="op">+</span> cos(x<span class="op">+</span>y)
right <span class="op">=</span> Integral(f, (x, x_llim, x_ulim), (y, y_llim, y_ulim), (z, z_llim, z_ulim))
<span class="bu">print</span>(latex(left) <span class="op">+</span> <span class="st">'&='</span> <span class="op">+</span> latex(right) <span class="op">+</span> <span class="vs">r'\\'</span>)
<span class="co"># For each step, I move limits from an outer integral to an inner, evaluated </span>
<span class="co"># integral until the outer integral is no longer needed</span>
right <span class="op">=</span> Integral(Integral(f, (z, z_llim, z_ulim)).doit(), (x, x_llim, x_ulim),
(y, y_llim, y_ulim))
<span class="bu">print</span>(<span class="st">'&='</span> <span class="op">+</span> latex(right) <span class="op">+</span> <span class="vs">r'\\'</span>)
right <span class="op">=</span> Integral(Integral(f, (z, z_llim, z_ulim), (y, y_llim, y_ulim)).doit(),
(x, x_llim, x_ulim))
<span class="bu">print</span>(<span class="st">'&='</span> <span class="op">+</span> latex(right) <span class="op">+</span> <span class="vs">r'\\'</span>)
right <span class="op">=</span> Integral(f, (z, z_llim, z_ulim), (y, y_llim, y_ulim),
(x, x_llim, x_ulim)).doit()
<span class="bu">print</span>(<span class="st">'&='</span> <span class="op">+</span> latex(right) <span class="op">+</span> <span class="vs">r'\\'</span>)
<span class="bu">print</span>(<span class="st">'&='</span> <span class="op">+</span> latex(N(right)) <span class="op">+</span> <span class="vs">r'\\'</span>)
<span class="bu">print</span>(<span class="vs">r'\end{align*}'</span>)</code></pre></div>
<p><span class="math display">\[\begin{aligned}
\int_{0}^{4}\int_{0}^{3}\int_{0}^{2} f(x,y,z)\, dx\, dy\, dz&=\int_{0}^{4}\int_{0}^{3}\int_{0}^{2} x y + y \sin{\left (z \right )} + \cos{\left (x + y \right )}\, dx\, dy\, dz\\
&=\int_{0}^{3}\int_{0}^{2} 4 x y - y \cos{\left (4 \right )} + y + 4 \cos{\left (x + y \right )}\, dx\, dy\\
&=\int_{0}^{2} 18 x - 4 \sin{\left (x \right )} + 4 \sin{\left (x + 3 \right )} - \frac{9}{2} \cos{\left (4 \right )} + \frac{9}{2}\, dx\\
&=4 \cos{\left (3 \right )} + 4 \cos{\left (2 \right )} - 4 \cos{\left (5 \right )} - 9 \cos{\left (4 \right )} + 41\\
&=40.1235865133293\\\end{aligned}\]</span></p>
<h1 id="including-stderr">Including stderr</h1>
<p><span>PythonTeX</span> allows code to be typset next to the stderr it produces. This requires the package option <code>makestderr</code>.</p>
<div class="sourceCode" linenos="" startFrom="1"><pre class="sourceCode python"><code class="sourceCode python">x <span class="op">=</span> <span class="dv">123</span>
y <span class="op">=</span> <span class="dv">345</span>
z <span class="op">=</span> x <span class="op">+</span> y <span class="op">+</span></code></pre></div>
<p>This code causes a syntax error:</p>
<pre><code> File "py_errorsession_9.py", line 3
z = x + y +
^
SyntaxError: invalid syntax</code></pre>
<p>The package option <code>stderrfilename</code> allows the file name that appears in the error message to be customized.</p>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>Since <span>PythonTeX</span> runs Python code (and potentially other code) on your computer, documents using <span>PythonTeX</span> have a greater potential for security risks than do standard LaTeX documents. You should only compile <span>PythonTeX</span> documents from sources you trust.<a href="#fnref1">↩</a></p></li>
</ol>
</div>
</body>
</html>