Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/latex/pythontex/pythontex.tex
Views: 989
% Showcasing PythonTeX1% Some examples are taken from2% http://mirrors.ctan.org/macros/latex/contrib/pythontex/pythontex_gallery.pdf34\documentclass{scrartcl}56% pdflatex7\ifx\pdfmatch\undefined8\else9\usepackage[T1]{fontenc}10\usepackage[utf8]{inputenc}11\fi12% xetex:13\ifx\XeTeXinterchartoks\undefined14\else15\usepackage{fontspec}16\defaultfontfeatures{Ligatures=TeX}17\fi18% luatex:19\ifx\directlua\undefined20\else21\usepackage{fontspec}22\fi23% End engine-specific settings2425\usepackage[parfill]{parskip}26\usepackage{amsmath,amssymb}27\usepackage{fullpage}28\usepackage{graphicx}29\usepackage[svgnames]{xcolor}30\usepackage{url}31\urlstyle{same}3233\usepackage[makestderr]{pythontex}34\restartpythontexsession{\thesection}3536\newcommand{\pytex}{Python\TeX}37\title{\pytex\ in CoCalc}38\author{Name of Author}3940\begin{document}4142\maketitle4344CoCalc supports running \pytex\ automatically for documents that load the \verb|pythontex| package.45It executes it using the \verb|pythontex3| engine for Python 3.4647\section{\pytex\ Examples}4849\begin{pycode}50import math51print(r'Pi = {}'.format(math.pi))52\end{pycode}5354A huge number is \py{2*3**45 - 1}.5556And a sum of unicode characters: \py{"ä"+"µ"+"ß"}.5758\subsection{A simple formula}5960$\frac{3}{\pi} = \py{3 / math.pi}$.6162\subsection{Native SymPy Support}6364Via the \verb|sympy| command and inside a \verb|sympyblock|.6566\begin{sympyblock}67var('x, y, z')68z = x + y69\end{sympyblock}7071Now we can access what $z$ is equal to:7273\[z=\sympy{z}\]7475Many things are possible, including some very nice calculus.76First, define the integral expression $g$ and then us \verb|g.doit()| to run it inside a \LaTeX\ formula.7778\begin{sympyblock}79f = x**3 + cos(x)**580g = Integral(f, x)81\end{sympyblock}8283\[\sympy{g}=\sympy{g.doit()}\]8485It's easy to use arbitrary symbols in equations.8687\begin{sympyblock}88phi = Symbol(r'\phi')89h = Integral(exp(-phi**2), (phi, 0, oo))90\end{sympyblock}9192\[\sympy{h}=\sympy{h.doit()}\]9394\pagebreak9596\subsection{Code block with a plot}9798The session is \verb|pycode| to be distinct from the \verb|default| session.99This allows \pytex\ to speed up the execution by running the sympy code and the plot below in parallel.100101\begin{pycode}[pycode]102from pylab import *103# Define f(t), the desired function to plot104def f(t):105return cos(2 * pi * exp(t)) * exp(-t)106# Generate the points (t_i, y_i) to plot107t = linspace(0, 4, 1000)108y = f(t)109# Begin with an empty plot, 5 x 3 inches110clf()111figure(figsize=(5, 3))112# Use TeX fonts113rc("text", usetex=True)114# Generate the plot with annotations115plot(t, y)116title("Damped exponential decay")117text(2.5, 0.4, r"$y = \cos(2 \pi \exp(t)) e^{-t}$")118xlabel("time (s)")119ylabel("voltage (mV)")120# Save the plot as a PDF file121savefig("myplot.pdf", bbox_inches="tight")122# Include the plot in the current LaTeX document123print(r"\begin{center}")124print(r"\includegraphics[width=0.85\textwidth]{myplot.pdf}")125print(r"\end{center}")126\end{pycode}127128\section{Error Handling}129130CoCalc also displays the first processing error next to the line \pytex\ reports.131The example below should have such a ``bug'' indicator.132133\pytex\ also allows code to be typeset next to the stderr it produces.134This requires the package option \verb|makestderr|.135136\begin{pyblock}[errorsession][numbers=left]137x = 123138y = 345139z = x + y +140\end{pyblock}141142This code causes a syntax error:143144\stderrpythontex[verbatim][frame=single]145146\section{Learn more}147148Package documentation: \url{https://ctan.org/pkg/pythontex}149150\end{document}151152153