Tb125ziegenhagen Python
Tb125ziegenhagen Python
Uwe Ziegenhagen
TUGboat, Volume 40 (2019), No. 2 127
\usepackage{minted} \begin{tabular}{lr}
\setminted[python]{frame=lines, framesep=2mm, \toprule
baselinestretch=1.2, bgcolor=colBack, Company & Latest quote \\
fontsize=\footnotesize, linenos} \midrule
\setminted[text]{frame=lines, framesep=2mm, Apple & \py{round(si.get_live_price("aapl"),2)} \\
baselinestretch=1.2, bgcolor=colBack, Amazon & \py{round(si.get_live_price("amzn"),2)} \\
fontsize=\footnotesize, linenos} Facebook & \py{round(si.get_live_price("fb"),2)} \\
\bottomrule
\usepackage{fancyvrb} \end{tabular}
\makeatletter \end{document}
\newenvironment{pycode}[1]%
{\xdef\d@tn@me{#1}% Listing 11: Using pythontex to retrieve stock prices
\xdef\r@ncmd{python #1.py > #1.plog}%
\typeout{Writing file #1}%
\VerbatimOut{#1.py}%
}%
{\endVerbatimOut %
Company Latest quote
\toks0{\immediate\write18}%
\expandafter\toks\expandafter1%
\expandafter{\r@ncmd}%
Apple 203.43
\edef\d@r@ncmd{\the\toks0{\the\toks1}}%
\d@r@ncmd
Amazon 1832.89
\noindent Input
\inputminted{python}{\d@[email protected]}%
Facebook 190.16
\noindent Output
\inputminted{text}{\d@[email protected]}%
}%
\makeatother Figure 1: Output resulting from Listing 11
Listing 10: The pycode environment The pythontex package provides many more fea-
tures, among them even symbolic computation. It
The pythontex package [2] uses a more advanced
can thus be highly recommended.
approach: it can detect if the Python code has been
edited or not. Only if an edit took place is the 4 Summary
Python code rerun, thus saving time especially with We have shown how easy LATEX documents can be
more complicated Python code. The workflow is the enriched by Python, a scripting language that is easy
following: first the LATEX engine of your choice is to learn and fun to work with. Accompanying this
run, followed by the pythontex executable, followed article is the more extensive presentation held at
by another latex run. The package offers various TUG 2019, for which the interested reader is directed
LATEX commands and corresponding environments; to the slides at www.uweziegenhagen.de.
see the package documentation.
Let us show with an example (Listing 11) how References
the package can be applied. After loading the pack- [1] M. Lutz. Learning Python. O’Reilly, 2013.
age pythontex we use the \pyc command, which [2] G. M. Poore. PythonTEX: Reproducible
only executes code and does not typeset it, for the documents with LATEX, Python, and
first line of Python code. Here we instruct Python more. Comput. Sci. Disc. 8(1), 2015.
to load a function from the yahoo_fin library which ctan.org/pkg/pythontex
allows us to retrieve stock information from Yahoo,
given that an Internet connection is available. Uwe Ziegenhagen
In the following table we then use \py com- Escher Str. 221
mands to specify which stock quote to be retrieved. 50739 Cologne, Germany
This command requires the executed Python code ziegenhagen (at) gmail dot com
to return a single expression. www.uweziegenhagen.de
Uwe Ziegenhagen