% proflycee-tools-pythontex.tex
% Copyright 2023-2025  Cédric Pierquet
% Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt

%2.7.5  Argument optionnel pour commencer la numérotation à une autre ligne que 1
%2.7.3  Correction de la couleur des bordures
%2.5.8  Style alternatif + Modification marges + Modification arguments

%%------CONSOLEPYTHON
\defKV[envpythonconsole]{%
  Largeur=\def\CSPYlargeur{#1},%
  Centre=\testboolKV{#1}
    {\def\hookcenterpre{\begin{center}}\def\hookcenterpost{\end{center}}}
    {\def\hookcenterpre{\begin{flushleft}}\def\hookcenterpost{\end{flushleft}}},%
  TaillePolice=\def\CSPYfonte{#1},%
  EspacementVertical=\def\CSPYstretch{#1}
}

\setKVdefault[envpythonconsole]{%
  Largeur=\linewidth,%
  Centre=false,%
  Label=true,%
  TaillePolice=\footnotesize,%
  EspacementVertical=1
}

\newlength{\PythontexCodeXLeft}
\setlength{\PythontexCodeXLeft}{14pt}

\newenvironment{ConsolePythontex}[2][]
{%
  \useKVdefault[envpythonconsole]%
  \setKV[envpythonconsole]{#1}% on paramètres les nouvelles clés et on les simplifie
  \VerbatimEnvironment
  \hookcenterpre
  \begin{minipage}{\CSPYlargeur}
    \ifboolKV[envpythonconsole]{Label}%si label
      {\begin{pyconsole}[][%
        framesep=3mm,frame=single,fontsize=\CSPYfonte,framerule=1pt,rulecolor=\color{CouleurVertForet},label={[\scriptsize Début de la console python]\scriptsize Fin de la console python},baselinestretch=\CSPYstretch]}%
      {\begin{pyconsole}[][%
        framesep=3mm,frame=single,fontsize=\CSPYfonte,framerule=1pt,rulecolor=\color{CouleurVertForet},baselinestretch=\CSPYstretch]}
}%
{%
    \end{pyconsole}
  \end{minipage}
  \hookcenterpost
}

%=========CODEPYTHONTEX=========== OK!!
\defKV[envpythonverb]{%
  Largeur=\def\CODPYlargeur{#1},%
  PremLigne=\def\CODPYpremligne{#1},%
  TaillePolice=\def\CODPYfonte{#1},%
  EspacementVertical=\def\CODPYstretch{#1}
%  Centre=\testboolKV{#1}
%    {\def\verbcenterpre{\begin{center}}\def\verbcenterpost{\end{center}}}
%    {\def\verbcenterpre{}\def\verbcenterpost{}}
}

\setKVdefault[envpythonverb]{%
  Largeur=\linewidth,%
  PremLigne=1,%
%  Centre=false,%
  Lignes=true,%
  TaillePolice=\footnotesize,%
  EspacementVertical=1
}

%v1

\tcbset{stylepythontex/.style={%
  enhanced,boxrule=1.25pt,%
  sharp corners=downhill,arc=12pt,
  before skip=0.5\baselineskip,after skip=0.5\baselineskip,%
  top=\baselineskip,bottom=1mm,right=5mm,left=0.6em,
  attach boxed title to top right={yshift=-\tcboxedtitleheight},
  boxed title style={
    size=small,colback=CouleurVertForet!25,boxrule=1.25pt,
    colframe=CouleurVertForet,boxsep=1.25pt,
    sharp corners=downhill,
    arc=12pt,
    top=2pt,bottom=1pt,left=6pt,right=6pt
    },
  fonttitle=\color{CouleurVertForet}\itshape\ttfamily\footnotesize,
  title={\scriptsize\pflfapython}\:Code Python\vphantom{p},
  watermark text={\pflfapython},watermark opacity=0.175,watermark zoom=0.50,
  colframe=CouleurVertForet,colback=CouleurVertForet!5,%
  before upper=\renewcommand\theFancyVerbLine{\scriptsize\ttfamily\color{darkgray}\arabic{FancyVerbLine}}
  }
}

\tcbset{stylepythonnolineos/.style={%
  stylepythontex,leftupper=1em
  }
}

\tcbset{stylepythonlineos/.style={%
  stylepythontex,leftupper=2em
  }
}

\newtcolorbox{tcpythontexcode}[2][\linewidth]{%
  width=#1,stylepythonlineos,#2
}

\newtcolorbox{tcpythontexcodeno}[2][\linewidth]{%
  width=#1,stylepythonnolineos,#2
}

\NewDocumentEnvironment{CodePythontex}{ O{} m }
{%
  \useKVdefault[envpythonverb]%
  \setKV[envpythonverb]{#1}% on paramètres les nouvelles clés et on les simplifie
  \VerbatimEnvironment
%  \verbcenterpre
  \ifboolKV[envpythonverb]{Lignes}%si lignes=true
    {\begin{tcpythontexcode}[\CODPYlargeur]{#2}}
    {\begin{tcpythontexcodeno}[\CODPYlargeur]{#2}}
  \ifboolKV[envpythonverb]{Lignes}%si lignes=true
    {\begin{pyverbatim}[][fontsize=\CODPYfonte,numbers=left,firstnumber=\CODPYpremligne,numbersep=0.75em,commandchars=\\\{\},mathescape,baselinestretch=\CODPYstretch]}
    {\begin{pyverbatim}[][numbers=none,numbersep=0pt,fontsize=\CODPYfonte,commandchars=\\\{\},mathescape,baselinestretch=\CODPYstretch]}
}%
{%
  \end{pyverbatim}
  \ifboolKV[envpythonverb]{Lignes}%si lignes=true
    {\end{tcpythontexcode}}
    {\end{tcpythontexcodeno}}
%  \verbcenterpost
}

%v2
\tcbset{stylepythontexalt/.style={%
  enhanced,boxrule=0.75pt,colframe=darkgray!50!black,%
  sharp corners,top=0mm,bottom=0mm,left=0.2em,right=5mm,%
  before skip=0.5\baselineskip,after skip=0.5\baselineskip,%
  colback=white,
  fontupper=\footnotesize,fontlower=\footnotesize,%
  title={{\scriptsize\pflfapcode} Code Python},
  lefttitle=0.4em,
  watermark text={\pflfapython},watermark opacity=0.25,watermark zoom=0.50,
  fonttitle=\bfseries\footnotesize\sffamily,colbacktitle=darkgray!50!black,
  before upper=\renewcommand\theFancyVerbLine{\scriptsize\ttfamily\color{darkgray}\arabic{FancyVerbLine}}
  }
}

\tcbset{stylepythonnolineosalt/.style={%
  stylepythontexalt,leftupper=0.2em,
  }
}

\tcbset{stylepythonlineosalt/.style={%
  stylepythontexalt,leftupper=1.35em,
  underlay={%
    \begin{tcbclipinterior}
      \draw[draw=none,fill=lightgray!25] (interior.south west) rectangle ([xshift=1.3em]interior.north west) ;
    \end{tcbclipinterior}%
    }
  }
}

\newtcolorbox{tcpythontexcodealt}[2][\linewidth]{%
  width=#1,stylepythonlineosalt,#2
}

\newtcolorbox{tcpythontexcodenoalt}[2][\linewidth]{%
  width=#1,stylepythonnolineosalt,#2
}
\NewDocumentEnvironment{CodePythontexAlt}{ O{} m }
{%
  \useKVdefault[envpythonverb]%
  \setKV[envpythonverb]{#1}% on paramètres les nouvelles clés et on les simplifie
  \VerbatimEnvironment
%  \verbcenterpre
  \ifboolKV[envpythonverb]{Lignes}%si lignes=true
    {\begin{tcpythontexcodealt}[\CODPYlargeur]{#2}}
    {\begin{tcpythontexcodenoalt}[\CODPYlargeur]{#2}}
  \ifboolKV[envpythonverb]{Lignes}%si lignes=true
    {\begin{pyverbatim}[][fontsize=\CODPYfonte,numbers=left,firstnumber=\CODPYpremligne,numbersep=0.75em,commandchars=\\\{\},mathescape,baselinestretch=\CODPYstretch]}
    {\begin{pyverbatim}[][numbers=none,numbersep=0pt,fontsize=\CODPYfonte,commandchars=\\\{\},mathescape,baselinestretch=\CODPYstretch]}
}%
{%
  \end{pyverbatim}
  \ifboolKV[envpythonverb]{Lignes}%si lignes=true
    {\end{tcpythontexcodealt}}
    {\end{tcpythontexcodenoalt}}
%  \verbcenterpost
}

\endinput