summaryrefslogtreecommitdiff
path: root/macros/luatex
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2021-08-08 03:01:03 +0000
committerNorbert Preining <norbert@preining.info>2021-08-08 03:01:03 +0000
commited204b296810be74994f70f76b7f5b849f6e9ecb (patch)
treef39bb9d2abbf54d1fa1a585d245a0942c77e2f05 /macros/luatex
parent5991e0faa2ab9a1b9bdb72d6399cff4444b02f35 (diff)
CTAN sync 202108080301
Diffstat (limited to 'macros/luatex')
-rw-r--r--macros/luatex/latex/pyluatex/README.md1
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.lua20
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.pdfbin78301 -> 79524 bytes
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.sty14
-rw-r--r--macros/luatex/latex/pyluatex/pyluatex.tex17
5 files changed, 38 insertions, 14 deletions
diff --git a/macros/luatex/latex/pyluatex/README.md b/macros/luatex/latex/pyluatex/README.md
index 6c8015dce9..3165301bd8 100644
--- a/macros/luatex/latex/pyluatex/README.md
+++ b/macros/luatex/latex/pyluatex/README.md
@@ -36,7 +36,6 @@ $\sqrt{371} = \py{math.sqrt(371)}$
\randint{2}{5}
\end{document}
```
-
2\. Compile using LuaLaTeX (shell escape is required)
```
lualatex -shell-escape example.tex
diff --git a/macros/luatex/latex/pyluatex/pyluatex.lua b/macros/luatex/latex/pyluatex/pyluatex.lua
index 506b8bf9cf..d7e27bc6cb 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.lua
+++ b/macros/luatex/latex/pyluatex/pyluatex.lua
@@ -37,6 +37,7 @@ local tcp = nil
local env_end = nil
local env_lines = nil
+local parent_env = nil
local last_code = nil
local last_output = nil
@@ -149,15 +150,28 @@ local function record_line(line)
end
function pyluatex.record_env(quiet)
- if quiet then
- env_end = "\\end{pythonq}"
+ local name
+ if parent_env ~= nil then
+ name = parent_env
+ parent_env = nil
else
- env_end = "\\end{python}"
+ if quiet then
+ name = "pythonq"
+ else
+ name = "python"
+ end
end
+ env_end = "\\end{" .. name .. "}"
env_lines = {}
luatexbase.add_to_callback("process_input_buffer", record_line, "pyluatex_record_line")
end
+function pyluatex.set_parent_env(name)
+ if parent_env == nil then
+ parent_env = name
+ end
+end
+
function pyluatex.run_file(path, write)
local f = io.open(path, "r")
if f then
diff --git a/macros/luatex/latex/pyluatex/pyluatex.pdf b/macros/luatex/latex/pyluatex/pyluatex.pdf
index 58d2de88e1..49fee5afac 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.pdf
+++ b/macros/luatex/latex/pyluatex/pyluatex.pdf
Binary files differ
diff --git a/macros/luatex/latex/pyluatex/pyluatex.sty b/macros/luatex/latex/pyluatex/pyluatex.sty
index 0709ba7600..81fa87ea82 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.sty
+++ b/macros/luatex/latex/pyluatex/pyluatex.sty
@@ -9,7 +9,7 @@
%% version 2005/12/01 or later.
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{pyluatex}[2021/07/26 v0.2.0 Execute Python code on the fly]
+\ProvidesPackage{pyluatex}[2021/08/07 v0.3.0 Execute Python code on the fly]
\RequirePackage{expl3}
\ExplSyntaxOn
@@ -30,18 +30,20 @@
\directlua{pyluatex.start([==[\pyluatex@executable]==])}
+\newcommand*{\PyLTVerbatimEnv}{\directlua{pyluatex.set_parent_env([==[\@currenvir]==])}}
+
\newenvironment{python}{\directlua{pyluatex.record_env(false)}}%
{\directlua{pyluatex.print_env()}}
\newenvironment{pythonq}{\directlua{pyluatex.record_env(true)}}{}
-\newcommand*{\python@inline}[3]{\directlua{pyluatex.execute([==[#1]==], #2, #3)}}
+\newcommand*{\pyluatex@inline}[3]{\directlua{pyluatex.execute([==[#1]==], #2, #3)}}
-\newcommand*{\py}[1]{\python@inline{#1}{true}{true}}
-\newcommand*{\pyq}[1]{\python@inline{#1}{true}{false}}
+\newcommand*{\py}[1]{\pyluatex@inline{#1}{true}{true}}
+\newcommand*{\pyq}[1]{\pyluatex@inline{#1}{true}{false}}
-\newcommand*{\pyc}[1]{\python@inline{#1}{false}{true}}
-\newcommand*{\pycq}[1]{\python@inline{#1}{false}{false}}
+\newcommand*{\pyc}[1]{\pyluatex@inline{#1}{false}{true}}
+\newcommand*{\pycq}[1]{\pyluatex@inline{#1}{false}{false}}
\newcommand*{\pysession}[1]{\directlua{pyluatex.session = [==[#1]==]}}
diff --git a/macros/luatex/latex/pyluatex/pyluatex.tex b/macros/luatex/latex/pyluatex/pyluatex.tex
index ffaf503914..d6c0325350 100644
--- a/macros/luatex/latex/pyluatex/pyluatex.tex
+++ b/macros/luatex/latex/pyluatex/pyluatex.tex
@@ -15,7 +15,7 @@
\usepackage{url}
\title{The \emph{pyluatex} package}
\author{Tobias Enderle\\\url{https://github.com/tndrle/PyLuaTeX}}
-\date{v0.2.0 (2021/07/26)}
+\date{v0.3.0 (2021/08/07)}
\begin{document}
\maketitle
\raggedright
@@ -58,8 +58,6 @@ $\sqrt{371} = \py{math.sqrt(371)}$
\randint{2}{5}
\end{document}
\end{tcblisting}
-\end{enumerate}
-\begin{enumerate}
\item Compile using Lua\LaTeX{} (shell escape is required)
\begin{tcblisting}{breakable,listing only,
size=fbox,colframe=black!8,boxrule=3pt,colback=black!8}
@@ -178,6 +176,16 @@ For an example, see the Typesetting Code section.
\item \inlcode|pythonq|\\[0.5ex]
Same as the \inlcode|python| environment, but any output is suppressed.
\end{itemize}
+You can create your own environments based on the \inlcode|python| and \inlcode|pythonq| environments.
+However, since they are verbatim environments, you have to use the macro \inlcode|\PyLTVerbatimEnv|
+in your environment definition, e.g.
+\begin{tcblisting}{breakable,listing only,
+ size=fbox,colframe=black!8,boxrule=3pt,colback=black!8}
+\newenvironment{custompy}
+{\PyLTVerbatimEnv\begin{python}}
+{\end{python}}
+\end{tcblisting}
+
\section{Requirements}
\begin{itemize}
\item Lua\LaTeX{}
@@ -194,7 +202,7 @@ However, PyLuaTeX has a \textbf{code and output buffer} which you can use to cre
This provides a lot of flexibility for your typesetting.
After a PyLuaTeX macro or environment has been executed, the corresponding Python code and output can be accessed via the Lua functions \inlcode|pyluatex.get_last_code()| and \inlcode|pyluatex.get_last_output()|, respectively.
-Both functions return a Lua table\footnote{\url{https://www.lua.org/pil/2.5.html}} where each table item corresponds to a line of code or output.
+Both functions return a Lua table\footnote{\url{https://www.lua.org/pil/2.5.html}} (basically an array) where each table item corresponds to a line of code or output.
A simple example for typesetting code and output using the \textit{listings} package would be:
\begin{tcblisting}{breakable,listing only,
@@ -238,6 +246,7 @@ Notice that we use the \inlcode|pythonq| environment, which suppresses any outpu
After that, the custom macro \inlcode|\pytypeset| is responsible for typesetting the code and its output.
Using a different code listings package like \textit{minted}, or typesetting inline code is very easy.
+You can also define your own environments that combine Python code and typesetting.
See the \inlcode|typesetting-*.tex| examples in the \inlcode|example| folder.
\section{How It Works}