Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: sagemathinc/cocalc-example-files
Path: blob/master/latex/pythontex/pythontex.tex
Views: 989
1
% Showcasing PythonTeX
2
% Some examples are taken from
3
% http://mirrors.ctan.org/macros/latex/contrib/pythontex/pythontex_gallery.pdf
4
5
\documentclass{scrartcl}
6
7
% pdflatex
8
\ifx\pdfmatch\undefined
9
\else
10
\usepackage[T1]{fontenc}
11
\usepackage[utf8]{inputenc}
12
\fi
13
% xetex:
14
\ifx\XeTeXinterchartoks\undefined
15
\else
16
\usepackage{fontspec}
17
\defaultfontfeatures{Ligatures=TeX}
18
\fi
19
% luatex:
20
\ifx\directlua\undefined
21
\else
22
\usepackage{fontspec}
23
\fi
24
% End engine-specific settings
25
26
\usepackage[parfill]{parskip}
27
\usepackage{amsmath,amssymb}
28
\usepackage{fullpage}
29
\usepackage{graphicx}
30
\usepackage[svgnames]{xcolor}
31
\usepackage{url}
32
\urlstyle{same}
33
34
\usepackage[makestderr]{pythontex}
35
\restartpythontexsession{\thesection}
36
37
\newcommand{\pytex}{Python\TeX}
38
\title{\pytex\ in CoCalc}
39
\author{Name of Author}
40
41
\begin{document}
42
43
\maketitle
44
45
CoCalc supports running \pytex\ automatically for documents that load the \verb|pythontex| package.
46
It executes it using the \verb|pythontex3| engine for Python 3.
47
48
\section{\pytex\ Examples}
49
50
\begin{pycode}
51
import math
52
print(r'Pi = {}'.format(math.pi))
53
\end{pycode}
54
55
A huge number is \py{2*3**45 - 1}.
56
57
And a sum of unicode characters: \py{"ä"+"µ"+"ß"}.
58
59
\subsection{A simple formula}
60
61
$\frac{3}{\pi} = \py{3 / math.pi}$.
62
63
\subsection{Native SymPy Support}
64
65
Via the \verb|sympy| command and inside a \verb|sympyblock|.
66
67
\begin{sympyblock}
68
var('x, y, z')
69
z = x + y
70
\end{sympyblock}
71
72
Now we can access what $z$ is equal to:
73
74
\[z=\sympy{z}\]
75
76
Many things are possible, including some very nice calculus.
77
First, define the integral expression $g$ and then us \verb|g.doit()| to run it inside a \LaTeX\ formula.
78
79
\begin{sympyblock}
80
f = x**3 + cos(x)**5
81
g = Integral(f, x)
82
\end{sympyblock}
83
84
\[\sympy{g}=\sympy{g.doit()}\]
85
86
It's easy to use arbitrary symbols in equations.
87
88
\begin{sympyblock}
89
phi = Symbol(r'\phi')
90
h = Integral(exp(-phi**2), (phi, 0, oo))
91
\end{sympyblock}
92
93
\[\sympy{h}=\sympy{h.doit()}\]
94
95
\pagebreak
96
97
\subsection{Code block with a plot}
98
99
The session is \verb|pycode| to be distinct from the \verb|default| session.
100
This allows \pytex\ to speed up the execution by running the sympy code and the plot below in parallel.
101
102
\begin{pycode}[pycode]
103
from pylab import *
104
# Define f(t), the desired function to plot
105
def f(t):
106
return cos(2 * pi * exp(t)) * exp(-t)
107
# Generate the points (t_i, y_i) to plot
108
t = linspace(0, 4, 1000)
109
y = f(t)
110
# Begin with an empty plot, 5 x 3 inches
111
clf()
112
figure(figsize=(5, 3))
113
# Use TeX fonts
114
rc("text", usetex=True)
115
# Generate the plot with annotations
116
plot(t, y)
117
title("Damped exponential decay")
118
text(2.5, 0.4, r"$y = \cos(2 \pi \exp(t)) e^{-t}$")
119
xlabel("time (s)")
120
ylabel("voltage (mV)")
121
# Save the plot as a PDF file
122
savefig("myplot.pdf", bbox_inches="tight")
123
# Include the plot in the current LaTeX document
124
print(r"\begin{center}")
125
print(r"\includegraphics[width=0.85\textwidth]{myplot.pdf}")
126
print(r"\end{center}")
127
\end{pycode}
128
129
\section{Error Handling}
130
131
CoCalc also displays the first processing error next to the line \pytex\ reports.
132
The example below should have such a ``bug'' indicator.
133
134
\pytex\ also allows code to be typeset next to the stderr it produces.
135
This requires the package option \verb|makestderr|.
136
137
\begin{pyblock}[errorsession][numbers=left]
138
x = 123
139
y = 345
140
z = x + y +
141
\end{pyblock}
142
143
This code causes a syntax error:
144
145
\stderrpythontex[verbatim][frame=single]
146
147
\section{Learn more}
148
149
Package documentation: \url{https://ctan.org/pkg/pythontex}
150
151
\end{document}
152
153