![Piton および Pyluatex 環境: コンテンツを外部ファイルに書き込む](https://rvso.com/image/472520/Piton%20%E3%81%8A%E3%82%88%E3%81%B3%20Pyluatex%20%E7%92%B0%E5%A2%83%3A%20%E3%82%B3%E3%83%B3%E3%83%86%E3%83%B3%E3%83%84%E3%82%92%E5%A4%96%E9%83%A8%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AB%E6%9B%B8%E3%81%8D%E8%BE%BC%E3%82%80.png)
piton
一方ではパッケージから作成された Python 用の環境がいくつかあり、他方ではパッケージから作成された Python 用の環境がいくつかありますPyluatex
。
場合によっては、その内容を外部の Python ファイル (まだ存在しない場合は作成されます) にコピーしたいことがあります。これをどのように実行できるかご存知ですか? たとえば、作成された環境にオプションの引数を作成するなどです。 のスター バージョンpyverbatim*
などでしょpyblock*
うか?
\documentclass{article}
\usepackage{xcolor,currfile,needspace,fontawesome5,luacode}
\usepackage{pyluatex}
\usepackage{piton}
\begin{luacode}
function pytypeset()
tex.print("\\begin{Piton}")
tex.print(pyluatex.get_last_code())
tex.print("\\end{Piton}")
tex.print("")
end
function pytypesetREPL()
tex.print("\\begin{Piton}")
tex.print(pyluatex.get_last_output())
tex.print("\\end{Piton}")
tex.print("")
end
\end{luacode}
\newcommand*{\pytypeset}
{\directlua{pytypeset()}}
\newcommand*{\pytypesetREPL}
{\directlua{pytypesetREPL()}}
% Environnements Python
\NewPitonEnvironment{pyverbatim}{ !d<> !O{} }{
\Needspace*{\baselineskip}
\IfValueT{#1}{{\bfseries\sffamily\color{mongris}{\tiny\faSquare\:\faSquare\:\:}#1}}
\PitonOptions{#2}}{}
\NewDocumentEnvironment{pyconsole}{ !d<> !O{} }
{\Needspace*{\baselineskip}\PyLTVerbatimEnv\begin{pythonrepl}}
{\end{pythonrepl}
\IfValueT{#1}{{\bfseries\sffamily{\tiny\faSquare\:\faSquare\:\:}#1}}
\PitonOptions{#2}\pytypesetREPL}
%
\newenvironment{pycode}
{\PyLTVerbatimEnv\begin{pythonq}}
{\end{pythonq}}
%
\NewDocumentEnvironment{pyblock}{ !d<> !O{} }
{\PyLTVerbatimEnv\begin{pythonq}}
{\end{pythonq}
\Needspace*{\baselineskip}
\IfValueT{#1}{{\bfseries\sffamily{\tiny\faSquare\:\faSquare\:\:}#1}}
\PitonOptions{#2}\pytypeset
\Needspace*{\baselineskip}
}
%
\begin{document}
\begin{pyverbatim}
x = 1
\end{pyverbatim}
\begin{pyblock}
y = 2
\end{pyblock}
% Desired : environments with an other option writing environment content in "filename.py"
\begin{pyverbatim}[write="filename.py"]
x = 1
\end{pyverbatim}
% at this step, the external file does not exist so the file is created and the content x=1 written to the file
\begin{pyverbatim}[write="filename.py"]
x = 1
\end{pyverbatim}
% at this step, filename.py exist, so y = 2 is added
\begin{pyblock}[write="filename.py"]
y = 2
\end{pyblock}
\end{document}
答え1
最新バージョンpiton
(v. 2.3 2024-01-06) では、その機能を備えた新しいキーが提供されますwrite
。