-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpythontex.tex
151 lines (113 loc) · 3.39 KB
/
pythontex.tex
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
% Showcasing PythonTeX
% Some examples are taken from
% http://mirrors.ctan.org/macros/latex/contrib/pythontex/pythontex_gallery.pdf
\documentclass{scrartcl}
% pdflatex
\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[parfill]{parskip}
\usepackage{amsmath,amssymb}
\usepackage{fullpage}
\usepackage{graphicx}
\usepackage[svgnames]{xcolor}
\usepackage{url}
\urlstyle{same}
\usepackage[makestderr]{pythontex}
\restartpythontexsession{\thesection}
\newcommand{\pytex}{Python\TeX}
\title{\pytex\ in CoCalc}
\author{Name of Author}
\begin{document}
\maketitle
CoCalc supports running \pytex\ automatically for documents that load the \verb|pythontex| package.
It executes it using the \verb|pythontex3| engine for Python 3.
\section{\pytex\ Examples}
\begin{pycode}
import math
print(r'Pi = {}'.format(math.pi))
\end{pycode}
A huge number is \py{2*3**45 - 1}.
And a sum of unicode characters: \py{"ä"+"µ"+"ß"}.
\subsection{A simple formula}
$\frac{3}{\pi} = \py{3 / math.pi}$.
\subsection{Native SymPy Support}
Via the \verb|sympy| command and inside a \verb|sympyblock|.
\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.
First, define the integral expression $g$ and then us \verb|g.doit()| to run it inside a \LaTeX\ formula.
\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()}\]
\pagebreak
\subsection{Code block with a plot}
The session is \verb|pycode| to be distinct from the \verb|default| session.
This allows \pytex\ to speed up the execution by running the sympy code and the plot below in parallel.
\begin{pycode}[pycode]
from pylab import *
# Define f(t), the desired function to plot
def f(t):
return cos(2 * pi * exp(t)) * exp(-t)
# Generate the points (t_i, y_i) to plot
t = linspace(0, 4, 1000)
y = f(t)
# Begin with an empty plot, 5 x 3 inches
clf()
figure(figsize=(5, 3))
# Use TeX fonts
rc("text", usetex=True)
# Generate the plot with annotations
plot(t, y)
title("Damped exponential decay")
text(2.5, 0.4, r"$y = \cos(2 \pi \exp(t)) e^{-t}$")
xlabel("time (s)")
ylabel("voltage (mV)")
# Save the plot as a PDF file
savefig("myplot.pdf", bbox_inches="tight")
# 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}
\section{Error Handling}
CoCalc also displays the first processing error next to the line \pytex\ reports.
The example below should have such a ``bug'' indicator.
\pytex\ also allows code to be typeset 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]
\section{Learn more}
Package documentation: \url{https://ctan.org/pkg/pythontex}
\end{document}