% Author     : Christophe Poulain
% licence    : Released under the LaTeX Project Public License v1.3c
% or later, see http://www.latex-project.org/lppl.txtf
\NeedsTeXFormat{LaTeX2e}%
\def\filedate{2023/08/17}%
\let\PfMfiledate\filedate%
\def\fileversion{0.4}%
\let\PfMfileversion\fileversion%
\ProvidesPackage{ProfMaquette}[\filedate\space v\fileversion\space Maquettage de documents]%

% packages nécessaires
\RequirePackage{verbatim}

\RequirePackage{calc}

\RequirePackage{xlop}

\RequirePackage{varwidth}

\RequirePackage{tcolorbox}
\tcbuselibrary{breakable,external,fitting,hooks,magazine,poster,raster,skins,theorems,listings,listingsutf8}

\RequirePackage{xinttools}

\RequirePackage{listofitems}
\RequirePackage{array}
\RequirePackage{datatool}

\RequirePackage{modulus}

%définition des clés
\RequirePackage{simplekv}
\setKVdefault[Boulot]{Cours=false,Base=false,BaseHTML=false,Fiche=false,DM=false,DS=false,IE=false,Kholle=false,RecapBrevet=false,PdT=false,Parcours=false,ParcoursPerso=false,CorrigeApres=false,CorrigeFin=false,CorrigeAll=false,Brevet=false,BaremeComplet=false,BaremeGlobal=false,CorrectionColonnes=false,Epaisseur=0pt,AllDirect=false,AllPouce=false}%Les deux derniers pour essayer de mettre tous les exercices d'un même fichier en mode Reponse, en mode Indice. Pas implanté, pas testé
\defKV[Boulot]{Colonnes=\setKV[Boulot]{CorrectionColonnes}}%
\setKVdefault[Cours]{Couleur=Crimson,Theme=Trace écrite,NomExercice=Exercice}%
\setKVdefault[DM]{Numero=1,Date=\today,Classe={},Niveau=3,Code=\jobname}%
\setKVdefault[DS]{Numero=1,Date=\today,Classe={},Niveau=3,Code=\jobname,Calculatrice=false,Sujet=A}%
\setKVdefault[Fiche]{Theme=Les nombres décimaux,Date=\today,Niveau=6,Classe={},Code={},NomExercice=Exercice}%
\setKVdefault[PdT]{Theme=Les nombres décimaux,Date=\today,Niveau=6,Classe={},Code={},NomExercice=Exercice}%
\setKVdefault[Parcours]{Theme=Les nombres décimaux,Date=\today,Niveau=6,Classe={},Code={},NomExercice=Exercice,Vertical=false}%
\setKVdefault[ParcoursPerso]{Fichier={}}%
\setKVdefault[IE]{Nom=\'Evaluation,Sujets=false,Theme={},Numero=1,Date=\today,Classe={},Niveau=3,Code=\jobname,Calculatrice=false}%
\defKV[IE]{Sujet=\setKV[IE]{Sujets}}%
\setKVdefault[RecapBrevet]{Theme=Les nombres décimaux,Date=\today,Code={},NomExercice=Exercice}%
\setKVdefault[Kholle]{Sujet=1,Numero=1,Date=15,Niveau=TSI-1,Code=\jobname,Lieu=Lycée
  du Hainaut (Valenciennes),NomEleve=Albert Einstein,CR=false}%
\setKVdefault[ClesExercices]{PasTitre=false,AffichageSource=false,BaremeTotal,BaremeDetaille=false,MotPoint=point,AffichageTitre=false,Calculatrice=true,Oral=false,Difficulte=0,AffichageCompetence=false,PasCorrige=false,Direct=false,Pouce=false,Parcours=false,Background=white,Cadre=gray!50}%
\defKV[ClesExercices]{Source=\setKV[ClesExercices]{AffichageSource}}%
\defKV[ClesExercices]{Titre=\setKV[ClesExercices]{AffichageTitre}}%
\defKV[ClesExercices]{Competence=\setKV[ClesExercices]{AffichageCompetence}}%
\defKV[ClesExercices]{Trajet=\setKV[ClesExercices]{Parcours}}%
\setKVdefault[SchemaParcours]{Colonnes=100,Rayon=2.5mm,Largeur=7.5mm,Couleur=LightSteelBlue,Csv=false}%
% Definition de longueurs
\newlength\PfMLargeurSchemaParcours
\newlength{\PfMHauteurAppreciation}
\setlength{\PfMHauteurAppreciation}{5cm}
\newlength{\PfMHauteurRemarque}
\setlength{\PfMHauteurRemarque}{5cm}
%définition des compteurs
\newcounter{PfMExo}%Numéro des exercices
\setcounter{PfMExo}{0}%
\newcounter{PfMExoSol}%Pour repérer les valeurs aléatoires construites automatiquement dans la solution
\setcounter{PfMExoSol}{0}%
\newcounter{PfMCorrigeParcoursPerso}%affichage des solutions ou pas dans le cadre d'un parcours perso
\setcounter{PfMCorrigeParcoursPerso}{0}%
\newcounter{PfMSOS}%Numéro des SOS dans les parcours fléchés
\setcounter{PfMSOS}{0}%
\newcounter{PfMAlea}%Pour repérer les valeurs aléatoires construites automatiquement
\setcounter{PfMAlea}{0}%
\newcounter{RetiensSol}%Pour l'affichage des solutions
\newcounter{RetiensRep}%Pour l'affichage des reponses.
\newcounter{RetiensInd}%Pour l'affichage des indices.
\setcounter{RetiensSol}{0}%
\setcounter{RetiensRep}{0}%
\setcounter{RetiensInd}{0}%
\newcounter{PfMNbLogo}%Pour décaler les logos sur la ligne haute de tcolorbox
\setcounter{PfMNbLogo}{0}%
\newcounter{PfMMaquette}%Pour compter le nombres de "travaux"
\setcounter{PfMMaquette}{0}%
\newcounter{PfMSection}%Pour le barème et ne pas faire obstruction à section
\setcounter{PfMSection}{0}%
\newcount\Orientation%Pour le schéma des parcours
% couleurs
\colorlet{PfMColCpt}{gray!50}
\colorlet{PfMColSrc}{gray!50}
%\colorlet{PfMCadreFiche}{gray!50}
\colorlet{PfMCadreDM}{gray!85}
\colorlet{PfMCadreParcours}{gray!50}
% Définitions de commandes
\newcommand\PfMIENom{\dotfill}
\newcommand\PfMIEPrenom{\dotfill}
\newcommand\PfMIEClasse{\dotfill}
\newcommand\PfMCompNA{NA}
\newcommand\PfMCompECA{ECA}
\newcommand\PfMCompA{A}
% Pour la gestion de l'aléatoire
% afin de permettre d'utiliser le même exercice.
% Extraire des sommets
\newcounter{PfMSommetcount}%
\newcommand\ChoixSommet[2]{%
  % #1 le nombre de sommets
  % #2 le nom de base des commandes qui sauvegarde les noms
  \xdef\CANSGFoo{A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}%
  \MelangeListe{\CANSGFoo}{#1}%
  \ignoreemptyitems
  \readlist*\PfMSommet{\faa}%
  \reademptyitems
  \xintFor* ##1 in{\xintSeq{1}{#1}}\do{%
    \setcounter{PfMSommetcount}{##1}
    \expandafter\xdef\csname p\Alph{PfMSommetcount}\endcsname{\PfMSommet[##1]}%
    \expandafter\xdef\csname #2\Alph{PfMExo}\alph{PfMSommetcount}\endcsname{\PfMSommet[##1]}%
  }%
}%
%%%

\ifdef{\VariableAlea}{%
  \RenewDocumentCommand\VariableAlea{s m m}{%
    \IfBooleanTF{#1}{%
      \stepcounter{PfMAlea}%
      \expandafter\xdef\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname{\fpeval{#3}}%
      \expandafter\xdef\csname #2\endcsname{\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname}%
    }{%
      \xdef#2{\fpeval{#3}}%
    }%
  }%
}{%
  \NewDocumentCommand\VariableAlea{s m m}{%
    \IfBooleanTF{#1}{%
      \stepcounter{PfMAlea}%
      \expandafter\xdef\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname{\fpeval{#3}}%
      \expandafter\xdef\csname #2\endcsname{\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname}%
    }{%
      \xdef#2{\fpeval{#3}}%
    }%
  }%
}%

\ifdef{\ChoixAlea}{%
  \RenewDocumentCommand\ChoixAlea{s o m m m}{%
    \IfBooleanTF{#1}{%
      \stepcounter{PfMAlea}%
      \IfNoValueTF{#2}{%
        \expandafter\xdef\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname{\fpeval{randint(#3,#4)}}%
        \expandafter\xdef\csname #5\endcsname{\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname}%
      }{%
        \expandafter\xdef\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname{\fpeval{round(randint(#3,#4)+rand(),#2)}}%
        \expandafter\xdef\csname #5\endcsname{\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname}%
      }%
    }{%
      \IfNoValueTF{#2}{%
        \xdef#5{\fpeval{randint(#3,#4)}}%
      }{%
        \xdef#5{\fpeval{round(randint(#3,#4)+rand(),#2)}}%
      }%
    }%
  }%
}{%
  \NewDocumentCommand\ChoixAlea{s o m m m}{%
    \IfBooleanTF{#1}{%
      \stepcounter{PfMAlea}%
      \IfNoValueTF{#2}{%
        \expandafter\xdef\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname{\fpeval{randint(#3,#4)}}%
        \expandafter\xdef\csname #5\endcsname{\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname}%
      }{%
        \expandafter\xdef\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname{\fpeval{round(randint(#3,#4)+rand(),#2)}}%
        \expandafter\xdef\csname #5\endcsname{\csname valeur\Alph{PfMExo}\alph{PfMAlea}\endcsname}%
      }%
    }{%
      \IfNoValueTF{#2}{%
        \xdef#5{\fpeval{randint(#3,#4)}}%
      }{%
        \xdef#5{\fpeval{round(randint(#3,#4)+rand(),#2)}}%
      }%
    }%
  }%
}% 

\NewDocumentCommand\RestoreChoixAlea{m}{%
  \stepcounter{PfMAlea}%
  \expandafter\xdef\csname #1\endcsname{\csname valeur\Alph{PfMExoSol}\alph{PfMAlea}\endcsname}
}%

\NewDocumentCommand\RestoreSommet{mm}{%
  % #1 le nombre de sommets
  % #2 le nom de base utilisé lors de la création des sommets
  \xintFor* ##1 in{\xintSeq{1}{#1}}\do{%
    \setcounter{PfMSommetcount}{##1}
    \expandafter\xdef\csname p\Alph{PfMSommetcount}\endcsname{\csname #2\Alph{PfMExoSol}\alph{PfMSommetcount}\endcsname}
  }%
}%

% Définitions pour les compétences
\NewDocumentCommand\Competences{O{3}m}{%
  \setsepchar[*]{/}\ignoreemptyitems%
  \readlist*\ListeCompetence{#2}
  \ifnum#1=3\relax
  \buildtabcompetences%
  \else
  \Buildtabcompetences{#1}{\ListeCompetencelen}%
  \fi
}

\def\buildtabcompetences{%
  \[\begin{tabular}{|p{0.70\linewidth}|*{3}{>{\centering\arraybackslash}p{0.05\linewidth}|}}
      \cline{2-4}
      \multicolumn{1}{c|}{}&\PfMCompNA&\PfMCompECA&\PfMCompA\\
      \hline
      \xintFor* ##1 in {\xintSeq {1}{\ListeCompetencelen}}\do{\ListeCompetence[##1]&&&\\\hline}
    \end{tabular}
  \]
}%

\newlength\PfMLargeurComp

\def\Buildtabcompetences#1#2{%
  \setlength{\PfMLargeurComp}{\fpeval{0.85-0.05*#1}\linewidth-\fpeval{2*(#1-3)}\tabcolsep}%
  \[\begin{tabular}{|p{\PfMLargeurComp}|*{#1}{>{\centering\arraybackslash}p{0.05\linewidth}|}}%
      \cline{2-\fpeval{1+#1}}%
      \multicolumn{1}{c|}{}\xintFor* ##1 in{\xintSeq{1}{#1}}\do{%
      &\ListeCompetence[##1]}\\%
      \hline%
      \xintFor* ##1 in {\xintSeq {\fpeval{#1+1}}{\ListeCompetencelen}}\do{\ListeCompetence[##1]%
      \xintFor* ##2 in {\xintSeq{1}{#1}}\do{&}%
      \\\hline%
      }%
    \end{tabular}%
  \]%
}%
  
%%%%
%%% Définitions des environnements pour la correction
\makeatletter
\newwrite\verbatim@out

\def\SolutionWrite{%
  \setcounter{PfMAlea}{0}%
  \@bsphack
  \immediate\openout \verbatim@out "\jobname-Ex\thePfMExo.sol" %
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\verbatim@out{\the\verbatim@line}}%
  \verbatim@start}
\def\endSolutionWrite{\immediate\closeout\verbatim@out\@esphack}

\def\SolutionWritePP{%
  \@bsphack
  \immediate\openout \verbatim@out "\jobname-PPEx\thePfMExo.sol" %
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\verbatim@out{\the\verbatim@line}}%
  \verbatim@start}
\def\endSolutionWritePP{\immediate\closeout\verbatim@out\@esphack}

\def\ReponseWrite{%
  \@bsphack
  \immediate\openout \verbatim@out "\jobname-Ex\thePfMExo.rep" %
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\verbatim@out{\the\verbatim@line}}%
  \verbatim@start}
\def\endReponseWrite{\immediate\closeout\verbatim@out\@esphack}

\def\IndiceWrite{%
  \@bsphack
  \immediate\openout \verbatim@out "\jobname-Ex\thePfMExo.cdp" %coup de pouce
  \let\do\@makeother\dospecials
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\verbatim@out{\the\verbatim@line}}%
  \verbatim@start}
\def\endIndiceWrite{\immediate\closeout\verbatim@out\@esphack}
\makeatother

\def\SolutionNow{\textbf{Correction de l'exercice --}\par\nopagebreak\setcounter{PfMExoSol}{\thePfMExo}\setcounter{PfMAlea}{0}}
\def\endSolutionNow{\par\goodbreak}

\def\ReponseNow{\textbf{Réponse(s) de l'exercice --}\par\nopagebreak}
\def\endReponseNow{\par\goodbreak}

\def\IndiceNow{\textbf{Indice(s) --}\par\nopagebreak}
\def\endIndiceNow{\par\goodbreak}

\NewDocumentEnvironment{Solution}{}{}{}%
\NewDocumentEnvironment{Reponse}{}{}{}%
\NewDocumentEnvironment{Indice}{}{}{}%

\NewDocumentCommand\PfMChoixAffichageCorrection{}{%
  \ifboolKV[Boulot]{CorrigeAll}{%
      \let\Solution\SolutionWrite\let\endSolution\endSolutionWrite
      \let\Reponse\ReponseWrite\let\endReponse\endReponseWrite
      \let\Indice\IndiceWrite\let\endIndice\endIndiceWrite
  }{%
  \ifboolKV[Boulot]{ParcoursPerso}{%
    \let\Solution\SolutionWritePP\let\endSolution\endSolutionWritePP
  }{%
    \ifnum\theRetiensSol=1\relax
    \let\Reponse\comment\let\endReponse\endcomment
    \let\Indice\comment\let\endIndice\endcomment
    \ifboolKV[Boulot]{CorrigeFin}{%
      \let\Solution\SolutionWrite\let\endSolution\endSolutionWrite
    }{\ifboolKV[Boulot]{CorrigeApres}{%
        \let\Solution\SolutionNow\let\endSolution\endSolutionNow
      }{}%
    }%
    \else
    \let\Solution\comment\let\endSolution\endcomment
    \let\Indice\comment\let\endIndice\endcomment
    \ifnum\theRetiensRep=1\relax
    \ifboolKV[Boulot]{CorrigeFin}{%
      \let\Reponse\ReponseWrite\let\endReponse\endReponseWrite
    }{\ifboolKV[Boulot]{CorrigeApres}{%
        \let\Reponse\ReponseNow\let\endReponse\endReponseNow
      }{}%
    }%
    \else
    \let\Solution\comment\let\endSolution\endcomment
    \let\Reponse\comment\let\endReponse\endcomment
    \ifnum\theRetiensInd=1\relax
    \ifboolKV[Boulot]{CorrigeFin}{%
      \let\Indice\IndiceWrite\let\endIndice\endIndiceWrite
    }{\ifboolKV[Boulot]{CorrigeApres}{%
        \let\Indice\IndiceNow\let\endIndice\endIndiceNow
      }{}%
    }%
    \fi
    \fi
    \fi
  }%
  }%
}%

\NewDocumentCommand\PfMChoixCorrection{}{%
  \setcounter{RetiensSol}{0}%
  \setcounter{RetiensRep}{0}%
  \setcounter{RetiensInd}{0}%
  \ifboolKV[Boulot]{CorrigeFin}{%
%    \ifboolKV[Boulot]{CorrigeAll}{%
%      \setcounter{RetiensSol}{1}%
%      \setcounter{RetiensRep}{1}%
%      \setcounter{RetiensInd}{1}%
%    }{%
      \setcounter{RetiensSol}{1}%
      \setcounter{RetiensRep}{0}%
      \setcounter{RetiensInd}{0}%
%    }%
  }{\ifboolKV[Boulot]{CorrigeApres}{%
      \setcounter{RetiensSol}{1}%
      \setcounter{RetiensRep}{0}%
      \setcounter{RetiensInd}{0}%
    }{}%
  }%
  \ifboolKV[ClesExercices]{PasCorrige}{%
    \setcounter{RetiensSol}{0}%
    \setcounter{RetiensRep}{0}%
    \setcounter{RetiensInd}{0}%
  }{%
    \ifboolKV[ClesExercices]{Direct}{%
      \setcounter{RetiensSol}{0}%
      \setcounter{RetiensRep}{1}%
      \setcounter{RetiensInd}{0}%
    }{%
      \ifboolKV[ClesExercices]{Pouce}{%
        \setcounter{RetiensSol}{0}%
        \setcounter{RetiensRep}{0}%
        \setcounter{RetiensInd}{1}%
      }{}%
    }%
  }%
}%

\NewDocumentCommand\PfMAffichageCorrige{}{%
  \xintFor* ##1 in{\xintSeq{1}{\thePfMExo}}\do{%
    \setcounter{PfMExoSol}{##1}%
    \setcounter{PfMAlea}{0}%
    \IfFileExists{\jobname-Ex##1.sol}{\textbf{Corrigé de l'exercice ##1}\par\nopagebreak\input{\jobname-Ex##1.sol}\par\goodbreak}{\IfFileExists{\jobname-Ex##1.rep}{\textbf{Réponse(s) seule(s) de l'exercice ##1}\par\nopagebreak\input{\jobname-Ex##1.rep}\par\goodbreak}{%
        \IfFileExists{\jobname-Ex##1.cdp}{\textbf{Indice(s) pour l'exercice ##1}\par\nopagebreak\input{\jobname-Ex##1.cdp}\par\goodbreak}{}
      }%
    }%
  }%
}%
%%%%%%%% BAREME %%%%%%%%%%%
%% Bareme
\newlength{\brouillon}

\newcommand*\FranPt[1]{%
  \ifboolKV[ClesExercices]{BaremeDetaille}{%
    \color{gray!75}(\num{#1})%
  }{}
}

\makeatletter
\newcommand\BaremeEspace@Marge{15pt}
\newcommand\BaremeEspace@MargeCorrection{0pt}
%\f@size est la taille par défaut de la fonte.

\newcommand*{\brm}[1]{%
  \ifboolKV[Boulot]{Fiche}{}{%
    \immediate\write\@auxout{%
      \string\opadd*{#1}%
      {total\the\c@PfMSection-\the\c@PfMExo}%
      {total\the\c@PfMSection-\the\c@PfMExo}%
      \string\opcopy{\thePfMExo}{TotalExo}%
    }%
    \ifboolKV[ClesExercices]{BaremeDetaille}{%
      \setlength{\brouillon}{\BaremeEspace@Marge-\linewidth+\textwidth}%
      \makebox[0pt][r]{\footnotesize\FranPt{#1}\hspace*{\brouillon}}%
      \ignorespaces%
    }%
  }{}%
}%

\newcommand*{\Brm}[1]{%
  \ifboolKV[Boulot]{Fiche}{}{%
    \setlength{\brouillon}{\BaremeEspace@MargeCorrection-\linewidth+\textwidth}%
    \makebox[0pt][r]{\footnotesize\color{gray!75}(\opprint{#1})\hspace*{\brouillon}}%
    \ignorespaces%
  }%
}%

\newcommand\total[1]{%
  \@ifundefined{Op@total#1}{recompilez}%
  {%
    \opunzero{total#1}% on ne sait jamais
    \opset{decimalsepsymbol={,}}
    \opprint{total#1}~%
    \opset{decimalsepsymbol={.}}
    \opcmp{total#1}{2}%
    \ifopge \useKV[ClesExercices]{MotPoint}s\else \useKV[ClesExercices]{MotPoint}\fi
  }%
}

\newcommand\NoteFinale{%
  \@ifundefined{Op@TotalExo}{Recompilez}%
  {%
    \opexport{TotalExo}{\TotalExo}%Le total d'exercices est \TotalExo.
    \xdef\NoteTotale{0}%
    \xintFor* ##1 in {\xintSeq{1}{\TotalExo}}\do{%
     \opexport{total##1-##1}{\totalUn}%
     \xdef\NoteTotale{\fpeval{\NoteTotale+\totalUn}}%
   }%
   \NoteTotale%
  }%
}%

\newcommand\TableauRecapitulatif{%
  \@ifundefined{Op@TotalExo}{Recompilez}%
  {%
    \opexport{TotalExo}{\TotalExo}%
    \begin{tabular}{|c|c|}
      \hline
      \multicolumn{2}{|c|}{Barème récapitulatif}\\
      \hline
      \xintFor* ##1 in {\xintSeq{1}{\TotalExo}}\do{
      Exercice ##1&\total{##1-##1}\\
      }
      \hline
    \end{tabular}
  }
}%
\makeatother
%%%%%%%% FIN BAREME %%%%%%%%%%%%%%%%
%%%Style des différents documents
\tcbset{%
  tikzbase/.style={%
    before skip=1em,
    enhanced,
    breakable,
    bicolor,
    colback=LightSteelBlue,
    colbacklower=white,
    frame hidden,
  }
}

\tcbset{%
  tikzbasehtml/.style={%
    before skip=1em,
    enhanced,
    breakable,
    frame hidden,
  }
}

\makeatletter
%%% Base des exercices DM
\tcbset{%
  BaseDM/.style={%
    lowerbox=ignored,
    enhanced,
    breakable,
    colback=white,
    colframe=PfMCadreDM,
    fonttitle=\bfseries\sffamily,
    title=Exercice \thePfMExo,
    attach boxed title to top left={xshift=0.2mm},
    boxed title size=standard,
    boxed title style={%
      empty,
      rounded corners=north, 
      boxrule=0pt,
      bottom=0pt
    },
    underlay boxed title={%
      \filldraw[rounded corners, gray!85, line width=.5mm]
      (title.south east)--++(90:\tcboxedtitleheight)--++(183:\tcboxedtitlewidth)--++(-90:\tcboxedtitleheight)|-cycle;
    },%
    overlay unbroken and first={%
      \ifboolKV[ClesExercices]{AffichageSource}{%
        \node[%
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=gray!50,
        ]
        at (frame.south east)
        {\scriptsize\sffamily \useKV[ClesExercices]{Source}};
      }{}%
      \ifboolKV[ClesExercices]{BaremeTotal}{%
        \node[
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=black,
        font=\bfseries
        ]
        at (frame.north east)
        {\footnotesize\sffamily \total{\the\c@PfMSection-\the\c@PfMExo}};
      }{}
    }%
  }%
}%

\newenvironment{exerciceDM}[1][]{%
  \setcounter{PfMAlea}{0}%
  \useKVdefault[ClesExercices]
  \setKV[ClesExercices]{#1}
  \refstepcounter{PfMSection}
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}
  \immediate\write\@auxout{%
    \string\opcopy{\thePfMExo}{TotalExo}%
  }
  \ifboolKV[ClesExercices]{BaremeTotal}{%
    \immediate\write\@auxout{%
      \string\opcopy{0}{total\the\c@PfMSection-\the\c@PfMExo}%
    }}{}
  \tcolorbox[%
  BaseDM,
  label is label,
  label=page-\thePfMMaquette-\thePfMExo%
  ]
}{%
  \endtcolorbox%
}

%%Parcours

%\makeatletter
\NewDocumentCommand\AfficheParcours{om}{%
  \useKVdefault[SchemaParcours]%
  \setKV[SchemaParcours]{#1}%
  \setlength{\PfMLargeurSchemaParcours}{\useKV[SchemaParcours]{Largeur}}%
  \colorlet{PfMCouleurSP}{\useKV[SchemaParcours]{Couleur}}%
  \@ifundefined{Op@\Alph{PfMMaquette}-ParcoursTotalExo}{Recompilez pour obtenir le parcours.}%
  {%
    \opexport{\Alph{PfMMaquette}-ParcoursTotalExo}{\ParcoursTotalExo}%
%    Total Exo : \ParcoursTotalExo
    \setsepchar{,}\ignoreemptyitems%
    \readlist*\ListeExercicesPA{\csname \Alph{PfMMaquette}Parcours#2\endcsname}
    \reademptyitems%
    \xdef\PfMFooTestA{\ListeExercicesPA[1]}%
    \ifnum\ListeExercicesPAlen>1\relax%
    \xintFor* ##1 in{\xintSeq{2}{\ListeExercicesPAlen}}\do{%
      \xdef\PfMFooTestA{\PfMFooTestA,\ListeExercicesPA[##1]}%
    }%
    \fi%
    \ifboolKV[Parcours]{Vertical}{%
      \BuildParcoursTikzV{\ParcoursTotalExo}{\PfMFooTestA}%
    }{%
      \BuildParcoursTikz{\ParcoursTotalExo}{\PfMFooTestA}%
    }%
  }%
}%
%\makeatother

\NewDocumentCommand\BuildParcoursTikzV{mm}{%
  \setsepchar{,}%
  \readlist*\ListeRetiens{#2}%
    \begin{tikzpicture}[remember picture,overlay]
      % On place les points
      \coordinate[xshift=-1cm,yshift=-4cm] (A-1) at (current page.north east);%
      \xintFor* ##1 in{\xintSeq{2}{#1}}\do{%
        \coordinate[yshift=-1cm] (A-##1) at (A-\fpeval{##1-1});%
      }%
      \foreach\i in{2,...,#1}{%
        \draw[dashed] (A-\fpeval{\i-1}) -- (A-\i);%
      }%
      \xintFor* ##1 in{\xintSeq{1}{#1}}\do{%
        \draw[fill=white,dashed](A-##1) circle (2.5mm) node{##1};%
      }%
      \foreachitem\compteur\in\ListeRetiens{%
        \draw[fill=PfMCouleurSP](A-\ListeRetiens[\compteurcnt]) circle (2.5mm) node{\ref{page-\thePfMMaquette-\ListeRetiens[\compteurcnt]}};%
      }%
    \end{tikzpicture}%
}%

\NewDocumentCommand\BuildParcoursTikz{mm}{%
  \setsepchar{,}%
  \readlist*\ListeRetiens{#2}%
  \begin{tikzpicture}
    % On place les points
    \Orientation=1\relax
    \coordinate (A-1) at (0,0);%
    \xintFor* ##1 in{\xintSeq{2}{#1}}\do{%
      \modulo{##1}{\useKV[SchemaParcours]{Colonnes}}%
      \ifnum\remainder=0\relax%
      \Orientation=\numexpr\Orientation*(-1)\relax%
      \coordinate[yshift=-1cm] (A-##1) at (A-\fpeval{##1-1});%
      \else%
      \coordinate[xshift=\the\Orientation*\PfMLargeurSchemaParcours] (A-##1) at (A-\fpeval{##1-1});%
      \fi%
    }%
    \foreach\i in{2,...,#1}{%
      \draw[dashed] (A-\fpeval{\i-1}) -- (A-\i);%
    }%
    \xintFor* ##1 in{\xintSeq{1}{#1}}\do{%
      \draw[fill=white,dashed](A-##1) circle (2.5mm) node{##1};%
    }%
    \foreachitem\compteur\in\ListeRetiens{%
      \draw[fill=PfMCouleurSP](A-\ListeRetiens[\compteurcnt]) circle (2.5mm) node{\ref{page-\thePfMMaquette-\ListeRetiens[\compteurcnt]}};%
    }%
  \end{tikzpicture}%
}%

\NewDocumentCommand\RemplisParcours{m}{%
  \ifcsname \Alph{PfMMaquette}trajet#1\endcsname
  \expandafter\xdef\csname \Alph{PfMMaquette}trajet#1\endcsname{\csname \Alph{PfMMaquette}trajet#1\endcsname,\thePfMExo}%
%  Yes !
  \else
%  No !
  \expandafter\xdef\csname \Alph{PfMMaquette}trajet#1\endcsname{\thePfMExo}%
  \fi
  \immediate\write\@auxout{\noexpand\expandafter\gdef\noexpand\csname \Alph{PfMMaquette}Parcours#1\endcsname{\csname \Alph{PfMMaquette}trajet#1\endcsname}}%
}%

\def\exerciceParcoursPerso{%
  \stepcounter{PfMExo}%
  \PfMChoixCorrection%
  \@bsphack%
  \immediate\openout \verbatim@out "\jobname-PPEx\thePfMExo.tex" %
  \let\do\@makeother\dospecials%
  \catcode`\^^M\active%
  \def\verbatim@processline{%
    \immediate\write\verbatim@out{\the\verbatim@line}}%
  \verbatim@start}%

\def\endexerciceParcoursPerso{\immediate\closeout\verbatim@out\@esphack}%
  
\newenvironment{exerciceParcours}[1][]{%
  \setcounter{PfMAlea}{0}%
  \setcounter{PfMNbLogo}{1}%
  \useKVdefault[ClesExercices]%
  \setKV[ClesExercices]{#1}%
  \setKV[ClesExercices]{BaremeTotal=false,BaremeDetaille=false}%
  \refstepcounter{PfMSection}%
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}%
  \immediate\write\@auxout{\string\opcopy{\thePfMExo}{\Alph{PfMMaquette}-ParcoursTotalExo}}%
  % Creation des parcours
  \ifboolKV[ClesExercices]{Parcours}{%
      % Remise à zéro de conteneurs des parcours
      \setsepchar{,}%
    \xdef\PfMFooListeParcours{\useKV[ClesExercices]{Trajet}}
    \readlist*\ListeParcours{\PfMFooListeParcours}%
    \foreachitem\compteur\in\ListeParcours{%
      \RemplisParcours{\ListeParcours[\compteurcnt]}%
    }%
  }{}%
  %
  \tcolorbox[%
  tikzparcours,
  label is label,
  label=page-\thePfMMaquette-\thePfMExo
  ]
}{\endtcolorbox%
}

\newenvironment{exerciceFiche}[1][]{%
  \setcounter{PfMAlea}{0}%
  \setcounter{PfMNbLogo}{1}%
  \useKVdefault[ClesExercices]%
  \setKV[ClesExercices]{#1}%
  \colorlet{ColExoFiche}{\useKV[ClesExercices]{Background}}%
  \colorlet{PfMCadreFiche}{\useKV[ClesExercices]{Cadre}}%
  \setKV[ClesExercices]{BaremeTotal=false,BaremeDetaille=false}%
  \refstepcounter{PfMSection}%
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}%
  \tcolorbox[%
  tikzfiche,
  label is label,
  label=page-\thePfMMaquette-\thePfMExo
  ]
}{\endtcolorbox%
}

\newenvironment{exerciceDS}[1][]{%
  \setcounter{PfMAlea}{0}%
  \useKVdefault[ClesExercices]%
  \setKV[ClesExercices]{#1}%
  \refstepcounter{PfMSection}%
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}%
  \immediate\write\@auxout{%
    \string\opcopy{\thePfMExo}{TotalExo}%
  }
  \ifboolKV[ClesExercices]{BaremeTotal}{%
    \immediate\write\@auxout{%
      \string\opcopy{0}{total\the\c@PfMSection-\the\c@PfMExo}%
    }}{}
  \tcolorbox[%
  tikzds,
  label is label,
  label=page-\thePfMMaquette-\thePfMExo
  ]
}{\endtcolorbox%
}
\makeatother
%%% Recap Brevet
%%% Style Brevet
\tcbset{%
  userbrevet/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\tcbset{%
  userbrevetcor/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

% Fiche Exercice Brevet
\newcommand\TikzRecapBrevet{%
\begin{tcolorbox}[userbrevet]%
  \Large\sffamily \useKV[RecapBrevet]{Theme}
  \par{\tiny\useKV[RecapBrevet]{Code}}\hfill{\scriptsize\useKV[RecapBrevet]{Date}}
  \end{tcolorbox}%
}

\newcommand\TikzRecapBrevetCor[1]{%
  \clearpage
  \setKV[RecapBrevet]{#1}%
  \begin{tcolorbox}[userbrevetcor]%
    \Large\sffamily \useKV[RecapBrevet]{Theme} (Correction)
    \par{\tiny\useKV[RecapBrevet]{Code}}\hfill{\scriptsize\useKV[RecapBrevet]{Date}}
  \end{tcolorbox}%
}

\makeatletter
\newenvironment{ExerciceRecapBrevet}[1][]{%
  \setcounter{PfMAlea}{0}%
  \useKVdefault[ClesExercices]%
  \setKV[ClesExercices]{#1}%
  \refstepcounter{PfMSection}%
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}%
  \ifboolKV[ClesExercices]{BaremeTotal}{%
    \immediate\write\@auxout{%
      \string\opcopy{0}{total\the\c@PfMSection-\the\c@PfMExo}%
    }%
  }{}%
  \tcolorbox[%
  tikzrecapbrevet,
  label is label,
  label=page-\thePfMMaquette-\thePfMExo
  ]
}{\endtcolorbox%
}

\tcbset{%
  tikzrecapbrevet/.style={%
    before skip=1em,%
    enhanced,%
    breakable,%
    colback=white,%
    colframe=PfMCadreFiche,%
    overlay unbroken and first={%
      \node[%
      name=A,%
      fill=white,%
      anchor=west,%
      xshift=10pt,%
      ] at (frame.north west){\bfseries\sffamily \useKV[Fiche]{NomExercice} \thePfMExo\ifboolKV[ClesExercices]{AffichageTitre}{ : \useKV[ClesExercices]{Titre}}{}};%
      %%
      \node[%
      fill=white,%
      anchor=east,%
      xshift=-10pt,%
      text=black,%
      font=\bfseries%
      ]
      at (frame.north east)%
      {\footnotesize\sffamily \total{\the\c@PfMSection-\the\c@PfMExo}};%
    }%
  }%
}%
\makeatother
%% PDT
\tcbset{%
  tikzpdt/.style={%
    top=\ifboolKV[ClesExercices]{PasTitre}{0mm}{2mm},bottom=0mm,left=0mm,right=0mm,%
    before skip=1em,
    enhanced,
    breakable,
    colback=white,
    colframe=blue!50,
    remember,
    overlay unbroken and first={%
      \coordinate[xshift=2pt,yshift=-2pt] (NO-\thePfMExo) at (frame.north west);%
      \coordinate[xshift=-2pt,yshift=-2pt] (NE-\thePfMExo) at (frame.north east);%
      \coordinate[xshift=-2pt,yshift=2pt] (SE-\thePfMExo) at (frame.south east);%
      \coordinate[xshift=2pt,yshift=2pt] (SO-\thePfMExo) at (frame.south west);%
      \coordinate (N-\thePfMExo) at (frame.north);
      \coordinate (S-\thePfMExo) at (frame.south);
      \coordinate (E-\thePfMExo) at (frame.east);
      \coordinate (O-\thePfMExo) at (frame.west);
      \ifboolKV[ClesExercices]{PasTitre}{%
        \coordinate[xshift=10pt] (A) at (frame.north west);
      }{%
        \node[%
        name=A,
        fill=white,
        anchor=west,
        xshift=10pt,
        ] at (frame.north west){\bfseries\sffamily \useKV[PdT]{NomExercice} \thePfMExo\ifboolKV[ClesExercices]{AffichageTitre}{ : \useKV[ClesExercices]{Titre}}{}};
      }
      %% 
      \ifboolKV[ClesExercices]{Calculatrice}{}{\node[fill=white,anchor=west,xshift=(\thePfMNbLogo*2.5+(\thePfMNbLogo-1)*20)*1pt] at (A.east) {\cancel{\faCalculator}};\stepcounter{PfMNbLogo}}
      \ifboolKV[ClesExercices]{Oral}{\node[fill=white,anchor=west,xshift=(\thePfMNbLogo*2.5+(\thePfMNbLogo-1)*20)*1pt] at (A.east) {\faBullhorn};\stepcounter{PfMNbLogo}}{}%
      %%
      \ifboolKV[ClesExercices]{AffichageCompetence}{%
        \node[%
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=PfMColCpt,%
        ]
        at (frame.north east)
        {\itshape\scriptsize\sffamily Compétence(s) dominante(s) : \useKV[ClesExercices]{Competence}};
      }{}%
      %%
      \ifboolKV[ClesExercices]{AffichageSource}{%
        \node[%
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=PfMColSrc,
        ]
        at (frame.south east)
        {\scriptsize\sffamily \useKV[ClesExercices]{Source}};
      }{}%
    }%
  }%
}%

\tcbset{%
  userpdt/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\tcbset{%
  userpdtcor/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\NewDocumentCommand\TikzPdT{}{%
  \begin{tcolorbox}[userpdt]%
    \Large\sffamily \useKV[PdT]{Theme}\hfill\useKV[PdT]{Niveau} \useKV[PdT]{Classe}%
    \par{\tiny\useKV[PdT]{Code}}\hfill{\scriptsize\useKV[PdT]{Date}}%
  \end{tcolorbox}%
}

\NewDocumentCommand\TikzPdTCor{m}{%
  \clearpage
  \setKV[PdT]{#1}%
  \begin{tcolorbox}[userpdtcor]%
    \Large\sffamily \useKV[PdT]{Theme} (Correction) \hfill\useKV[PdT]{Niveau} \useKV[PdT]{Classe}
    \par{\tiny\useKV[PdT]{Code}}\hfill{\scriptsize\useKV[PdT]{Date}}
  \end{tcolorbox}%
}

\newenvironment{exercicePdT}[1][]{%
  \setcounter{PfMAlea}{0}%
  \setcounter{PfMNbLogo}{1}
  \useKVdefault[ClesExercices]%
  \setKV[ClesExercices]{#1}%
  \setKV[ClesExercices]{BaremeTotal=false,BaremeDetaille=false}%
  \refstepcounter{PfMSection}%
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}%
  \tcolorbox[%
  tikzpdt
  ]%
}{\endtcolorbox%
}

\newenvironment{SOS}[1][]{%
  \stepcounter{PfMSOS}%
  \tcolorbox[%
  enhanced,
  remember,%
  tikzsos,
  overlay unbroken and first={%
    \coordinate[xshift=2pt,yshift=-2pt] (SOS-NO-\thePfMSOS) at (frame.north west);%
    \coordinate[xshift=-2pt,yshift=-2pt] (SOS-NE-\thePfMSOS) at (frame.north east);%
    \coordinate[xshift=-2pt,yshift=2pt] (SOS-SE-\thePfMSOS) at (frame.south east);%
    \coordinate[xshift=2pt,yshift=2pt] (SOS-SO-\thePfMSOS) at (frame.south west);%
    \coordinate (SOS-N-\thePfMSOS) at (frame.north);
    \coordinate (SOS-S-\thePfMSOS) at (frame.south);
    \coordinate (SOS-E-\thePfMSOS) at (frame.east);
    \coordinate (SOS-O-\thePfMSOS) at (frame.west);
  }%
  ]%
}{\endtcolorbox%
}

\tikzstyle{cheminPdT}=[-stealth]

\NewDocumentCommand\CheminVrai{m}{%
  \setsepchar[*]{,*/}%
  \readlist*\ListeCheminVrai{#1}%
  \setsepchar{,}%
  \begin{tikzpicture}[remember picture,overlay]%
    \foreachitem\compteur\in\ListeCheminVrai{%
      \draw[cheminPdT] (\ListeCheminVrai[\compteurcnt,1]) to node[midway]{\Large \RKbigsmile}(\ListeCheminVrai[\compteurcnt,2]);%
    }%
  \end{tikzpicture}%
}%

\NewDocumentCommand\CheminFaux{m}{%
  \setsepchar[*]{,*/}%
  \readlist*\ListeCheminVrai{#1}%
  \setsepchar{,}%
  \begin{tikzpicture}[remember picture,overlay]%
    \foreachitem\compteur\in\ListeCheminVrai{%
      \draw[cheminPdT] (\ListeCheminVrai[\compteurcnt,1]) to node[midway]{\Large \RKdevilish}(\ListeCheminVrai[\compteurcnt,2]);%
    }%
  \end{tikzpicture}%
}%

\tcbset{%
  tikzsos/.style={%
    left=0pt,right=0pt,top=0pt,bottom=0pt,%
    colback=Crimson!15,%
    colframe=Crimson!50,%
  }%
}%

%% Kholle
\tcbset{%
  tikzkholle/.style={%
    before skip=1em,
    enhanced,
    breakable,
    colback=white,
    colframe=gray!50,
    overlay unbroken and first={%
      \node[%
      name=A,
      fill=white,
      anchor=west,
      xshift=10pt,
      ] at (frame.north west){\ifboolKV[ClesExercices]{AffichageTitre}{%
          {\bfseries\sffamily\useKV[ClesExercices]{Titre}}%
        }{\bfseries\sffamily Exercice \thePfMExo}%
        \xintifboolexpr{\useKV[ClesExercices]{Difficulte}<1}{}{%
          ~(\foreach \s in {1,...,\useKV[ClesExercices]{Difficulte}}{%
            $\star$%
          })%
        }
      }
      ;
      %% 
      \ifboolKV[ClesExercices]{AffichageSource}{%
        \node[
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=gray!50,
        ]
        at (frame.south east)
        {\scriptsize\sffamily \useKV[ClesExercices]{Source}};
      }{}%
    }%
  }%
}%

\newenvironment{CRNote}{%
  \tcolorbox[colback=white,height=2cm,title=NOTE,fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white,enhanced,%
    overlay unbroken and first={%
      \node[%
      anchor=east,
      yshift=-8pt,
      xshift=-10pt,
      text=black,
      ]
      at (frame.north east)
      {\sffamily\bfseries \useKV[Kholle]{NomEleve}};
      \node[%
      anchor=center,
      yshift=-8pt,
      text=black,
      ]
      at (frame.north)
      {\sffamily\bfseries \useKV[Kholle]{Date}};
    }
    ]%
  }{%
  \endtcolorbox%
}

\newlength{\CRRHauteur}
\setlength{\CRRHauteur}{10cm}

\newenvironment{CRRemarque}{%
  \tcolorbox[colback=white,height=\CRRHauteur,title=REMARQUE(S),fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white%
  ]}
{%
  \endtcolorbox%
}%

\NewDocumentEnvironment{exerciceKholle}{o}{%
  \useKVdefault[ClesExercices]%
  \setKV[ClesExercices]{#1}%
  \setKV[ClesExercices]{BaremeTotal=false,BaremeDetaille=false}%
  \PfMChoixCorrection%
  \refstepcounter{PfMExo}%
  \tcolorbox[%
  tikzkholle,
  label is label,
  label=page-\thePfMMaquette-\thePfMExo
  ]
}{\endtcolorbox%
}

\newenvironment{exerciceBase}[1][]{%
  \setcounter{PfMAlea}{0}%
  \setcounter{PfMNbLogo}{1}
  \useKVdefault[ClesExercices]
  \setKV[ClesExercices]{#1}
  \setKV[ClesExercices]{BaremeTotal=false,BaremeDetaille=false}
  \refstepcounter{PfMExo}
  \tcolorbox[%
  tikzbase
  ]
}{\endtcolorbox%
}

\newenvironment{exerciceBaseHTML}[1][]{%
  \setcounter{PfMAlea}{0}%
  \setcounter{PfMNbLogo}{1}
  \useKVdefault[ClesExercices]
  \setKV[ClesExercices]{#1}
  \setKV[ClesExercices]{BaremeTotal=false,BaremeDetaille=false}
  \refstepcounter{PfMExo}
  \tcolorbox[%
  standard jigsaw, opacityback=0,
  lowerbox=ignored,
  tikzbasehtml
  ]
}{\endtcolorbox%
}

%Alias pour tous les exercices.
\NewDocumentEnvironment{exercice}{}{}{}%

%%%%%%%%% STyle Kholle%%%%%%%%%%
% Kholle
\tcbset{%
  userkholle/.style={%
    frame hidden,colback=white,enhanced,%
    borderline north={3pt}{0pt}{gray!85},
    borderline north={2pt}{0.5pt}{gray!15},
    borderline south={3pt}{0pt}{gray!85},
    borderline south={2pt}{0.5pt}{gray!15}
  }%
}%

\newcommand\TikzKholle{%
  \clearpage
  \begin{tcolorbox}[userkholle]%
    \sffamily Interrogation Orale \no\useKV[Kholle]{Numero}\hfill
    \ifboolKV[Kholle]{CR}{}{Sujet \useKV[Kholle]{Sujet}\hfill}\useKV[Kholle]{Niveau}%
    \par\useKV[Kholle]{Lieu}\hfill Le \useKV[Kholle]{Date}%
  \end{tcolorbox}%
}

\newcommand\TikzKholleCor[1]{%
  \clearpage
  \setKV[Kholle]{#1}
  \begin{tcolorbox}[colback=white,height=2cm,title=NOTE,fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white,enhanced,%
    overlay unbroken and first={%
      \node[%
      anchor=east,
      yshift=-8pt,
      xshift=-10pt,
      text=black,
      ]
      at (frame.north east)
      {\sffamily\bfseries Nom : \hbox to5cm{}};
      \node[%
      anchor=center,
      yshift=-8pt,
      text=black,
      ]
      at (frame.north)
      {\sffamily\bfseries \useKV[Kholle]{Date}};
    }
    ]%
  \end{tcolorbox}%
  \begin{tcolorbox}[colback=white,height=2cm,title=EXERCICE(S),fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white%
    ]%
  \end{tcolorbox}%
  \begin{tcolorbox}[colback=white,height=\textheight-5cm,title=REMARQUE(S),fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white%
    ]%
  \end{tcolorbox}%
}

%%%%%%%%%% Style DM%%%%%%%%%%%%%
\tcbset{%
  userdm/.style={%
    frame hidden,colback=white,enhanced,%
    borderline north={3pt}{0pt}{gray!85},%
    borderline north={2pt}{0.5pt}{gray!15},%
    borderline south={3pt}{0pt}{gray!85},%
    borderline south={2pt}{0.5pt}{gray!15}%
  }%
}%

\tcbset{%
  userdmcor/.style={%
    colback=gray!5%
  }%
}%

\NewDocumentCommand\TikzDM{}{%
  \clearpage%
  \begin{tcolorbox}[userdm]%
    \sffamily Devoir en temps libre \useKV[DM]{Numero}\hfill\useKV[DM]{Niveau} \useKV[DM]{Classe}%\no
    \par{\tiny\useKV[DM]{Code}}\hfill{\scriptsize Pour le \useKV[DM]{Date}}%
  \end{tcolorbox}%
}%

\NewDocumentCommand\TikzDMCor{m}{%
  \clearpage
  \setKV[DM]{#1}
  \begin{tcolorbox}[colback=white,height=\PfMHauteurAppreciation,title=APPR\'ECIATION,fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white,enhanced,%
    overlay unbroken and first={%
      \node[%
      inner sep=0pt,
      fill=white,
      anchor=east,
      yshift=-9pt,
      xshift=-10pt,
      text=black,
      ]
      at (frame.north east)
      {\scriptsize\sffamily\bfseries \useKV[DM]{Code}};
    }
    ]%
  \end{tcolorbox}%
  \begin{tcolorbox}[colback=white,height=\PfMHauteurRemarque,title=CONSEIL(S) / REMARQUE(S),fonttitle=\color{black}\bfseries\sffamily,colbacktitle=white%
    ]%
  \end{tcolorbox}%
}
%%% Style Cours
\tcbset{
  usercours/.style={
    enhanced,
  skin=enhancedlast jigsaw,
  attach boxed title to top left={xshift=-4mm,yshift=-0.5mm},
  fonttitle=\bfseries\sffamily,
  colbacktitle=blue!45,
  colframe=red!50!black,
  interior style={
    top color=blue!10,
    bottom color=red!10
  },
  boxed title style={
    empty,
    arc=0pt,
    outer arc=0pt,
    boxrule=0pt
  },
  underlay boxed title={
    \fill[blue!45!white] 
      (title.north west) -- 
      (title.north east) -- 
      +(\tcboxedtitleheight-1mm,-\tcboxedtitleheight+1mm) -- 
      ([xshift=4mm,yshift=0.5mm]frame.north east) -- 
      +(0mm,-1mm) -- 
      (title.south west) -- cycle;
    \fill[blue!45!white!50!black] 
      ([yshift=-0.5mm]frame.north west) -- 
      +(-0.4,0) -- 
      +(0,-0.3) -- cycle;
    \fill[blue!45!white!50!black] 
      ([yshift=-0.5mm]frame.north east) -- 
      +(0,-0.3) -- 
      +(0.4,0) -- cycle; 
  },
  title={\useKV[Cours]{Theme}},
}
}

\NewDocumentCommand\TikzCours{}{%
  \begin{tcolorbox}[usercours]%
%    \Large\sffamily\bfseries  Cours : \useKV[Fiche]{Theme}%
%    \useKV[Fiche]{Niveau}~\useKV[Fiche]{Classe}\hfill{\scriptsize\useKV[Fiche]{Date}}%
  \end{tcolorbox}%
}

%%%%%% Style Fiche
\tcbset{%
  userfiche/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\tcbset{%
  userfichecor/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\NewDocumentCommand\TikzFiche{}{%
  \begin{tcolorbox}[userfiche]%
    \Large\sffamily \useKV[Fiche]{Theme}\hfill\useKV[Fiche]{Niveau} \useKV[Fiche]{Classe}%
    \par{\tiny\useKV[Fiche]{Code}}\hfill{\scriptsize\useKV[Fiche]{Date}}%
  \end{tcolorbox}%
}

\NewDocumentCommand\TikzFicheCor{m}{%
  \clearpage
  \setKV[Fiche]{#1}%
  \begin{tcolorbox}[userfichecor]%
    \Large\sffamily \useKV[Fiche]{Theme} (Correction) \hfill\useKV[Fiche]{Niveau} \useKV[Fiche]{Classe}
    \par{\tiny\useKV[Fiche]{Code}}\hfill{\scriptsize\useKV[Fiche]{Date}}
  \end{tcolorbox}%
}

\NewDocumentCommand\TikzFicheCorRep{m}{%
  \clearpage
  \setKV[Fiche]{#1}%
  \begin{tcolorbox}[userfichecor]%
    \Large\sffamily \useKV[Fiche]{Theme} (Réponses) \hfill\useKV[Fiche]{Niveau} \useKV[Fiche]{Classe}
    \par{\tiny\useKV[Fiche]{Code}}\hfill{\scriptsize\useKV[Fiche]{Date}}
  \end{tcolorbox}%
}

\NewDocumentCommand\TikzFicheCorInd{m}{%
  \clearpage
  \setKV[Fiche]{#1}%
  \begin{tcolorbox}[userfichecor]%
    \Large\sffamily \useKV[Fiche]{Theme} (Indices) \hfill\useKV[Fiche]{Niveau} \useKV[Fiche]{Classe}
    \par{\tiny\useKV[Fiche]{Code}}\hfill{\scriptsize\useKV[Fiche]{Date}}
  \end{tcolorbox}%
}

\makeatletter
\tcbset{%
  tikzfiche/.style={%
    before skip=1em,
    enhanced,
    breakable,
    colback=ColExoFiche,
    colframe=PfMCadreFiche,
    overlay unbroken and first={%
      \node[%
      name=A,
      fill=ColExoFiche,
      anchor=west,
      xshift=\kvtcb@leftlower,
      ] at (frame.north west){\bfseries\sffamily \useKV[Fiche]{NomExercice} \thePfMExo\ifboolKV[ClesExercices]{AffichageTitre}{ : \useKV[ClesExercices]{Titre}}{}};
      %% 
      \ifboolKV[ClesExercices]{Calculatrice}{}{\node[fill=ColExoFiche,anchor=west,xshift=(\thePfMNbLogo*2.5+(\thePfMNbLogo-1)*20)*1pt] at (A.east) {\cancel{\faCalculator}};\stepcounter{PfMNbLogo}}
      \ifboolKV[ClesExercices]{Oral}{\node[fill=ColExoFiche,anchor=west,xshift=(\thePfMNbLogo*2.5+(\thePfMNbLogo-1)*20)*1pt] at (A.east) {\faBullhorn};\stepcounter{PfMNbLogo}}{}
      %%
      \ifboolKV[ClesExercices]{AffichageCompetence}{%
        \node[%
        fill=ColExoFiche,
        anchor=east,
        xshift=-\kvtcb@rightlower,
        text=PfMColCpt,%
        ]
        at (frame.north east)
        {\itshape\scriptsize\sffamily Compétence(s) dominante(s) : \useKV[ClesExercices]{Competence}};
      }{}%
      %%
      \ifboolKV[ClesExercices]{AffichageSource}{%
        \node[%
        fill=ColExoFiche,
        anchor=east,
        xshift=-\kvtcb@rightlower,
        text=PfMColSrc,
        ]
        at (frame.south east)
        {\scriptsize\sffamily \useKV[ClesExercices]{Source}};
      }{}%
    }%
  }%
}%
\makeatother
%%% Parcours %%%
\tcbset{%
  userparcours/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\tcbset{%
  userparcourscor/.style={%
    spread upwards=-1cm,colback=gray!5%
  }%
}%

\NewDocumentCommand\TikzParcours{}{%
  \begin{tcolorbox}[userparcours]%
    \Large\sffamily \useKV[Parcours]{Theme}\hfill\useKV[Parcours]{Niveau} \useKV[Parcours]{Classe}%
    \par{\tiny\useKV[Parcours]{Code}}\hfill{\scriptsize\useKV[Parcours]{Date}}%
  \end{tcolorbox}%
}

\NewDocumentCommand\TikzParcoursCor{m}{%
  \clearpage
  \setKV[Parcours]{#1}%
  \begin{tcolorbox}[userparcourscor]%
    \Large\sffamily \useKV[Parcours]{Theme} (Correction) \hfill\useKV[Parcours]{Niveau} \useKV[Parcours]{Classe}
    \par{\tiny\useKV[Parcours]{Code}}\hfill{\scriptsize\useKV[Parcours]{Date}}
  \end{tcolorbox}%
}

\tcbset{%
  tikzparcours/.style={%
    before skip=1em,
    enhanced,
    breakable,
    colback=white,
    colframe=PfMCadreParcours,
    overlay unbroken and first={%
      \node[%
      name=A,
      fill=white,
      anchor=west,
      xshift=10pt,
      ] at (frame.north west){\bfseries\sffamily \useKV[Parcours]{NomExercice} \thePfMExo\ifboolKV[ClesExercices]{AffichageTitre}{ : \useKV[ClesExercices]{Titre}}{}};
      %% 
      \ifboolKV[ClesExercices]{Calculatrice}{}{\node[fill=white,anchor=west,xshift=(\thePfMNbLogo*2.5+(\thePfMNbLogo-1)*20)*1pt] at (A.east) {\cancel{\faCalculator}};\stepcounter{PfMNbLogo}}
      \ifboolKV[ClesExercices]{Oral}{\node[fill=white,anchor=west,xshift=(\thePfMNbLogo*2.5+(\thePfMNbLogo-1)*20)*1pt] at (A.east) {\faBullhorn};\stepcounter{PfMNbLogo}}{}
      %%
      \ifboolKV[ClesExercices]{AffichageCompetence}{%
        \node[%
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=PfMColCpt,%
        ]
        at (frame.north east)
        {\itshape\scriptsize\sffamily Compétence(s) dominante(s) : \useKV[ClesExercices]{Competence}};
      }{}%
      %%
      \ifboolKV[ClesExercices]{AffichageSource}{%
        \node[%
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=PfMColSrc,
        ]
        at (frame.south east)
        {\scriptsize\sffamily \useKV[ClesExercices]{Source}};
      }{}%
    }%
  }%
}%
%% Fin Parcours %%%
%%% DS %%%
\tcbset{%
  userds/.style={%
    colback=gray!5,%
    enhanced,%
    overlay unbroken and first={%
      \node[yshift=1em] at (frame.south) {\scriptsize\sffamily-- Calculatrice \ifboolKV[DS]{Calculatrice}{autorisée}{interdite} --};
    }%
  }%
}%

\tcbset{%
  userdscor/.style={%
    colback=gray!5%
  }%
}%


\newcommand\TikzDS{%
  \clearpage
  \begin{tcolorbox}[userds]%
    \sffamily Devoir surveillé \no\useKV[DS]{Numero} (Sujet \useKV[DS]{Sujet})\hfill\useKV[DS]{Niveau} \useKV[DS]{Classe}
    \par{\tiny\useKV[DS]{Code}}\hfill{\scriptsize\useKV[DS]{Date}}
  \end{tcolorbox}%
}%

\newcommand\TikzDSCor[1]{%
  \clearpage
  \setKV[DS]{#1}
  \begin{tcolorbox}[userdscor]%
    \sffamily Devoir surveillé \no\useKV[DS]{Numero} (Sujet \useKV[DS]{Sujet})\hfill\useKV[DS]{Niveau} \useKV[DS]{Classe}%
    \par Correction%
  \end{tcolorbox}%
}

\makeatletter
\tcbset{%
  tikzds/.style={%
    enhanced,
    breakable,
    before skip=2mm,after skip=2mm,
    colback=white,
    attach boxed title to top left={xshift=1cm,yshift*=1mm-\tcboxedtitleheight},
    varwidth boxed title*=-3cm,
    boxed title style={frame code={
        \path[fill=tcbcolback!30!black]
        ([yshift=-1mm,xshift=-1mm]frame.north west)
        arc[start angle=0,end angle=180,radius=1mm]
        ([yshift=-1mm,xshift=1mm]frame.north east)
        arc[start angle=180,end angle=0,radius=1mm];
        \path[left color=tcbcolback!60!black,right color=tcbcolback!60!black,
        middle color=tcbcolback!80!black]
        ([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
        [rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
        -- (frame.south east) -- (frame.south west)
        -- ([xshift=-1mm,yshift=-1mm]frame.north west)
        [sharp corners]-- cycle;
      },interior engine=empty,
    },
    fonttitle=\bfseries\sffamily,
    title=Exercice \thePfMExo \ifboolKV[ClesExercices]{AffichageTitre}{ : \useKV[ClesExercices]{Titre}}{},
    overlay unbroken and first={%
      \ifboolKV[ClesExercices]{AffichageSource}{%
        \node[
        fill=white,
        anchor=east,
        yshift=0.5pt,
        xshift=-10pt,
        text=gray!85,
        ]
        at (frame.south east)
        {\scriptsize\sffamily \useKV[ClesExercices]{Source}};
      }{}
      \ifboolKV[ClesExercices]{BaremeTotal}{%
        \node[
        fill=white,
        anchor=east,
        xshift=-10pt,
        text=black,
        font=\bfseries
        ]
        at (frame.north east)
        {\footnotesize\sffamily \total{\the\c@PfMSection-\the\c@PfMExo}};
      }{}
    }%
  }%
}%
\makeatother
% Interrogations écrites
\tcbset{%
  userie/.style={%
  colback=gray!5,
  enhanced,%
  overlay unbroken and first={%
    \node[yshift=1em] at (frame.south) {\scriptsize\sffamily-- Calculatrice \ifboolKV[IE]{Calculatrice}{autorisée}{interdite} --};
    }%
  }%
}%

\tcbset{%
  useriecor/.style={%
  colback=gray!5,
  enhanced,%
  overlay unbroken and first={%
    \node[yshift=1em] at (frame.south) {
      \scriptsize\sffamily-- Calculatrice \ifboolKV[DS]{Calculatrice}{autorisée}{interdite} --
      };%
    }%
  }%
}%
\newcommand\TikzIE{%
  \clearpage
  \begin{minipage}{0.6\linewidth}
    \begin{tcolorbox}[userie]%
      \sffamily \useKV[IE]{Nom} \no\useKV[IE]{Numero} : \useKV[IE]{Theme} \ifboolKV[IE]{Sujets}{\scriptsize(Sujet \useKV[IE]{Sujet})}{}
      \vspace{1em}
      \par{\tiny\useKV[IE]{Code}}\hfill{\scriptsize\useKV[IE]{Date}}
    \end{tcolorbox}%
  \end{minipage}\hfill\begin{minipage}{0.35\linewidth}
    Nom : \PfMIENom\\[0.5em]
    Prénom : \PfMIEPrenom\\[0.5em]
    Classe : \PfMIEClasse%
  \end{minipage}%
  \par\bigskip%
}%

\newcommand\TikzIECor[1]{%
  \clearpage%
  \setKV[IE]{#1}%
  \begin{tcolorbox}[useriecor]%
    \sffamily \useKV[IE]{Nom} \no\useKV[IE]{Numero} : \useKV[IE]{Theme} \ifboolKV[IE]{Sujets}{\scriptsize(Sujet \useKV[IE]{Sujet})}{}\hfill Correction%
    \par{\tiny\useKV[IE]{Code}}\hfill{\scriptsize\useKV[IE]{Date}}%
  \end{tcolorbox}%
}%

\NewDocumentCommand\Recapitulatif{m}{%
  \DTLforeach*{dataperso}{\Prenom=PrenomEleve,\Nom=NomEleve,\RecapListeExo=ListeExo}{%
    \begin{Maquette}[Fiche]{#1,Code=\normalsize\Prenom~\Nom}%
      \readlist*\ListeExercices{\RecapListeExo}%
      \foreach\i in{1,...,\ListeExerciceslen}{%
        \begin{exercice}%
          \input{\jobname-PPEx\ListeExercices[\i].tex}%
        \end{exercice}%
      }
      \ifnum\thePfMCorrigeParcoursPerso=1\relax%
      % \RecapitulatifCorrection{#2}%
      \clearpage
      \TikzFicheCor{#1,Code=\normalsize\Prenom~\Nom}%
      \readlist*\ListeExercices{\RecapListeExo}%
      \foreach\i in{1,...,\ListeExerciceslen}{%
        \textbf{Corrigé de l'exercice \i} : \input{\jobname-PPEx\ListeExercices[\i].sol}\par\goodbreak%
      }%
      \fi
    \end{Maquette}%
  }%
}%

\NewDocumentCommand\RecapitulatifCorrection{m}{%
  \DTLforeach*{dataperso}{\Prenom=PrenomEleve,\Nom=NomEleve,\RecapListeExo=ListeExo}{%
    \clearpage
    \TikzFicheCor{#1,Code=\normalsize\Prenom~\Nom}%
    \readlist*\ListeExercices{\RecapListeExo}%
    \foreach\i in{1,...,\ListeExerciceslen}{%
      \IfFileExists{\jobname-PPEx\ListeExercices[\i].sol}{\textbf{Corrigé de l'exercice \i} : \input{\jobname-PPEx\ListeExercices[\i].sol}\par\goodbreak}{}%
    }%
  }%
}%

%%%%%%%%%%%%%%% Debut Maquette %%%%%%%
% Environnement englobant le tout.
%\makeatletter
\NewDocumentEnvironment{Maquette}{om}{%
  % on remet à zéro les compteurs de solutions : inutile ?
  \setcounter{RetiensSol}{0}%
  \setcounter{RetiensRep}{0}%
  \setcounter{RetiensInd}{0}%
  % on remet à zéro le compteur des exercices
  \setcounter{PfMExo}{0}%
  \setcounter{PfMSOS}{0}%
  % nouvelles valeurs.
  \stepcounter{PfMMaquette}%
  % options de boulot
  \IfHookExistsTF{env/Maquette/after}{\RemoveFromHook{env/Maquette/after}[*]}{}%
  \IfHookExistsTF{env/exercice/after}{\RemoveFromHook{env/exercice/after}[*]}{}%
  %\AddToHook{env/exercice/after}[ChoixExo]{\PfMChoixAffichageCorrection}%
  \useKVdefault[Boulot]%
  \setKV[Boulot]{#1}%
  \IfHookExistsTF{env/exercice/after}{\RemoveFromHook{env/exercice/after}[*]\AddToHook{env/exercice/after}[ChoixExo]{\PfMChoixAffichageCorrection}}{\AddToHook{env/exercice/after}[ChoixExo]{\PfMChoixAffichageCorrection}}%
  \ifboolKV[Boulot]{Cours}{%
      \setKV[Cours]{#2}%
      \TikzCours%
      \let\exercice\exerciceFiche\let\endexercice\endexerciceFiche%
      %\let\Exercice\exerciceFiche\let\endExercice\endexerciceFiche%
      \ifboolKV[Boulot]{CorrigeFin}{%
          \AddToHook{env/Maquette/after}[Correction]{%
            \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
            \TikzFicheCor{#2}%
            \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                \PfMAffichageCorrige%
                \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
          }%
        }{}%
    }{%
  \ifboolKV[Boulot]{RecapBrevet}{%
    \setKV[RecapBrevet]{#2}%
    \TikzRecapBrevet%
    \let\exercice\ExerciceRecapBrevet\let\endexercice\endExerciceRecapBrevet%
    %\let\Exercice\ExerciceRecapBrevet\let\endExercice\endExerciceRecapBrevet%
    \ifboolKV[Boulot]{CorrigeFin}{%
        \AddToHook{env/Maquette/after}[Correction]{%
          \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
        \TikzRecapBrevetCor{#2}%
        \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                       \PfMAffichageCorrige%
                     \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
      }%
    }{}%
  }{%
    \ifboolKV[Boulot]{ParcoursPerso}{%
      \setKV[ParcoursPerso]{#2}%
      \DTLloaddb[noheader,keys={PrenomEleve,NomEleve,ListeExo}]{dataperso}{\useKV[ParcoursPerso]{Fichier}.csv}%
      \let\exercice\exerciceParcoursPerso\let\endexercice\endexerciceParcoursPerso%
      %\let\Exercice\exerciceParcoursPerso\let\endExercice\endexerciceParcoursPerso%
      \ifboolKV[Boulot]{CorrigeFin}{\setcounter{PfMCorrigeParcoursPerso}{1}}{}%
      \setcounter{PfMExo}{0}%
      \AtEndDocument{%
        \Recapitulatif{#2}%
      }%
    }{%
      \ifboolKV[Boulot]{Parcours}{%
        \setKV[Parcours]{#2}\TikzParcours%
        \let\exercice\exerciceParcours\let\endexercice\endexerciceParcours%
        %\let\Exercice\exerciceParcours\let\endExercice\endexerciceParcours%
        \ifboolKV[Boulot]{CorrigeFin}{%
            \AddToHook{env/Maquette/after}[Correction]{%
              \setKV[Boulot]{#1}%
              \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
              \TikzParcoursCor{#2}
              \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                  \PfMAffichageCorrige%
                  \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
            }%
        }{}%
      }{%
        \ifboolKV[Boulot]{PdT}{%
          \setKV[PdT]{#2}\TikzPdT%
          \let\exercice\exercicePdT\let\endexercice\endexercicePdT%
          %\let\Exercice\exercicePdT\let\endExercice\endexercicePdT%
          \ifboolKV[Boulot]{CorrigeFin}{%
              \AddToHook{env/Maquette/after}[Correction]{%
                \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
              \TikzPdTCor{#2}%
              \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                       \PfMAffichageCorrige%
                     \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
            }%
          }{}%
        }{
          \ifboolKV[Boulot]{DM}{%
            \setKV[DM]{#2}\TikzDM%
            \let\exercice\exerciceDM\let\endexercice\endexerciceDM%
            %\let\Exercice\exerciceDM\let\endExercice\endexerciceDM%
            \ifboolKV[Boulot]{CorrigeFin}{%
                \AddToHook{env/Maquette/after}[Correction]{%
                  \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
                \TikzDMCor{#2}%
                \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                    \PfMAffichageCorrige%
                    \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
              }%
            }{}%
          }{%
            \ifboolKV[Boulot]{Fiche}{%
                \setKV[Fiche]{#2}%
                \TikzFiche
                \let\exercice\exerciceFiche\let\endexercice\endexerciceFiche%
                %\let\Exercice\exerciceFiche\let\endExercice\endexerciceFiche%
                \ifboolKV[Boulot]{CorrigeFin}{%
                    \ifboolKV[Boulot]{CorrigeAll}{%
                        \AddToHook{env/Maquette/after}[Correction]{%
                      \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
                      \TikzFicheCor{#2}%
                      \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                          \xintFor* ##1 in{\xintSeq{1}{\thePfMExo}}\do{%
                            \IfFileExists{\jobname-Ex##1.sol}{\textbf{Corrigé de l'exercice ##1}\par\nopagebreak\input{\jobname-Ex##1.sol}\par\goodbreak}{}
                          }% 
                      \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
                      \TikzFicheCorRep{#2}%
                      \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                          \xintFor* ##1 in{\xintSeq{1}{\thePfMExo}}\do{%
                            \IfFileExists{\jobname-Ex##1.rep}{\textbf{Réponse(s) de l'exercice ##1}\par\nopagebreak\input{\jobname-Ex##1.rep}\par\goodbreak}{}
                          }% 
                      \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
                      \TikzFicheCorInd{#2}%
                      \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                          \xintFor* ##1 in{\xintSeq{1}{\thePfMExo}}\do{%
                            \IfFileExists{\jobname-Ex##1.cdp}{\textbf{Indice(s) pour l'exercice ##1}\par\nopagebreak\input{\jobname-Ex##1.cdp}\par\goodbreak}{}
                          }% 
                          \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
                        }%
                   }{%
                   \AddToHook{env/Maquette/after}[Correction]{%
                     \setKV[Boulot]{#1}%
                     \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
                     \TikzFicheCor{#2}%
                     \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                       \PfMAffichageCorrige%
                     \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
                   }%
                 }%
              }{}%
            }{%
              \ifboolKV[Boulot]{DS}{%
                \setKV[DS]{#2}\TikzDS%
                \let\exercice\exerciceDS\let\endexercice\endexerciceDS%
%                \let\Exercice\exerciceDS\let\endExercice\endexerciceDS%
                \ifboolKV[Boulot]{BaremeComplet}{%
                  \begin{center}
                    \TableauRecapitulatif
                  \end{center}
                }{}%
                \ifboolKV[Boulot]{CorrigeFin}{%
                    \AddToHook{env/Maquette/after}[Correction]{%
                      \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
                    \TikzDSCor{#2}%
                    \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                       \PfMAffichageCorrige%
                     \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
                  }%
                }{}%
              }{%
                \ifboolKV[Boulot]{IE}{%
                  \setKV[IE]{#2}\TikzIE%
                  \let\exercice\exerciceDS\let\endexercice\endexerciceDS%On garde exerciceDS
 %                 \let\Exercice\exerciceDS\let\endExercice\endexerciceDS%On garde exerciceDS
                  \ifboolKV[Boulot]{CorrigeFin}{%
                      \AddToHook{env/Maquette/after}[Correction]{%
                        \setKV[Boulot]{#1}%
                      \setlength{\columnseprule}{\useKV[Boulot]{Epaisseur}}%
                      \TikzIECor{#2}%
                      \ifboolKV[Boulot]{CorrectionColonnes}{\begin{multicols}{\useKV[Boulot]{Colonnes}}}{}%
                       \PfMAffichageCorrige%
                     \ifboolKV[Boulot]{CorrectionColonnes}{\end{multicols}}{}%
                    }%
                  }{}%
                }{%
                  \ifboolKV[Boulot]{Kholle}{%
                    \setKV[Kholle]{#2}%
                    \TikzKholle%
                    \let\exercice\exerciceKholle\let\endexercice\endexerciceKholle%
                    \ifboolKV[Kholle]{CR}{}{%
                      \ifboolKV[Boulot]{CorrigeFin}{%
                        \AddToHook{env/Maquette/after}[Correction]{%
                          \TikzKholleCor{#2}%
                          \TikzKholleCor{#2}%
                          \TikzKholleCor{#2}%
                          \PfMAffichageCorrige%
                        }%
                      }{}%
                    }%
                  }{}%
                }%
              }%
            }%
          }%
        }%
      }%
    }%
    }%
  }%
}{}%
%\makeatother

%D'après JCC
% Package pour gérer les nombres pseudo-aléatoires
\makeatletter
\input{random}

% Pour toutes les macros de liste, le premier paramètre est le nom de
% la macro représentant la liste.

% macro pour tester si une liste est vide
\def\empty@list{}

% Macro pour définir une nouvelle liste
\newcommand\definelist[2]{\def#1{#2}}

% Macro pour ajouter un élément en fin de liste
\newcommand\push[2]{%
  \ifx\\#2\\%
    \PackageError{randomlist}
                 {You can't push an empty value.}
                 {Arg 2 of \string\pushlist is empty.}%
  \else
    \ifx#1\empty@list
      \def#1{#2}%
    \else
      \edef#1{#1,#2}%
    \fi
  \fi
}

% Macro pour retirer le premier élément d'une liste
\newcommand\pop[2]{%
  \getwidth{#1}{\RL@temp}%
  \ifnum\RL@temp = 0
    % cas de la liste vide
    \PackageError{randomlist}
                 {You can't pop an empty list.}
                 {There is no value to pull in list #1.}%
  \else\ifnum\RL@temp = 1
    % cas de la liste avec un seul élément
    \edef#2{#1}%
    \def#1{}%
  \else
    % cas de la liste avec au moins deux éléments
    \expandafter\@@poplist#1,\@nil#1#2%
  \fi\fi
}
\def\@@poplist#1,#2,\@nil#3#4{%
  \def#4{#1}%
  \def#3{#2}%
}

% Macro comptant le nombre d'élément d'une liste.
\newcount\RL@cnta
\newcommand\getwidth[2]{%
  \RL@cnta \z@
  \unless\ifx#1\empty@list
    \expandafter\@countlist#1,\@nil,%
  \fi
  \edef#2{\the\RL@cnta}%
}
\def\@countlist#1,{%
  \def\RL@arg{#1}%
  \unless\ifx\RL@arg\@nnil
    \advance\RL@cnta \@ne
    \expandafter\@countlist
  \fi
}

% Macro d'extraction de la nieme valeur
\newcommand\extract[3]{%
  % #1 = liste
  % #2 = indice
  % #3 = macro de retour de la nieme valeur
  % On vérifie si la longueur de la liste est suffisante
  \getwidth{#1}{\RL@temp}%
  \ifnum#2>\RL@temp
    \PackageError{randomlist}
                 {Value number #2 doesn't exist in list #1}
                 {List #1 has only \RL@temp\ values}%
  \else
    \RL@cnta #2
    % On initialise une liste temporaire qui se remplira avec les
    % éléments non extraits
    \def\RL@temp{}%
    % Lancement de la lecture récursive de la liste
    \expandafter\RL@extract#1,\@nil,%
    % On met la valeur extraite dans la macro prévue
    \edef#3{\RL@return}%cp a remplacé edef par xdef
    % La liste est celle sans l'élément extrait
    \let#1\RL@temp
  \fi
}
\def\RL@extract#1,{%
  % Dans cette étape, on ne peut pas atteindre la fin de la liste, il
  % faut juste repérer l'élément à extraire
  \advance\RL@cnta \m@ne
  \ifnum\RL@cnta = 0
    % L'élément est atteint, on le sauvegarde
    \def\RL@return{#1}%
    % et on appelle la macro récursive de fin de lecture.
    \expandafter\RL@@extract
  \else
    % L'élément n'est pas atteint, on augmente la liste temporaire
    % on protège d'éventuelles virgule avec des accolades
    \edef\RL@temp{\RL@temp,{#1}}%
    % et on réalise l'appel récursif
    \expandafter\RL@extract
  \fi
}
\def\RL@@extract#1,{%
  % L'élément a été extrait, on lit le reste de la liste pour mettre à
  % jour la liste temporaire
  \def\RL@arg{#1}%
  \ifx\RL@arg\@nnil
    % Si la liste est vide, on place une virgule
    \ifx\RL@temp\empty
      \def\RL@temp{,}%
    \fi
    % La fin de la liste est atteinte, il faut supprimer la virgule
    % initiale de la liste temporaire
    \expandafter\RL@@@extract\RL@temp\@nil
  \else
    % On n'a pas atteint la fin de liste, on augmente la liste
    % temporaire avec l'élément lu et on réalise l'appel récursif
    % l'élément ajouté est protégé avec des accolades au cas où il
    % contiendrait des virgules.
    \edef\RL@temp{\RL@temp,{#1}}%
    \expandafter\RL@@extract
  \fi
}
\def\RL@@@extract,#1\@nil{\def\RL@temp{#1}}

% Extraction de l'élément <n> de la liste. Le choix de <n> étant
% aléatoire
\newcount\RL@cntb
\newcommand\randomextract[2]{%
  % Le choix aléatoire se fait dans l'intervalle [1,max de la liste]
  % donc on commence par récupérer l'indice maximum de la liste.
  \getwidth{#1}{\RL@temp}%
  % Nombre aléatoire dans le compteur \RL@cntb
  \setrannum{\RL@cntb}{1}{\RL@temp}%
  % Appel de la macro d'extraction
  \extract{#1}{\the\RL@cntb}{#2}%
}
%
% Initialisation du germe en gardant la même valeur toutes les #2
% compilations. Lors de la première compilation, c'est #1 qui sera le
% germe (si valeur vide, on laisse faire random)
\newcommand\initrandomlist[2][]{%
  \ifdefined\randomlistseed
    % Si le germe existe, c'est qu'il a été déclaré dans le
    % fichier .aux avec le compteur de compilation. Le germe et le
    % compteur de compilation existent et sont à jour.
  \else
    % Si le fichier aux n'a pas déclaré de compteur \randomlistseed
    % on considère qu'on est à la première compilation donc qu'on
    % compile avec un nouveau germe et un compteur de compilation à
    % un. Dans ce cas particulier, on laisse random.tex choisir le
    % germe.
    \newcount\randomlistseed
    \ifx\\#1\\
      % si l'argument optionnel est vide, on laisse faire random
      \nextrandom
      \randomlistseed \randomi
    \else
      % sinon, on prend cet argument comme germe initial
      \randomlistseed #1
    \fi
    \newcount\randomlistcompile
    \randomlistcompile \@ne
  \fi
  % Utilisation du germe adéquat pour la compilation en cours.
  \randomi\randomlistseed
  % Préparation de la compilation suivante (dans le fichier .aux)
  % On regarde si le nombre de compilations est égale  à zéro modulo #2 
  \ifnum\numexpr(\randomlistcompile/#2)*#2-\randomlistcompile=0
    % Si le nombre de compilations avec le même germe est atteint
    % on changera le germe à la prochaine compilation.
    \immediate\write\@mainaux{%
      \string\newcount\string\randomlistseed
    }%
    % modification du germe (sans laisser faire random sinon cela ne
    % change pas au cours de chaque minute)
    \ifnum\randomlistseed>2147483646
      \randomlistseed \z@
    \else
      \advance\randomlistseed \@ne
    \fi
    \immediate\write\@mainaux{%
      \string\global\string\randomlistseed=\the\randomlistseed
    }%
  \else
    % Il faut faire encore des compilations avec le même germe, on
    % sauvegarde le germe et le compteur de compilation dans le
    % fichier .aux.
    \immediate\write\@mainaux{%
      \string\newcount\string\randomlistseed
    }%
    \immediate\write\@mainaux{%
      \string\global\string\randomlistseed=\the\randomlistseed
    }%
  \fi
  % Dans tous les cas, on augmente le compteur de compilation
    \immediate\write\@mainaux{%
      \string\newcount\string\randomlistcompile
    }%
    \advance\randomlistcompile \@ne
    \immediate\write\@mainaux{%
      \string\global\string\randomlistcompile=\the\randomlistcompile
    }%
}
\makeatother
\newcommand\initEnsemblede{%
  \definelist{\EnsembleDe}{%
    {2,4,6,8,10,12},%
    {1,3,5,7,9,11},%
    {2,3,5,7,11,13},%
  }
}

\newcommand\GetValeurDe[1]{%
  \extract{\EnsembleDe}{#1}{\DeChoisi}%
  \randomextract{\DeChoisi}{\NbDeChoisi}%
}

\newcommand\initpremiers{%
  \definelist{\listpremiers}{13,17,19,23,29,31,37,41,43,47}%
}

\newcommand\initPremiers{%
  \definelist{\listPremiers}{71,73,79,83,89,97,101,103,107,109,113,127,131,137,139}%
}

\newcommand\getpremier{%
  \randomextract{\listpremiers}{\NbPremier}
}%

\newcommand\getPremier{%
  \randomextract{\listPremiers}{\NBPremier}
}%

\newcommand\getpytha{%
  \randomextract{\tripletspythaentier}{\triplet}%
  \extract{\triplet}{3}{\grand}% le troisième et le plus grand
  \randomextract{\triplet}{\cote}%
  \randomextract{\triplet}{\autre}%
}

\newcommand\initalpha{%
  \definelist{\alphas}{b,c,d,e,f}
}
\newcommand\getalpha{%
  \randomextract{\alphas}{\alphaA}%
}%

\newcommand\initsommet{%
  \definelist{\sommets}{A,B,C,D,E,F,G,H,I,J,K,L,M,% pas d'espace ?
                        N,O,P,Q,R,S,T,U,V,W,X,Y,Z}%
}

\newcommand\initsommetrep{%
  \definelist{\sommets}{A,B,C,D,E,F,G,H,I,J,K,L,M,% pas d'espace ?
                        N,P,Q,R,S,T,U,V,W,X,Y,Z}%
}

\newcommand\gettri{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
}

\newcommand\getquad{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
}

\newcommand\getpenta{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
  \randomextract{\sommets}{\pE}%
}

\newcommand\gethexa{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
  \randomextract{\sommets}{\pE}%
  \randomextract{\sommets}{\pF}%
}

\newcommand\gethepta{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
  \randomextract{\sommets}{\pE}%
  \randomextract{\sommets}{\pF}%
  \randomextract{\sommets}{\pG}%
}

\newcommand\getocto{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
  \randomextract{\sommets}{\pE}%
  \randomextract{\sommets}{\pF}%
  \randomextract{\sommets}{\pG}%
  \randomextract{\sommets}{\pH}%
}

\newcommand\getnonna{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
  \randomextract{\sommets}{\pE}%
  \randomextract{\sommets}{\pF}%
  \randomextract{\sommets}{\pG}%
  \randomextract{\sommets}{\pH}%
  \randomextract{\sommets}{\pI}%
}

\newcommand\gettreize{%
  \randomextract{\sommets}{\pA}%
  \randomextract{\sommets}{\pB}%
  \randomextract{\sommets}{\pC}%
  \randomextract{\sommets}{\pD}%
  \randomextract{\sommets}{\pE}%
  \randomextract{\sommets}{\pF}%
  \randomextract{\sommets}{\pG}%
  \randomextract{\sommets}{\pH}%
  \randomextract{\sommets}{\pI}%
  \randomextract{\sommets}{\pJ}%
  \randomextract{\sommets}{\pK}%
  \randomextract{\sommets}{\pL}%
  \randomextract{\sommets}{\pM}%
}

\definelist{\quadruplets}{%
{2{,}8,9{,}6,6,8},%10
{3{,}2,12{,}6,5,12},%13
{6{,}6,11{,}2,5,12},%13
{7{,}8,10{,}4,5,12},%13
{3{,}2,12{,}6,6{,}6,11{,}2},%13
{3{,}2,12{,}6,7{,}8,10{,}4},%13
{6{,}6,11{,}2,7{,}8,10{,}4},%13
{7{,}8,10{,}4,6{,}6,11{,}2},%13
{4{,}2,14{,}4,9,12},%15
{2{,}6,16{,}8,7{,}2,15{,}4},%17
{8,15,10{,}2,13{,}6},%17
{2{,}6,16{,}8,8,15},%17
{2{,}6,16{,}8,10{,}2,13{,}6},%17
{5{,}6,19{,}2,12,16},%20
{12,16,5{,}6,19{,}2},%20
{7,24,8{,}8,23{,}4},%25
{7,24,15,20},%25
{8{,}8,23{,}4,15,20},%25
{6{,}4,25{,}2,10,24},%26
{6{,}4,25{,}2,13{,}2,22{,}4},%26
{10,24,13{,}2,22{,}4},%26
{10,24,15{,}6,20{,}8},%26
{13{,}2,22{,}4,15{,}6,20{,}8},%26
{9,40,16{,}8,37{,}4},%41
{24{,}6,32{,}8,26{,}6,31{,}2},%41
{9,40,26{,}6,31{,}2},%41
{24{,}6,32{,}8,16{,}8,37{,}4}%41
}%

\definelist{\Quadruplets}{%pour un fonctionnement avec \fpeval
{2.8,9.6,6,8},%10
{3.2,12.6,5,12},%13
{6.6,11.2,5,12},%13
{7.8,10.4,5,12},%13
{3.2,12.6,6.6,11.2},%13
{3.2,12.6,7.8,10.4},%13
{6.6,11.2,7.8,10.4},%13
{7.8,10.4,6.6,11.2},%13
{4.2,14.4,9,12},%15
{2.6,16.8,7.2,15.4},%17
{8,15,10.2,13.6},%17
{2.6,16.8,8,15},%17
{2.6,16.8,10.2,13.6},%17
{5.6,19.2,12,16},%20
{12,16,5.6,19.2},%20
{7,24,8.8,23.4},%25
{7,24,15,20},%25
{8.8,23.4,15,20},%25
{6.4,25.2,10,24},%26
{6.4,25.2,13.2,22.4},%26
{10,24,13.2,22.4},%26
{10,24,15.6,20.8},%26
{13.2,22.4,15.6,20.8},%26
{9,40,16.8,37.4},%41
{24.6,32.8,26.6,31.2},%41
{9,40,26.6,31.2},%41
{24.6,32.8,16.8,37.4}%41
}%

\definelist{\tripletspytha}{{2{,}8,9{,}6,10},{6,8,10},%10
{3{,}2,12{,}6,13},{5,12,13},%13
{6{,}6,11{,}2,13},{7{,}8,10{,}4,13},%13
{4{,}2,14{,}4,15},{9,12,15},%15
{2{,}6,16{,}8,17},{7{,}2,15{,}4,17},%17
{8,15,17},{10{,}2,13{,}6,17},%17
{5{,}6,19{,}2,20},{12,16,20},%20
{7,24,25},{8{,}8,23{,}4,25},%25
{15,20,25},%25
{8{,}8,23{,}4,25},%25
{6{,}4,25{,}2,26},{10,24,26},%26
{13{,}2,22{,}4,26},%26
{15{,}6,20{,}8,26},%26
{9,40,41},{16{,}8,37{,}4,41},%41
{24{,}6,32{,}8,41},{26{,}6,31{,}2,41}%41
}%

\definelist{\tripletspythaentier}{%
{3,4,5},{5,12,13},{6,8,10},{7,24,25},{8,15,17},{9,12,15},{9,40,41},{10,24,26},{11,60,61},{12,16,20},{12,35,37},{13,84,85},{14,48,50},{15,20,25},{15,36,39},{16,30,34},{16,63,65},{18,24,30},{18,80,82},{20,21,29},{20,48,52},{21,28,35},{21,72,75},{24,32,40},{24,45,51},{24,70,74},{25,60,65},{27,36,45},{28,45,53},{28,96,100},{30,40,50},{30,72,78},{32,60,68},{33,44,55},{33,56,65},{35,84,91},{36,48,60},{36,77,85},{39,52,65},{39,80,89},{40,42,58},{40,75,85},{42,56,70},{45,60,75},{48,55,73},{48,64,80},{51,68,85},{54,72,90},{57,76,95},{60,63,87},{60,80,100},{65,72,97}}%%d'après villement.gerard.free.fr


\newcommand\initcarre{%
  \definelist{\carres}{4,9,16,25,36,49,64,81,100}%
}
\newcommand\getcarre{%
  \randomextract{\carres}{\carreA}
}

%2009-2018
  % Source https://ec.europa.eu/eurostat/databrowser/view/ENV_AIR_GGE__custom_743354/default/table?lang=en
\definelist{\GazSerreEu}{%
  {{la },Belgique,131038.46,123642.9},%
  {{la },Bulgarie,58617.88,58595.07},%
  {{le },Danemark,65734.85,51299.75},%
  {{l'},Allemagne,934202.04,888718.56},%
  {{l'},Irlande,63985.03,64241.55},%
  {{la },Grèce,127381.2,96113.19},%
  {{l'},Espagne,385211.04,352208.55},%
  {{la },France,521519.73,462798.66},%
  {{l'},Italie,510950.49,439263.95},%
  {{les },{Pays Bas},212360.67,200458.52},%
  {{la },Pologne,395464.33,415858.2},%
  {{la },Norvège,54280.59,53769.04},%
  {{la },Suisse,57276.22,52099.15}%
}

\newcommand\getGazSerre{%
  \randomextract{\GazSerreEu}{\GazSerrePays}%
  \extract{\GazSerrePays}{1}{\ArticlePaysGS}%
  \extract{\GazSerrePays}{1}{\NomPaysGS}%
  \extract{\GazSerrePays}{1}{\ConsoAPaysGS}%
  \extract{\GazSerrePays}{1}{\ConsoBPaysGS}%
}

  \definelist{\VehiculesINSEE}{%
    {2012,13132,22264,8111,15242},%
    {2013,12853,22712,8028,15020},%
    {2014,12778,23134,8049,14905},%
    {2015,12874,23460,8144,14813},%
    {2016,13144,23634,8308,14813},%
    {2017,13580,23645,8437,14629},%
    {2018,14140,23293,8652,14453},%
    {2019,14756,22636,8920,14392}%
  }%
  \newcommand\getVehiculesINSEE{%
    \randomextract{\VehiculesINSEE}{\NbVehiculesINSEE}%
    \extract{\NbVehiculesINSEE}{1}{\AnneeVehiculesINSEE}%
    \extract{\NbVehiculesINSEE}{1}{\EssenceVehiculesINSEE}%
    \extract{\NbVehiculesINSEE}{1}{\DieselVehiculesINSEE}%
    \extract{\NbVehiculesINSEE}{1}{\KmEssenceVehiculesINSEE}%
    \extract{\NbVehiculesINSEE}{1}{\KmDieselVehiculesINSEE}%
  }