Pythontex/Pygments:为内联代码选择其他 pygopt 样式 \py{} \pyv{}

Pythontex/Pygments:为内联代码选择其他 pygopt 样式 \py{} \pyv{}

我正在使用mdframed环境以便为 pythontex 环境pyverbatim、、pyblock等添加深色背景pycode。代码以 monokai 样式打印。

对于内联代码(无背景),我想使用其他样式,例如tango。是否可以为环境设置一种样式pyverbatimpyblock为内联版本设置另一种样式\pyv{}, \pyc{}……?

平均能量损失

\documentclass[a4paper,8pt,table]{scrbook}
\usepackage{color}
\usepackage{tikz}
%
\usepackage{fvextra}
\fvset{breaklines=true,linenos=true}
\usepackage[pygopt={style=monokai}]{pythontex}
%
%\setpythontexpygopt[pycon]{style=monokai}
%\setpygmentspygopt[pyv]{style=monokai}
%tango style just for inline families ?

% Dark mode
\usepackage{mdframed}
\definecolor{bgmonokai}{RGB}{4, 44, 52}
\newenvironment{codepy}[1][]{%
   \mdfsetup{
      backgroundcolor = bgmonokai,
      hidealllines    = true,
      skipabove       = 0.2\baselineskip,
      skipbelow       = 0.7\baselineskip,
      splitbottomskip = 2pt,
      splittopskip    = 2pt,
      innerleftmargin = 8pt,
      innertopmargin  = 2pt,
%      frametitlealignment={\hspace*{-50pt}}
      }
    \ifstrempty{#1}%
    {\begin{mdframed}
    }{
    \begin{mdframed}[
    frametitle   = {\color{white}\texttt{\tbf{#1}}}]
    }
    }{\end{mdframed}%
    }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\begin{codepy}
\begin{pyconsole}
x = 1
x + 1
\end{pyconsole}
\end{codepy}

\begin{codepy}
\begin{pyverbatim}
x = 1
y + 1
\end{pyverbatim}
\end{codepy}

We don't see this text : \pyv{numpy}. 
\end{document}

在此处输入图片描述

答案1

以下解决方法修改了\pyv命令,在命令之前和之后设置内部样式宏\pytx@style。重新定义放在后面,\begin{document}因为\pyv在前言中尚未定义。

\documentclass[a4paper,8pt,table]{scrbook}
\usepackage{color}
\usepackage{tikz}
%
\usepackage{fvextra}
\fvset{breaklines=true,linenos=true}
\usepackage[pygopt={style=monokai}]{pythontex}
%
%\setpythontexpygopt[pycon]{style=monokai}
%\setpygmentspygopt[pyv]{style=monokai}
%tango style just for inline families ?

% Dark mode
\usepackage{mdframed}
\definecolor{bgmonokai}{RGB}{4, 44, 52}
\newenvironment{codepy}[1][]{%
   \mdfsetup{
      backgroundcolor = bgmonokai,
      hidealllines    = true,
      skipabove       = 0.2\baselineskip,
      skipbelow       = 0.7\baselineskip,
      splitbottomskip = 2pt,
      splittopskip    = 2pt,
      innerleftmargin = 8pt,
      innertopmargin  = 2pt,
%      frametitlealignment={\hspace*{-50pt}}
      }
    \ifstrempty{#1}%
    {\begin{mdframed}
    }{
    \begin{mdframed}[
    frametitle   = {\color{white}\texttt{\tbf{#1}}}]
    }
    }{\end{mdframed}%
    }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\makeatletter
\let\oldpyv\pyv
\def\mypyv#1{\def\pytx@style{tango}\oldpyv{#1}\def\pytx@style{monokai}}
\let\pyv\mypyv
\makeatother

\begin{codepy}
\begin{pyverbatim}
x = 1
y + 1
\end{pyverbatim}
\end{codepy}

We don't see this text : \pyv{self.var = var}.

\begin{codepy}
\begin{pyverbatim}
x = 1
y + 1
\end{pyverbatim}
\end{codepy}
\end{document}

结果:

在此处输入图片描述

请注意,该解决方法不允许使用可选参数\pyv

相关内容