| 1 |
# PyLuaTeX |
| 2 |
**Execute Python code on the fly in your LaTeX documents** |
| 3 |
|
| 4 |
PyLuaTeX allows you to execute Python code and to include the resulting output |
| 5 |
in your LaTeX documents in a *single compilation run*. |
| 6 |
LaTeX documents must be compiled with LuaLaTeX for this to work. |
| 7 |
|
| 8 |
|
| 9 |
## Example |
| 10 |
1\. LaTeX document `example.tex` |
| 11 |
```latex |
| 12 |
\documentclass{article} |
| 13 |
|
| 14 |
\usepackage{pyluatex} |
| 15 |
|
| 16 |
\begin{python} |
| 17 |
import math |
| 18 |
import random |
| 19 |
|
| 20 |
random.seed(0) |
| 21 |
|
| 22 |
greeting = 'Hello PyLuaTeX!' |
| 23 |
\end{python} |
| 24 |
|
| 25 |
\newcommand{\randint}[2]{\py{random.randint(#1, #2)}} |
| 26 |
|
| 27 |
\begin{document} |
| 28 |
\py{greeting} |
| 29 |
|
| 30 |
$\sqrt{371} = \py{math.sqrt(371)}$ |
| 31 |
|
| 32 |
\randint{2}{5} |
| 33 |
\end{document} |
| 34 |
``` |
| 35 |
2\. Compile using LuaLaTeX (shell escape is required) |
| 36 |
``` |
| 37 |
lualatex --shell-escape example.tex |
| 38 |
``` |
| 39 |
|
| 40 |
**Note:** PyLuaTeX starts Python 3 using the command `python3` by default. |
| 41 |
If `python3` does not start Python 3 on your system, find the correct command |
| 42 |
and extend `\usepackage{pyluatex}` to |
| 43 |
`\usepackage[executable=<your python command>]{pyluatex}`. |
| 44 |
For example, `\usepackage[executable=python.exe]{pyluatex}`. |
| 45 |
|
| 46 |
**Security note:** Running LaTeX with the `--shell-escape` option |
| 47 |
allows arbitrary code to be executed. |
| 48 |
For this reason, it is recommended to **compile trusted documents only**. |
| 49 |
|
| 50 |
## Requirements |
| 51 |
* LuaLaTeX |
| 52 |
* Python 3 |
| 53 |
* Linux, macOS or Windows |
| 54 |
|
| 55 |
## License |
| 56 |
[LPPL 1.3c](http://www.latex-project.org/lppl.txt) for LaTeX code and |
| 57 |
[MIT license](https://opensource.org/licenses/MIT) for Python and Lua code |
| 58 |
and other files. |
| 59 |
|
| 60 |
## Further Information |
| 61 |
Author: Tobias Enderle |
| 62 |
|
| 63 |
Development: https://github.com/tndrle/PyLuaTeX |