1

I'm using a mdframed environment in order to add a dark background to the pythontex environments pyverbatim, pyblock, pycode, etc.. The code is printed with the monokai style.

For inline codes (without background), I would like to use an other style like tango. Would it possible to set one style for the environments pyverbatim, pyblock... and an other one for the inline version \pyv{}, \pyc{}... ?

MWE

\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}

enter image description here

1

The following workaround modifies the \pyv command to set the internal style macro \pytx@style before and after the command. The redefinition is placed after \begin{document} because \pyv is not yet defined in the preamble.

\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}

Result:

enter image description here

Note that the workaround does not allow for optional arguments to \pyv.

1
  • Thanks, it works perfectly ! I tried the command \AfterEndPreamble{\makeatletter \let\oldpyv\pyv... } but it has no effect.
    – jowe_19
    May 6 '20 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.