From bfa7c6612a8c83982239daadda8bd5769cf236cb Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sat, 7 Aug 2021 20:20:15 +0000 Subject: pyluatex (7aug21) git-svn-id: svn://tug.org/texlive/trunk@60185 c570f23f-e606-0410-a88d-b1316a301751 --- Master/texmf-dist/doc/lualatex/pyluatex/README.md | 1 - .../texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf | Bin 78301 -> 79524 bytes .../texmf-dist/doc/lualatex/pyluatex/pyluatex.tex | 17 +++++++++++++---- .../texmf-dist/tex/lualatex/pyluatex/pyluatex.lua | 20 +++++++++++++++++--- .../texmf-dist/tex/lualatex/pyluatex/pyluatex.sty | 14 ++++++++------ 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Master/texmf-dist/doc/lualatex/pyluatex/README.md b/Master/texmf-dist/doc/lualatex/pyluatex/README.md index 6c8015dce92..3165301bd89 100644 --- a/Master/texmf-dist/doc/lualatex/pyluatex/README.md +++ b/Master/texmf-dist/doc/lualatex/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/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf index 58d2de88e15..49fee5afac4 100644 Binary files a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf and b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.pdf differ diff --git a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex b/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex index ffaf503914c..d6c03253508 100644 --- a/Master/texmf-dist/doc/lualatex/pyluatex/pyluatex.tex +++ b/Master/texmf-dist/doc/lualatex/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} diff --git a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua index 506b8bf9cf8..d7e27bc6cb9 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.lua +++ b/Master/texmf-dist/tex/lualatex/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/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty b/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty index 0709ba76000..81fa87ea827 100644 --- a/Master/texmf-dist/tex/lualatex/pyluatex/pyluatex.sty +++ b/Master/texmf-dist/tex/lualatex/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]==]}} -- cgit v1.2.3