我正在使用颜色来显示python代码片段,它们嵌入在具有定义(下面的环境)minted
的tcolorbox中。\newtcblisting
codepy
我想创建另一个名为 的环境,codepyexec
其功能与 相同,但另外使用 CTAN 上最近发布的包codepy
运行 Python 代码(环境) 。该怎么做?python
pyluatex
平均能量损失
\documentclass[11pt]{scrbook}
\usepackage{minted}
\usemintedstyle{manni}
%
\usepackage[verbose]{pyluatex}
%
\RequirePackage[skins,breakable,xparse,many,minted]{tcolorbox}
\tcbsetforeverylayer{%
enhanced,
coltitle = black ,
colbacktitle = white ,
boxrule = 0.5pt ,
colback = white ,
breakable ,
arc = 2mm ,
size = title ,
toprule at break = 0mm ,
bottomrule at break = 0mm ,
lower separated = false ,
boxed title style ={size=small, boxrule=0pt, colframe=white,} ,
fonttitle = \bfseries\sffamily ,
varwidth boxed title ,
before app = \parfillskip0pt
}
%
\newtcblisting{mintedpython}[1][]{
listing engine=minted,
minted language=python,
minted options={autogobble,breaklines,breakanywhere} ,
listing only,
skin=enhancedmiddle jigsaw, % Rule D/G uniquement
enlarge top initially by = 2.5mm,
rightrule=0mm,
leftrule=2mm,
arc=0mm,
outer arc=0mm,
left=3.4mm,
titlerule=0mm,
detach title,
attach title to upper={\ \quad \ },
IfValueTF={#1}{title={#1}}{},
}
%
\newenvironment{codepy}{\VerbatimEnvironment\begin{mintedpython}%
}{
\end{mintedpython}
}
%
%\newenvironment{codepyexec}{\VerbatimEnvironment...
% }{
% ...
% }
%
\begin{document}
\begin{codepy}
x = 1
print(x)
def f(x):
return x**2
\end{codepy}
%codepyexec should do this :
%\begin{codepy}
%x = 1
%print(x)
%
%def f(x):
% return x**2
%\end{codepy}
%AND
%\begin{python}
%x = 1
%print(x)
%
%def f(x):
% return x**2
%\end{python}
\end{document}
答案1
供参考,这里有一种方法可以使用包piton
而不是来实现相同的功能minted
。Python 代码是用包执行的pyluatex
(这需要使用 LuaLaTeX),但语法着色是由包完成的piton
(该包不需要外部工具,不像minted
使用外部程序 Pygments)。
\documentclass[11pt]{scrbook}
\usepackage[verbose]{pyluatex}
\usepackage{piton}
\RequirePackage[skins,breakable,xparse,many]{tcolorbox}
\tcbsetforeverylayer{
enhanced,
coltitle=black,
colbacktitle=white,
boxrule=0.5pt,
colback=white,
breakable,
arc=2mm,
size=title,
toprule at break=0mm,
bottomrule at break=0mm,
lower separated=false,
boxed title style={size=small,boxrule=0pt,colframe=white},
fonttitle=\bfseries\sffamily,
varwidth boxed title,
before app=\parfillskip0pt,
}
\newtcolorbox{tcbpitonpython}[1][]{
skin=enhancedmiddle jigsaw,
enlarge top initially by=2.5mm,
rightrule=0mm,
leftrule=2mm,
arc=0mm,
outer arc=0mm,
left=3.4mm,
titlerule=0mm,
detach title,
attach title to upper={\ \quad \ },
IfValueTF={#1}{title={#1}}{},
}
\NewPitonEnvironment{pitonpython}{}
{ \begin{tcbpitonpython} }
{ \end{tcbpitonpython} }
\usepackage{luacode}
\begin{luacode}
function pytypeset()
tex.print("\\begin{pitonpython}")
tex.print(pyluatex.get_last_code())
tex.print("\\end{pitonpython}")
tex.print("") -- ensure newline
end
\end{luacode}
\newenvironment{codepyexec}
{\PyLTVerbatimEnv\begin{pythonq}}
{\end{pythonq}
\directlua{pytypeset()}
% Optional: print Python output (enable next line)
% Output: \directlua{tex.print(pyluatex.get_last_output())}
}
\begin{document}
\begin{codepyexec}
x = 1
print(x)
def f(x):
return x**2
\end{codepyexec}
% Check whether code was not only typeset but also executed:
\py{f(3)}
\end{document}
答案2
从 v0.3.0 开始,可以通过宏实现所需的功能\PyLTVerbatimEnv
。
例如:
\documentclass[11pt]{scrbook}
\usepackage{minted}
\usemintedstyle{manni}
\usepackage[verbose]{pyluatex}
\RequirePackage[skins,breakable,xparse,many,minted]{tcolorbox}
\tcbsetforeverylayer{
enhanced,
coltitle=black,
colbacktitle=white,
boxrule=0.5pt,
colback=white,
breakable,
arc=2mm,
size=title,
toprule at break=0mm,
bottomrule at break=0mm,
lower separated=false,
boxed title style={size=small,boxrule=0pt,colframe=white},
fonttitle=\bfseries\sffamily,
varwidth boxed title,
before app=\parfillskip0pt,
}
\newtcblisting{mintedpython}[1][]{
listing engine=minted,
minted language=python,
minted options={autogobble,breaklines,breakanywhere},
listing only,
skin=enhancedmiddle jigsaw,
enlarge top initially by=2.5mm,
rightrule=0mm,
leftrule=2mm,
arc=0mm,
outer arc=0mm,
left=3.4mm,
titlerule=0mm,
detach title,
attach title to upper={\ \quad \ },
IfValueTF={#1}{title={#1}}{},
}
\usepackage{luacode}
\begin{luacode}
function pytypeset()
tex.print("\\begin{mintedpython}")
tex.print(pyluatex.get_last_code())
tex.print("\\end{mintedpython}")
tex.print("") -- ensure newline
end
\end{luacode}
\newenvironment{codepyexec}
{\PyLTVerbatimEnv\begin{pythonq}}
{\end{pythonq}
\directlua{pytypeset()}
% Optional: print Python output (enable next line)
% Output: \directlua{tex.print(pyluatex.get_last_output())}
}
\begin{document}
\begin{codepyexec}
x = 1
print(x)
def f(x):
return x**2
\end{codepyexec}
% Check whether code was not only typeset but also executed:
\py{f(3)}
\end{document}