% \mainfolder est ../ si compilation d'un chapitre, sinon c'est vide
\ProvidesPackage{\mainfolder python-print}

% ----------------------------------------------------------------------
% Format de la page
\usepackage[
papersize={7.5in,9.25in},
% old Python/Scratch papersize={8.5in,11in},
% algebre/analyse papersize={7.5in,9.25in},  
nomarginpar, nofoot,
margin = 0.7in,
%old margin = 0.9in,
%top=0.9in,
%right=0.8in, left=0.8in, top=0.5in, bottom=0.7in,
bindingoffset=0.5in,
headsep=21pt, heightrounded,
%  showframe,
]{geometry}
% pas de retrait
\setlength{\parindent}{0cm}
% Espace interligne (+ xx%)
\linespread{1.1}

% ----------------------------------------------------------------------
% PDF/X (print ou test), nécessaire pour l'impression sur Amazon par exemple.
\usepackage{iftex}
\ifXeTeX\else
\pdfobjcompresslevel=0%
\pdfminorversion=4%
\fi
% ======================================================================
\usepackage{\mainfolder python-common}
% ======================================================================

% Cette partie ne s'applique pas à la création des images en PDF
% Elle rajoute des informations aux PDF nécéssaires pour l'impression sur Amazon par exemple.
\ifXeTeX\else
\tikzifexternalizing{}{
  \pdfinfo{
    /Title (Python au lycée - tome 2)
	/Author (Arnaud Bodin)
	/Subject (algorithmes et programmation)
	/Keywords (informatique, mathématiques, algorithme, python)
    /GTS_PDFXVersion (PDF/X-3:2002)
  }%
  \pdfcatalog{
    /PageMode /UseNone
    /OutputIntents [ <<
      /Info (none)
      /Type /OutputIntent
      /S /GTS_PDFX
      /OutputConditionIdentifier (createspace.com)
      /RegistryName (http://www.color.org/)
    >> ]
  }%
  % http://tex.stackexchange.com/a/248911/831
  \makeatletter
  \edef\pwpt{\strip@pt\dimexpr0.996264009963\paperwidth\relax} %paper width in PS points
  \edef\phpt{\strip@pt\dimexpr0.996264009963\paperheight\relax} %paper height in PS points
  \makeatother
  \edef\mypdfpageattr{
  /MediaBox [0 0 \pwpt\space\phpt]^^J
  /BleedBox [0.00000 0.00000 \pwpt\space\phpt]^^J
  /CropBox [0 0 \pwpt\space\phpt]^^J
  /TrimBox [0.00000 0.00000 \pwpt\space\phpt]
  }
  \expandafter\pdfpageattr\expandafter{\mypdfpageattr}
} % fin \tikzifexternalizing
\fi
% ----------------------------------------------------------------------
\usepackage{etex} % http://tex.stackexchange.com/a/38609
% ----------------------------------------------------------------------
% Police par defaut

\ifPDFTeX\else
\setmainfont
     [ BoldFont       = texgyrepagella-bold.otf ,
       ItalicFont     = texgyrepagella-italic.otf ,
       BoldItalicFont = texgyrepagella-bolditalic.otf ]
     {texgyrepagella-regular.otf}
\fi


\usepackage[charter,cal=cmcal]{mathdesign}


% Police pour l'entete
%\newcommand{\entetefonte}[1]{\fontfamily{lmss}\selectfont\textls[-10]{\textsc{#1}}}
\newcommand{\entetefonte}[1]{\textls[-10]{\textsc{#1}}}

% Police pour les sections
\newcommand{\sectionfonte}{\fontfamily{phv}\bfseries}

% ----------------------------------------------------------------------
% Microtype
%\frenchbsetup{AutoSpacePunctuation=false}
\usepackage[kerning=true,activate={true,nocompatibility},final,tracking=true,spacing=true,factor=1100,stretch=20,shrink=20]{microtype}
% activate={true,nocompatibility} - activate protrusion and expansion
% final - enable microtype; use "draft" to disable
% tracking=true, kerning=true, spacing=true - activate these techniques
% factor=1100 - add 10% to the protrusion amount (default is 1000)
% stretch=10, shrink=10 - reduce stretchability/shrinkability (default is 20/20)
%\SetExtraKerning[unit=space]{encoding={T1}, family={bch}, series={m}, size={*}, shape={it}}{'={100,200}} 
%\SetExtraKerning[name=frenchdefault,context=french,unit=space]{encoding={OT1,T1,LY1}}{'= {1000,1000},}

% ----------------------------------------------------------------------
% Overfull/Underfull
\tolerance=250%
\emergencystretch=1.5em%
\hfuzz=2pt % Pas de warning overfull horizontal
\vfuzz=2pt % Pas de warning overfull vertical
\hbadness=10000 % Pas de warning underfull
\vbadness=10000 % Pas de warning underfull
% ----------------------------------------------------------------------
% saut de page
\usepackage{needspace}
% ----------------------------------------------------------------------
% reduction espace pour les environnement align and co
\usepackage{etoolbox}
\newcommand{\smalldisplayskips}{%
\setlength{\abovedisplayskip}{4pt plus 3pt}%
\setlength{\belowdisplayskip}{4pt plus 3pt}%
\setlength{\abovedisplayshortskip}{1pt plus 3pt}%
\setlength{\belowdisplayshortskip}{1pt plus 3pt}%
}
\appto{\normalsize}{\smalldisplayskips}
\appto{\small}{\smalldisplayskips}
\appto{\footnotesize}{\smalldisplayskips} 
% ----------------------------------------------------------------------
% Sommaire
\usepackage[titles]{tocloft}  % pour l'indentation des pages > 100
\setlength{\cftchapnumwidth}{2.55em}
\setlength{\cftsecindent}{2.55em}
\renewcommand\cftchapafterpnum{\vskip-4pt}
%\renewcommand\cftsecafterpnum{\vskip15pt}
%\usepackage{titletoc}  % Inutile
%\addto\captionsfrancais{\def\contentsname{Sommaire}}  % Ne marche pas !
\AtBeginDocument{\renewcommand{\contentsname}{Sommaire}}
% ----------------------------------------------------------------------


% ----------------------------------------------------------------------
% Définition d'un terme, mise en relief
% \newcommand{\defi}[1]{\underline{\underline{\textbf{\emph{#1}}}\index{#1}}}
% \usepackage{soul}
% \setul{1pt}{.4pt} % 1pt sous la baseline
% \newcommand{\defi}[1]{\ul{\textbf{\emph{#1}}}\index{#1}}
\usepackage[normalem]{ulem}
% \setlength{\ULdepth}{3pt}
% \newcommand{\defi}[1]{\uuline{\textbf{#1\hphantom{\!}}}\,\index{#1}}
\setlength{\ULdepth}{1.5pt}
\renewcommand{\ULthickness}{0.6pt}
\newcommand{\defi}[1]{\uline{\textbf{#1}}}
\newcommand{\evidence}[1]{\textbf{#1}}
\newcommand{\assertion}[1]{{\og\emph{#1}\fg}} % pour chapitre logique
% ----------------------------------------------------------------------
% Titre du livre
\newcommand{\montitre}[1]{
\pagestyle{empty}\thispagestyle{empty}
\begin{center}
\Huge #1
\end{center}
\begin{center}
\LogoExoSept{5}
\end{center}
\addtocontents{toc}{\protect\setcounter{tocdepth}{1}}
\tableofcontents
\pagestyle{chapterpage}
}
% ----------------------------------------------------------------------
% Les chapitres

% Pour figure titre \part python2
\usetikzlibrary{lindenmayersystems}
\usetikzlibrary[shadings]
\pgfdeclarelindenmayersystem{Sierpinski arrow}{
	\symbol{X}{\pgflsystemdrawforward}
	\symbol{Y}{\pgflsystemdrawforward}
	\rule{X -> Y-X-Y}
	\rule{Y -> X+Y+X}
}
\newcommand{\mylsystem}[1]{
	\begin{center} 
		\begin{tikzpicture}
		\def\k{#1+2}
		\begin{scope}[scale=0.25]
		\shadedraw[bottom color=red!80,top color=red!10,draw=red!80!black,rotate=30-30*(-1)^(\k)]  % rotate=30-30*(-1)^\k
		[lindenmayer system={Sierpinski arrow,angle=60,axiom=X,step=200pt/2^\k,order=\k}]
		lindenmayer system;
		\end{scope}
		\end{tikzpicture}  
	\end{center}
}
% Fin figure \part python2





\newcommand{\debutchapitres}{\clearpage\pagestyle{main}\thispagestyle{empty}\setcounter{page}{1}}

%\usepackage[explicit,pagestyles]{titlesec}  %-> Baculer dans scratch-common.sty-
\ifPDFTeX % PDFLaTeX
  \newcommand{\ugqfont}{\fontfamily{ugq}}
\else
  \newcommand{\ugqfont}{\fontspec{URWGrotesk-bold.ttf}}
  \fi
  
\titleformat{\part}[display]
  {\Huge\center\scshape}% format
  {\titlerule[3pt]\vspace{3pt}\titlerule[1pt]\vspace{20pt}%
  \partname~\thepart\\[25pt]%
  \mylsystem{\value{part}}
  %\includegraphics[width = 6cm]{divers/logoScratchAuCollege}\\
  \MakeLowercase{#1}}% label
  {15pt}% sep
  {% the title
  \titlerule[1pt]\vspace{3pt}\titlerule[3pt]\vspace{4pc}
  }  
  
  
\titleformat{\chapter}[block]
  {\raggedleft\ugqfont}% format
  {}% label
  {0pt}% sep
  {% the title
    \begin{tikzpicture}
      \node[black, above left, inner ysep=0pt, text width=.7\linewidth, align=right, node font=\huge]{\color{Firebrick4}#1};
      \draw[lightgray,line width=5pt] (0,-.1) -- (0,2.1);
      \ifstandalone % Style chapitre si on compile un seul chapitre : pas numéro
      \else % Style chapitre si on compile un seul livre
        \coordinate (C) at (2,1.7);
        \node[left, lightgray, inner sep=0pt] at (C) {\chaptertitlename};
        \node[left, lightgray, inner sep=0pt, scale=3] at ([yshift=-20pt]C) {~\thechapter};
      \fi
    \end{tikzpicture}
  }
  
\titleformat{name=\chapter,numberless}[block]
  {\raggedleft\ugqfont}% format
  {}% label
  {0pt}% sep
  {% the title
    \begin{tikzpicture}
      \node[black, above left, inner ysep=0pt, text width=.7\linewidth, align=right, node font=\huge]{\color{Firebrick4}#1};
      \draw[lightgray,line width=5pt] (0,-.1) -- (0,2.1);
    \end{tikzpicture}
  }

\newcommand{\chapitre}[2][]{
\clearemptydoublepage\thispagestyle{empty}
%\setcounter{theoreme}{0}
%\setcounter{proposition}{0}
%\setcounter{lemme}{0}
%\setcounter{corollaire}{0}
%\setcounter{definition}{0}
%\setcounter{exemple}{0}
%\setcounter{tp}{0}
%\setcounter{exercicecours}{0}
%\setcounter{equation}{0}
\setcounter{activite}{0}
%\setcounter{exemple}{0}
\setcounter{cours}{0}
\ifx&#1&\chapter{#2}\else\chapter[#1]{#2}\fi
}
\newcommand{\finchapitre}{}
% les sections

\makeatletter
\renewcommand{\thesection}{\@arabic\c@section}
% \renewcommand{\thechapter}{}
% \renewcommand{\chaptername}{}
\makeatother


\titleformat{\section}
  {\Needspace{10\baselineskip}\sectionfonte\selectfont\color{darkgray}\LARGE}% format
  {}% label
  {0pt}% sep
  {\thesection.~#1}

\titleformat{name=\section,numberless}
  {\Needspace{10\baselineskip}\sectionfonte\selectfont\color{darkgray}\LARGE}% format
  {}% label
  {0pt}% sep
  {#1}

% les sous-sections
\titleformat{\subsection}
  {\Needspace{5\baselineskip}\sectionfonte\selectfont\color{gray}\Large}% format
  {}% label
  {0pt}% sep
  {\thesubsection.~#1}

\titleformat{name=\subsection, numberless}
  {\Needspace{5\baselineskip}\sectionfonte\selectfont\color{gray}\Large}% format
  {}% label
  {0pt}% sep
  {#1}


% ----------------------------------------------------------------------
% Format en-têtes
%\usepackage{titleps} % chargé par titlesec, je suppse
\newpagestyle{main}[\small]{
  \setheadrule{0pt}%{.55pt}
  \sethead[{\large\textbf{\thepage}}\quad{\large\color{gray}\entetefonte{\chaptertitle}}]% even-left
          []% even-center
          []% even-right
          {}% odd-left
          {}% odd-center
          {{\large\color{gray}\entetefonte{\chaptertitle}}\quad{\large\textbf{\thepage}}}% odd-right
  %\renewcommand\makeheadrule{\color{lightgray}\rule[-.3\baselineskip]{\linewidth}{0.4pt}}
}
\newpagestyle{chapterpage}[\small]{
  \setheadrule{0pt}%
  \sethead[]% even-left
          []% even-center
          []% even-right
          {}% odd-left
          {}% odd-center
          {}% odd-right
  %\renewcommand\makeheadrule{\color{lightgray}\rule[-.3\baselineskip]{\linewidth}{0.4pt}}
}
\assignpagestyle{\chapter}{chapterpage}
\assignpagestyle{\part}{chapterpage}
\pagestyle{main}
% ----------------------------------------------------------------------
% Personnalisation pour les théorèmes,...
\usepackage{amsthm}

\newtheoremstyle{theoremeexo7}% name
{3pt}% Space above
{3pt}% Space below
{\itshape}% Body font
{}% Indent amount
{\bfseries}% Theorem head font
{.\newline}% Punctuation after theorem head
{0pt\Needspace{3\baselineskip}}% Space after theorem head
{}% Theorem head spec (can be left empty, meaning ‘normal’ )

\newtheoremstyle{definitionexo7}% name
{5cm plus 1cm minus 2cm}% Space above
{3pt}% Space below
{\upshape}% Body font
{}% Indent amount
{\bfseries}% Theorem head font
{.\newline}% Punctuation after theorem head
{0pt\Needspace{3\baselineskip}}% Space after theorem head
{}% Theorem head spec (can be left empty, meaning ‘normal’ )


%\theoremstyle{theoremeexo7}
%\newtheorem{theoreme}{Théorème}
%\newtheorem{proposition}{Proposition}
%\newtheorem*{propriete*}{Propriété}
%\newtheorem{lemme}{Lemme}
%\newtheorem{corollaire}{Corollaire}

\theoremstyle{definitionexo7}
\newtheorem{activite}{Activité}
\newtheorem{cours}{Cours}
\newtheorem*{algorithme}{Algorithme}
\newtheorem*{moncode}{Code}
\newtheorem{enigme}{\'Enigme}
\newtheorem*{exemple}{Exemple}
%\newtheorem*{solution}{Solution}

% legèrement personnalisées
\usepackage{tcolorbox}
\tcbuselibrary{theorems, skins, breakable}
\tcbuselibrary{listings}
\tcbset{listing engine=listings}


\tcbset{
  leftlined/.style 2 args={
    blanker, breakable,
    borderline west={#1}{0pt}{#2},left={#1*14/10+7pt},top=2.1ex, bottom=2.1ex
  }
}
%\tcolorboxenvironment{activite}{leftlined={5pt}{gray}}
%\tcolorboxenvironment{enigme}{leftlined={5pt}{gray}}
%\tcolorboxenvironment{solution}{leftlined={3pt}{lightgray}}
%\tcolorboxenvironment{exemple}{leftlined={3pt}{lightgray,decorate,decoration={name=zigzag}}}
%\tcolorboxenvironment{cours}{leftlined={3pt}{lightgray}}
%\tcolorboxenvironment{algorithme}{leftlined={3pt}{lightgray}}
%% Code 
%%\tcolorboxenvironment{moncode}{leftlined={5pt}{gray}}
%
%\tcolorboxenvironment{moncode}{fonttitle=\bfseries\upshape,fontupper=\itshape,
%colframe=red!50!black,colback=black!5!white,colbacktitle=green!20!white,coltitle=blue!75!black,titlerule=3mm}

\tcolorboxenvironment{activite}{leftlined={5pt}{gray}}
\tcolorboxenvironment{algorithme}{leftlined={3pt}{lightgray}}
\tcolorboxenvironment{exemple}{leftlined={3pt}{lightgray}}
\tcolorboxenvironment{cours}{leftlined={3pt}{lightgray,decorate,decoration={name=zigzag}}}



% ----------------------------------------------------------------------
% Encadrement auteurs
\newcommand{\auteurs}[1]{\vspace*{\fill}\textbf{\ugqfont\selectfont\color{gray}Auteurs du chapitre }#1
}

% ----------------------------------------------------------------------
% Encadrement des formules
\usepackage{fancybox}
%\setlength{\fboxsep}{7pt}
%\newcommand{\mybox}[1]{\begin{center}\shadowbox{#1}\end{center}}
%\newcommand{\myboxinline}[1]{\raisebox{-2ex}{\shadowbox{#1}}}
% ---- autre version
\usepackage{varwidth}
\tikzset{
  block/.style = {
    minimum height=1em,
    inner xsep=.8em, inner ysep=.45em,
    draw=black, thick, %rounded corners,
    execute at begin node={\begin{varwidth}{\linewidth}\begin{center}},
    execute at end node={\end{center}\end{varwidth}}
  },
  inline/.style = {
    inner sep=.35em, draw=black, thick,
  }
}
\newcommand{\mybox}[1]{\begin{center}\tikz{\node[block]{#1};}\end{center}}
\newcommand{\myboxinline}[1]{\tikz[baseline=(X.base)]{\node[inline](X){#1};}}

% ----------------------------------------------------------------------
% Alignement vertical des item
% \renewcommand{\labelitemi}{\raisebox{0ex}{\footnotesize$\bullet$}\hskip0.1em}
% \renewcommand{\labelitemii}{\raisebox{0.12ex}{$-$}\hskip0.1em}
% ----------------------------------------------------------------------
% simple affichage des href pour l'impression
\providecommand{\href}[2]{#2}

% --- Liens vers vidéo Youtube
% \usepackage{qrcode}
% \newcommand{\insertvideo}[2]{
%   \qrcode[height=1cm]{https://www.youtube.com/watch/?v=\detokenize{#1}}\hspace{3mm}
%   \parbox[c][1cm]{\textwidth-1cm}{\fontsize{10pt}{7pt}\selectfont{\ugqfont\selectfont{\color{gray} Vidéo~}#2}\\[1pt]\texttt{\color{gray}{\small https://www.youtube.com/watch/?v=}\detokenize{#1}}}
% }
\newcommand{\insertvideo}[2]{}
\newcommand{\video}[1]{\insertvideo{#1}{--- Titre manquant ---}}

% --- Liens vers les fiches d'exercices
\newcommand{\insertfiche}[2]{
  \qrcode[height=1cm]{http://exo7.emath.fr/ficpdf/\detokenize{#1}}\hspace{3mm}
  \parbox[c][1cm]{\textwidth-1cm}{\fontsize{10pt}{7pt}\selectfont{\ugqfont\selectfont{\color{gray} Fiche d'exercices~}#2}\\[1pt]\texttt{\color{gray}{\small http://exo7.emath.fr/ficpdf/}\detokenize{#1}}}
}
\renewcommand{\insertfiche}[2]{}
\newcommand{\mafiche}[1]{\insertfiche{#1}{--- Titre manquant ---}}

% --- Indication des couleurs (pour couleur #1, pour nb #2)
\newcommand{\couleurnb}[2]{#2}


% ----------------------------------------------------------------------
% Constantes pour l'inclusion des figures
\newcommand{\myscale}{0.8} 



% Pour afficher/cacher les solutions 
\usepackage{comment}

%\newenvironment{code}{\begin{center}}{\end{center}}
\newenvironment{solution}{\bigskip\begin{minipage}{0.8\textwidth}\textbf{Solution.}}{\end{minipage}}
%\tcolorboxenvironment{solution}{leftlined={3pt}{lightgray,decorate,decoration={name=zigzag}}}

\def\myzero{0}
\ifx \displaysolutions \myzero \excludecomment{solution}\let\endsolution\relax\else\fi
\ifx \displaysolutions \myzero \excludecomment{code}\let\endsolution\relax\else\fi

\newcommand{\onesolution}[3]{
\tcbox[center title,fonttitle = \bfseries,title={#1 \\ #2},halign=center,box align=top,nobeforeafter,enlarge by=2mm]
%,enlarge top initially by=3mm,enlarge bottom finally by=3mm,show bounding box]
{#3}
%,nobeforeafter
}


% --- les figures avec échelle éventuel
\renewcommand{\myfigure}[2]{% entrée : échelle, fichier(s) figure à inclure
\begin{center}\small
\tikzstyle{every picture}=[scale=0.95*#1]% mise en échelle + 10% (automatiquement annulé à la fin du groupe)
#2
\end{center}}


% ----------------------------------------------------------------------
% Algorithmes
% bloc de code (\insertcode)

\newcommand{\insertcode}[2]{%
\begin{tcolorbox}[title={#2},breakable,fonttitle=\bfseries\upshape,fontupper=\itshape, colback=black!5!white,colbacktitle=black!10!white,coltitle=red!75!black,toptitle=5pt,bottomtitle=4pt,
arc=0pt,outer arc=0pt,
left=5pt,right=0pt,top=2pt,
arc=0pt,
boxrule=0pt,
leftrule=5pt,
colframe=black!50]%
\myinputlisting{#1}% 
\end{tcolorbox}
}



%\newcommand{\insertcode}[2]{%
%\begin{moncode}[#2]%
%\myinputlisting{#1}% 
%\end{moncode}
%}


%\newcommand{\fonction}[2][]{
%\begin{center}
%\begin{minipage}{0.7\textwidth}
%Titre : #1 \\
%#2
%\end{minipage}
%\end{center}
%}

\newenvironment{fonction}[1][\unskip]{%
\begin{center}
\begin{minipage}{0.8\textwidth}
\begin{tcolorbox}[colback=white,colframe=lightgray,coltitle=black,title=#1,halign title=center]
}
{\end{tcolorbox}
\end{minipage}
\end{center}}

\newenvironment{fonctionpython}[1][\unskip]{%
\begin{center}
\begin{minipage}{0.8\textwidth}
\begin{tcolorbox}[title=#1,halign title=center]
}
{\end{tcolorbox}
\end{minipage}
\end{center}}


\newenvironment{code}[1][\unskip]{%
\begin{center}
\begin{minipage}{0.49\textwidth}
\begin{tcolorbox}[title=#1,halign title=left,subtitle style={boxrule=0.6pt,
colback=lightgray}]
%\begin{lstlisting}
}
{
%\end{lstlisting}
\end{tcolorbox}
\end{minipage}
\end{center}}

\newcommand{\soustitre}[1]{\tcbsubtitle{#1}}

\newcommand{\objectifs}[1]{
\sauteligne
\begin{center}
\begin{minipage}{0.9\textwidth}
\emph{#1}
\end{minipage}
\end{center}
}

% code en ligne (\codeinline)
%\newcommand{\codeinline}[1]{\lstinline[showstringspaces=false]!#1!}
\newcommand{\codeinline}[1]{\lstinline[showstringspaces=false,breaklines=true,breakatwhitespace=true]!#1!}
\newcommand{\ci}[1]{\codeinline{#1}}

% Ligne bien centrée ()sans utiliser \centerline)
\newenvironment{nscenter}
{\parskip=0pt\par\nopagebreak\centering}
{\par\noindent\ignorespacesafterend}
\newcommand{\mycenterline}[1]{\begin{nscenter}#1\end{nscenter}}
%\renewcommand{\centerline}[1]{\erreur}  % A commenter après débugage
