Je ne sais plus trop pourquoi, mais je m'étais créé un package basé sur pythonhighlight.
Au cas où (il s'appelle pythonhighlight_marco_bis et est à mettre dans le texmf perso) :
Code : Tout sélectionner
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{pythonhighlight_marco_bis}
\RequirePackage{listingsutf8}
\RequirePackage[breakable]{tcolorbox}
\RequirePackage{xcolor}
\definecolor{intdef}{rgb}{0.9 1 0.8}
\definecolor{intprop}{rgb}{1 1 0.7}
\renewcommand*{\lstlistlistingname}{Code Listings}
\renewcommand*{\lstlistingname}{Code Listing}
\definecolor{gray}{gray}{0.5}
\colorlet{commentcolour}{green!60!black}
\colorlet{stringcolour}{red!80!black}
\colorlet{keywordcolour}{magenta!90!black}
\colorlet{exceptioncolour}{yellow!80!red}
\colorlet{commandcolour}{blue!60!black}
\colorlet{numpycolour}{blue!80!green}
\colorlet{literatecolour}{magenta!90!black}
\colorlet{promptcolour}{red!80!black}
\colorlet{specmethodcolour}{violet}
\newcommand*{\framemargin}{3ex}
\newcommand*{\literatecolour}{\textcolor{literatecolour}}
\newcommand*{\pythonprompt}{\textcolor{promptcolour}{{>}{>}{>}}}
\lstdefinestyle{mypython}{
%\lstset{
%keepspaces=true,
inputencoding=utf8,
language=python,
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily\footnotesize, %\setstretch{.5},
stringstyle=\color{stringcolour},
showstringspaces=false,
keepspaces=true,
alsoletter={1234567890},
otherkeywords={\%, \}, \{, \&, \|},
morekeywords={as},
keywordstyle=\color{keywordcolour}\bfseries,
emph={and,break,class,continue,def,yield,del,elif ,else,%
except,exec,finally,for,from,global,if,import,in,%
lambda,not,or,pass,print,raise,return,try,while,assert,with},
emphstyle=\color{blue}\bfseries,
emph={[2]True, False, None},
emphstyle=[2]\color{keywordcolour},
emph={[3]object,type,isinstance,copy,deepcopy,zip,enumerate,reversed,list,len,dict,tuple,xrange,append,execfile,real,imag,reduce,str,repr},
emphstyle=[3]\color{commandcolour},
emph={Exception,NameError,IndexError,SyntaxError,TypeError,ValueError,OverflowError,ZeroDivisionError},
emphstyle=\color{exceptioncolour}\bfseries,
%upquote=true,
morecomment=[s]{"""}{"""},
commentstyle=\color{commentcolour}\slshape,
%emph={[4]1, 2, 3, 4, 5, 6, 7, 8, 9, 0},
emph={[4]ode, fsolve, sqrt, exp, sin, cos,arctan, arctan2, arccos, pi, array,
norm, solve, dot, arange, , isscalar, max, sum, flatten, shape, reshape, find,
any, all, abs, plot, show, linspace, legend, quad, polyval,polyfit, hstack,
concatenate,vstack,column_stack,empty,zeros,ones,rand,vander,grid,pcolor,eig,
eigs,eigvals,svd,qr,tan,det,logspace,roll,min,mean,cumsum,cumprod,diff,vectorize
,lstsq,cla,eye,xlabel,ylabel,squeeze},
emphstyle=[4]\color{numpycolour},
emph={[5]__init__,__add__,__mul__,__div__,__sub__,__call__,__getitem__,__setitem__,__eq__,__ne__,__nonzero__,__rmul__,__radd__,__repr__,__str__,__get__,__truediv__,__pow__,__name__,__future__,__all__},
emphstyle=[5]\color{specmethodcolour},
emph={[6]assert,yield},
emphstyle=[6]\color{keywordcolour}\bfseries,
emph={[7]range},
emphstyle={[7]\color{keywordcolour}\bfseries},
% emph={[7]self},
% emphstyle=[7]\bfseries,
escapeinside={<@}{@>},
literate=
{é}{{\'e}}{1}%
{è}{{\`e}}{1}%
{à}{{\`a}}{1}%
{â}{{\^a}}{1}%
{ç}{{\c{c}}}{1}%
{æ}{{\ae}}{1}%
{œ}{{\oe}}{1}%
{ù}{{\`u}}{1}%
{É}{{\'E}}{1}%
{È}{{\`E}}{1}%
{À}{{\`A}}{1}%
{Ç}{{\c{C}}}{1}%
{Æ}{{\AE}}{1}%
{Œ}{{\OE}}{1}%
{Ê}{{\^E}}{1}%
{ê}{{\^e}}{1}%
{î}{{\^i}}{1}%
{ï}{{\"i}}{1}%
{ô}{{\^o}}{1}%
{û}{{\^u}}{1}%
{²}{{$^2$}}1
{³}{{$^3$}}1
{⁴}{{$^4$}}1
{⁵}{{$^5$}}1
{⁶}{{$^6$}}1
{⁷}{{$^7$}}1
{⁸}{{$^8$}}1
{⁹}{{$^9$}}1
{:}{{\literatecolour:}}{1}%
{=}{{\literatecolour=}}{1}%
{-}{{\literatecolour-}}{1}%
{+}{{\literatecolour+}}{1}%
{*}{{\literatecolour*}}{1}%
{**}{{\literatecolour{**}}}2%
{/}{{\literatecolour/}}{1}%
{//}{{\literatecolour{//}}}2%
{!}{{\literatecolour!}}{1}%
{[}{{\literatecolour[}}{1}%
{]}{{\literatecolour]}}{1}%
{<}{{\literatecolour<}}{1}%
{>}{{\literatecolour>}}{1}%
{>>>}{\pythonprompt}{3}%
,
%aboveskip=.5ex,
%frame=trbl, % à virer pour les petites fenêtre
%frameround=tttt,
%framesep=.3ex,
%rulecolor=\color{black!40}, % à virer pour les petites fenêtre
%framexleftmargin=\framemargin,
%framextopmargin=.1ex,
%framexbottommargin=.1ex,
%framexrightmargin=\framemargin,
%framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox, rulesepcolor=\color{blue},#1
%frame=tb,
%backgroundcolor=\color{green!20}, % à virer pour les petites fenêtres
%breakindent=.5\textwidth,frame=single, % à virer pour les petites fenêtres
breaklines=true%
%}
}
\lstdefinestyle{mypython_list}{
inputencoding=utf8,
language=python,
showtabs=true,
tab=,
tabsize=2,
basicstyle=\ttfamily,
stringstyle=\color{stringcolour},
showstringspaces=false,
keepspaces=true,
alsoletter={1234567890},
otherkeywords={\%, \}, \{, \&, \|},
morekeywords={as},
keywordstyle=\color{keywordcolour}\bfseries,
emph={and,break,class,continue,def,yield,del,elif ,else,%
except,exec,finally,for,from,global,if,import,in,%
lambda,not,or,pass,print,raise,return,try,while,assert,with},
emphstyle=\color{blue}\bfseries,
emph={[2]True, False, None},
emphstyle=[2]\color{keywordcolour},
emph={[3]object,type,isinstance,copy,deepcopy,zip,enumerate,reversed,list,len,dict,tuple,xrange,append,execfile,real,imag,reduce,str,repr},
emphstyle=[3]\color{commandcolour},
emph={Exception,NameError,IndexError,SyntaxError,TypeError,ValueError,OverflowError,ZeroDivisionError},
emphstyle=\color{exceptioncolour}\bfseries,
morecomment=[s]{"""}{"""},
commentstyle=\color{commentcolour}\slshape,
emph={[4]ode, fsolve, sqrt, exp, sin, cos,arctan, arctan2, arccos, pi, array,
norm, solve, dot, arange, , isscalar, max, sum, flatten, flat, size, new, open, Image, save, load,
getdata, putpixel, putdata, getpixel, shape, reshape, find,
any, all, abs, plot, show, linspace, legend, quad, polyval,polyfit, hstack,
concatenate,vstack,column_stack,empty,zeros,ones,rand,vander,grid,pcolor,eig,
eigs,eigvals,svd,qr,tan,det,logspace,roll,min,mean,cumsum,cumprod,diff,vectorize
,lstsq,cla,eye,xlabel,ylabel,squeeze},
emphstyle=[4]\color{numpycolour},
emph={[5]__init__,__add__,__mul__,__div__,__sub__,__call__,__getitem__,__setitem__,__eq__,__ne__,__nonzero__,__rmul__,__radd__,__repr__,__str__,__get__,__truediv__,__pow__,__name__,__future__,__all__},
emphstyle=[5]\color{specmethodcolour},
emph={[6]assert,yield},
emphstyle=[6]\color{keywordcolour}\bfseries,
emph={[7]range},
emphstyle={[7]\color{keywordcolour}\bfseries},
escapeinside={<@}{@>},
literate=
{é}{{\'e}}{1}%
{è}{{\`e}}{1}%
{à}{{\`a}}{1}%
{â}{{\^a}}{1}%
{ç}{{\c{c}}}{1}%
{æ}{{\ae}}{1}%
{œ}{{\oe}}{1}%
{ù}{{\`u}}{1}%
{É}{{\'E}}{1}%
{È}{{\`E}}{1}%
{À}{{\`A}}{1}%
{Ç}{{\c{C}}}{1}%
{Æ}{{\AE}}{1}%
{Œ}{{\OE}}{1}%
{Ê}{{\^E}}{1}%
{ê}{{\^e}}{1}%
{î}{{\^i}}{1}%
{ï}{{\"i}}{1}%
{ô}{{\^o}}{1}%
{û}{{\^u}}{1}%
{²}{{$^2$}}1
{³}{{$^3$}}1
{⁴}{{$^4$}}1
{⁵}{{$^5$}}1
{⁶}{{$^6$}}1
{⁷}{{$^7$}}1
{⁸}{{$^8$}}1
{⁹}{{$^9$}}1
{:}{{\literatecolour:}}{1}%
{=}{{\literatecolour=}}{1}%
{-}{{\literatecolour-}}{1}%
{+}{{\literatecolour+}}{1}%
{*}{{\literatecolour*}}{1}%
{**}{{\literatecolour{**}}}2%
{/}{{\literatecolour/}}{1}%
{//}{{\literatecolour{//}}}2%
{!}{{\literatecolour!}}{1}%
{[}{{\literatecolour[}}{1}%
{]}{{\literatecolour]}}{1}%
{<}{{\literatecolour<}}{1}%
{>}{{\literatecolour>}}{1}%
{>>>}{\pythonprompt}{3}%
,
backgroundcolor=\color{violet!10}
}
\newcommand*{\inputpython}[4]{
\begin{tcolorbox}[breakable,colback=violet!10,colframe=blue!60!black!30,coltitle=black,title=\textbf{#4}]
{\lstinputlisting[
firstline=#2,
lastline=#3,
firstnumber=1,
breakindent=.5\textwidth,
numbers=left,
framexleftmargin=8mm,
numberstyle=\ttfamily \footnotesize \bf \color{blue},
numbersep=11pt,
xleftmargin=1cm,
breaklines=true,
columns=flexible,
style=mypython_list]{#1}
}
\end{tcolorbox}
}
\newcommand*{\inputpythonini}[5]{
\begin{tcolorbox}[breakable,colback=violet!10,colframe=blue!60!black!30,coltitle=black,title=\textbf{#5}]
{\lstinputlisting[
firstline=#2,
lastline=#3,
firstnumber=#4,
breakindent=.5\textwidth,
numbers=left,
framexleftmargin=8mm,
numberstyle=\ttfamily \footnotesize \bf \color{blue},
numbersep=11pt,
xleftmargin=1cm,
breaklines=true,
columns=flexible,
style=mypython_list]{#1}
}
\end{tcolorbox}
}
%\lstnewenvironment{python_cons}[1][]{\lstset{style=mypython}} {} % à virer pour les petites fenêtres
%\makeatletter % à enlever dans un fichier .sty
\newsavebox{\lstb@x}
\newlength\largeurboxlisting
\newsavebox{\pythonbox}%
\lstnewenvironment{python_cons}[1][5]{%
\begingroup \lrbox{\pythonbox}%
\lstset{style=mypython}}{%
\endlrbox\endgroup
\settowidth{\largeurboxlisting}{\usebox\pythonbox}
\tikz\node[%
drop shadow={shadow xshift=3pt, shadow yshift=-3pt},
rounded corners = 3pt,
line width = 1pt ,
draw = green!80 ,
fill = green!10 ,
inner sep = 4mm,
] {
\begin{minipage}{\dimexpr\largeurboxlisting+#1mm}
\hspace*{#1mm}\usebox\pythonbox
\end{minipage}
};
}
%\makeatother % à enlever dans un fichier .sty
%\makeatletter % à enlever dans un fichier .sty
%\newsavebox{\lstb@x}
\lstnewenvironment{python_def}[1][5]{%
\begingroup \lrbox{\pythonbox}%
\lstset{style=mypython}}{%
\endlrbox\endgroup
\settowidth{\largeurboxlisting}{\usebox\pythonbox}
\tikz\node[%
drop shadow={shadow xshift=3pt, shadow yshift=-3pt},
rounded corners = 3pt,
line width = 1pt ,
draw = yellow!80 ,
fill = yellow!10 ,
inner sep = 4mm,
] {
\begin{minipage}{\dimexpr\largeurboxlisting+#1mm}
\hspace*{#1mm}\usebox\pythonbox
\end{minipage}
};
}
%\makeatother % à enlever dans un fichier .sty
%\makeatletter % à enlever dans un fichier .sty
%\newsavebox{\lstb@x}
%\lstnewenvironment{python_list}
% {\lstset{style=mypython}\setbox\lstb@x\hbox\bgroup\color@setgroup}
% {\color@endgroup\egroup\fcolorbox{black}{black!20}{\usebox{\lstb@x}}}
%\makeatother % à enlever dans un fichier .sty
\lstnewenvironment{python_list}{\lstset{breaklines=true,showspaces=false,framexleftmargin=5mm,frame=shadowbox,rulesepcolor=\color{black},backgroundcolor=\color{lightgray!50},linewidth=\textwidth,inputencoding=utf8}}{}
\lstnewenvironment{algo_list}{\lstset{breaklines=true,showspaces=false,framexleftmargin=5mm,frame=shadowbox, rulesepcolor=\color{black},backgroundcolor=\color{yellow!30},linewidth=\textwidth,inputencoding=utf8}}{}
\lstdefinestyle{mypythoninline}{
style=mypython,%
basicstyle=\ttfamily,%
keywordstyle=\color{keywordcolour},%
emphstyle={[7]\color{keywordcolour}},%
emphstyle=\color{exceptioncolour},%
literate=*%
{:}{{\literatecolour:}}{2}%
{=}{{\literatecolour=}}{2}%
{-}{{\literatecolour-}}{2}%
{+}{{\literatecolour+}}{2}%
{*}{{\literatecolour*}}2%
{**}{{\literatecolour{**}}}3%
{/}{{\literatecolour/}}{2}%
{//}{{\literatecolour{//}}}{2}%
{!}{{\literatecolour!}}{2}%
%{(}{{\literatecolour(}}{2}%
%{)}{{\literatecolour)}}{2}%
{[}{{\literatecolour[}}{2}%
{]}{{\literatecolour]}}{2}%
{<}{{\literatecolour<}}{2}%
{<=}{{\literatecolour{<=}}}3%
{>}{{\literatecolour>}}{2}%
{>=}{{\literatecolour{>=}}}3%
{==}{{\literatecolour{==}}}3%
{!=}{{\literatecolour{!=}}}3%
{+=}{{\literatecolour{+=}}}3%
{-=}{{\literatecolour{-=}}}3%
{*=}{{\literatecolour{*=}}}3%
{/=}{{\literatecolour{/=}}}3%
%% emphstyle=\color{blue},%
}
\newcommand*{\pyth}{\lstinline[style=mypythoninline]}