\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%% Tables des matières %%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addto\captionsfrench{\renewcommand{\contentsname}{Sommaire}}
\newcommand{\chaptertoccolor}{black}
\renewcommand*\l@chapter[2]{%
  \ifnum\c@tocdepth>\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode \bfseries
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \def\@linkcolor{\chaptertoccolor}%
      \color{\chaptertoccolor}{\mathversion{bold}#1}\nobreak\
       \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfil\nobreak\hb@xt@\@pnumwidth{\hss
        \def\@linkcolor{\chaptertoccolor}%
        \color{\chaptertoccolor}#2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
\def\@dottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont #5}%
     \par}%
  \fi}
\def\contentsline#1#2#3#4{%
  \ifx\\#4\\%
    \csname l@#1\endcsname{#2}{#3}%
  \else
      \csname l@#1\endcsname{\hyper@linkstart{link}{#4}{#2}\hyper@linkend}{%
        \hyper@linkstart{link}{#4}{#3}\hyper@linkend
      }%
  \fi
}

%<-------- Définition des en-têtes & pieds de page ------->
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]%
{\markboth{\MakeUppercase{\thechapter.\ #1}}{}}
\renewcommand{\sectionmark}[1]%
{\markright{\MakeUppercase{\thechapter.\ #1}}}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\newcommand{\helv}{%
\fontfamily{phv}\itshape\fontsize{9}{11}\selectfont}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[LO]{\helv \rightmark}
\fancyhead[RE]{\helv \leftmark}

%<-------------- Définition des titres de chapitres ----------------->
\titleformat{\chapter}
  {\fontfamily{phv}\fontseries{b}\Huge}
  {}{0pt}
  {%
	\hspace{-2.4cm}
  	\begin{minipage}{\linewidth}
  		\begin{flushright}
  			\ifnum\thechapter=0
  				\bfseries\Huge\color{chapitre}#1
  			\else
  				\ifx#1\bibname
  					\bfseries\Huge\color{chapitre}#1
  				\else
  					\ifx#1\indexname
  						\bfseries\Huge\color{chapitre}#1
  					\else
			\begin{tikzpicture}
				\node[white] (num) {\bfseries\Huge\thechapter};
				\fill[rounded corners=2pt,fill=shadow] ($(num.south west)+(-0.1,-0.6)$) -- ($(num.north west)+(-0.6,0.4)$) -- ($(num.north east)+(0.4,0.1)$) -- ($(num.south east)+(-0.1,-0.3)$) -- cycle;
				\fill[rounded corners=2pt,fill=chapitre] ($(num.south west)+(0,-0.5)$) -- ($(num.north west)+(-0.5,0.5)$) -- ($(num.north east)+(0.5,0.2)$) -- ($(num.south east)+(0,-0.2)$) -- cycle;
				\node[white] at (num.center) {\bfseries\Huge\thechapter};
				\node[text=chapitre,left,inner xsep=2em] at (num.west) {\begin{minipage}{\dimexpr\linewidth-3cm}\begin{flushright}\bfseries\Huge\color{chapitre}#1\end{flushright}\end{minipage}};
			\end{tikzpicture}
					\fi
				\fi
			\fi
		\end{flushright}
	\end{minipage}
  }
\titlespacing*{\chapter}{0pt}{0pt}{30pt}

%<-------------- Définition des titres de section ----------------->
\titleformat{\section}
	{\fontfamily{yes}\fontseries{b}\large}
	{}{0pt}
	{%
		\begin{tikzpicture}
			\shade[left color=white,right color=section] (0,-.2) rectangle +(0.99\linewidth,.4);
			\node[fill=white,left,text=section] at (\linewidth,0) {#1};
		\end{tikzpicture}
	}
\titlespacing*{\section}{0em}{0em}{0em}

%<-------------- Environnement "cadre" --------------->
\newsavebox{\contenu@cadre}

\def\notation{notation}
\def\definition{definition}
\def\theoreme{theoreme}
\def\exemple{exemple}
\def\proposition{proposition}
\def\exercice{exercice}

\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}

\newcounter{propositioncnt}
\newcounter{theoremecnt}
\newcounter{exercicecnt}
\newcounter{videcnt}
\newcounter{exemplecnt}
\newcounter{notationcnt}
\newcounter{definitioncnt}

\newcounter{proposition}[chapter]
\newcounter{theoreme}[chapter]
\newcounter{exercice}[chapter]
\newcounter{exemple}[chapter]
\newcounter{notation}[chapter]
\newcounter{definition}[chapter]

\renewcommand{\theproposition}{\thechapter.\arabic{proposition}}
\renewcommand{\thetheoreme}{\thechapter.\arabic{theoreme}}
\renewcommand{\theexercice}{\thechapter.\arabic{exercice}}
\renewcommand{\theexemple}{\thechapter.\arabic{exemple}}
\renewcommand{\thenotation}{\thechapter.\arabic{notation}}
\renewcommand{\thedefinition}{\thechapter.\arabic{definition}}

\newenvironment{enonce}[3]
{%
	\begin{trivlist}\item
   		\noindent
   			\begin{tikzpicture}[color=#2,text=black]
   				\node[inner ysep=2pt,inner xsep=4pt] (textbox) {\iffalse`}\fi
   				\hspace{-4pt}
     			\begin{minipage}{\dimexpr\linewidth}
     				\hspace{-4pt}
     				\begin{tikzpicture}
     					\node[inner sep=4pt] (titlebox) {%
       						\begin{minipage}{\dimexpr\linewidth-4pt}
       						\ifcat%
       						$\detokenize{#1}$\else
       							\fontfamily{yes}\fontsize{14}{16}\bfseries\selectfont#1%
       	\ifnum\thedefinitioncnt=1
			\refstepcounter{definition}
			\thedefinition
		\else
			\ifnum\thenotationcnt=1
				\refstepcounter{notation}
				\thenotation
			\else
				\ifnum\thetheoremecnt=1
					\refstepcounter{theoreme}
					\thetheoreme
				\else
					\ifnum\theexemplecnt=1
						\stepcounter{exemple}
						\theexemple
					\else
						\ifnum\thepropositioncnt=1
							\refstepcounter{proposition}
							\theproposition
						\else
							\ifnum\theexercicecnt=1
								\refstepcounter{exercice}
								\theexercice
							\else
							\fi
						\fi
					\fi
				\fi
			\fi
		\fi
       							\normalfont
       						\fi
       						\ifcat%
       						$\detokenize{#3}$\else
								\ifcat%
       								$\detokenize{#1}$       														\fontfamily{yes}\fontsize{14}{16}\bfseries\selectfont#3
       							\else
       								\space
       								\fontfamily{yes}\fontsize{10}{12}\selectfont#3
       							\fi
       						\fi
       						\end{minipage}\hspace{4pt}};
     					\begin{pgfonlayer}{background layer}
     						\shade[rounded corners,left color=#2!10,right color=white] 
        					(titlebox.south west) [rounded corners] --
        					(titlebox.north west) -- 
        					(titlebox.north east) [rounded corners=0pt] -- 
        					(titlebox.south east) -- cycle;
     					\end{pgfonlayer}
   					\end{tikzpicture}
}
{%
	\end{minipage}\egroup;
   \draw (textbox.south west) [rounded corners] -- 
         (textbox.north west) -- 
         (textbox.north east) [rounded corners=0pt] -- 
         (textbox.south east) -- cycle;
   \end{tikzpicture}
\end{trivlist}
}

\define@cmdkey [PAS] {cadre} {style}{}
\define@cmdkey [PAS] {cadre} {titre}{}
\define@cmdkey [PAS] {cadre} {color}{}
\presetkeys    [PAS] {cadre} {titre=,style=,color=}{}

\newenvironment*{cadre}[1][]
{%
\setkeys[PAS]{cadre}{#1}
\ifx\cmdPAS@cadre@style\definition
	\setcounter{definitioncnt}{1}
	\setcounter{notationcnt}{0}
	\setcounter{theoremecnt}{0}
	\setcounter{exemplecnt}{0}
	\setcounter{propositioncnt}{0}
	\setcounter{exercicecnt}{0}
	\setcounter{videcnt}{0}
	\enonce{Définition}{definition}{\cmdPAS@cadre@titre}
\else
	\ifx\cmdPAS@cadre@style\notation
		\setcounter{definitioncnt}{0}
		\setcounter{notationcnt}{1}
		\setcounter{theoremecnt}{0}
		\setcounter{exemplecnt}{0}
		\setcounter{propositioncnt}{0}
		\setcounter{exercicecnt}{0}
		\setcounter{videcnt}{0}
		\enonce{Activité}{notation}{\cmdPAS@cadre@titre}
	\else
		\ifx\cmdPAS@cadre@style\theoreme
			\setcounter{definitioncnt}{0}
			\setcounter{notationcnt}{0}
			\setcounter{theoremecnt}{1}
			\setcounter{exemplecnt}{0}
			\setcounter{propositioncnt}{0}
			\setcounter{exercicecnt}{0}
			\setcounter{videcnt}{0}
			\enonce{Théorème}{theoreme}{\cmdPAS@cadre@titre}
		\else
			\ifx\cmdPAS@cadre@style\exemple
				\setcounter{definitioncnt}{0}
				\setcounter{notationcnt}{0}
				\setcounter{theoremecnt}{0}
				\setcounter{exemplecnt}{1}
				\setcounter{propositioncnt}{0}
				\setcounter{exercicecnt}{0}
				\setcounter{videcnt}{0}
				\enonce{Exemple}{exemple}{\cmdPAS@cadre@titre}
			\else
				\ifx\cmdPAS@cadre@style\proposition
					\setcounter{definitioncnt}{0}
					\setcounter{notationcnt}{0}
					\setcounter{theoremecnt}{0}
					\setcounter{exemplecnt}{0}
					\setcounter{propositioncnt}{1}
					\setcounter{exercicecnt}{0}
					\setcounter{videcnt}{0}
					\enonce{Proposition}{proposition}{\cmdPAS@cadre@titre}
				\else
					\ifx\cmdPAS@cadre@style\exercice
						\setcounter{definitioncnt}{0}
						\setcounter{notationcnt}{0}
						\setcounter{theoremecnt}{0}
						\setcounter{exemplecnt}{0}
						\setcounter{propositioncnt}{0}
						\setcounter{exercicecnt}{1}
						\setcounter{videcnt}{0}
						\enonce{Exercice}{exercice}{\cmdPAS@cadre@titre}
					\else
						\enonce{}{\cmdPAS@cadre@color}{\cmdPAS@cadre@titre}
						\setcounter{definitioncnt}{0}
						\setcounter{notationcnt}{0}
						\setcounter{theoremecnt}{0}
						\setcounter{exemplecnt}{0}
						\setcounter{propositioncnt}{0}
						\setcounter{exercicecnt}{0}
						\setcounter{videcnt}{1}
					\fi
				\fi
			\fi
		\fi
	\fi
\fi
}
{
	\endenonce
	\setcounter{definitioncnt}{0}
	\setcounter{notationcnt}{0}
	\setcounter{theoremecnt}{0}
	\setcounter{exemplecnt}{0}
	\setcounter{propositioncnt}{0}
	\setcounter{exercicecnt}{0}
	\setcounter{videcnt}{0}
	
}

%<--------------- Commande "exergue" ---------->

\newcommand{\bold}[1]
{%
	{%
		\ifnum\thedefinitioncnt=1
			\color{definition}
		\else
			\ifnum\thenotationcnt=1
				\color{notation}
			\else
				\ifnum\thetheoremecnt=1
					\color{theoreme}
				\else
					\ifnum\theexemplecnt=1
						\color{exemple}
					\else
						\ifnum\thepropositioncnt=1
							\color{proposition}
						\else
							\ifnum\theexercicecnt=1
								\color{exercice}
							\else
								\ifnum\thevidecnt=1
									\color{vide}
								\else
									\color{black}
								\fi
							\fi
						\fi
					\fi
				\fi
			\fi
		\fi
		\index{#1}
		\fontfamily{yes}\bfseries\selectfont#1\normalfont%
	}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% Environnement enumerate %%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setlist[enumerate,1]{label=\fcolorbox{exercice}{exercice!20}{\bfseries\arabic*}}
\setlist[enumerate,2]{label=\textcolor{definition}{(\alph*)}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{defin}[1]{%
\begin{cadre}[style=definition,titre={#1}]}
{\end{cadre}}

\newenvironment{act}[1]{\begin{cadre}[style=notation,titre={#1}]}{\end{cadre}}

\newenvironment{actsuite}{\begin{cadre}[style=vide,titre={(suite)},color=notation]}{\end{cadre}}

\renewenvironment{exemple}[1]{\begin{cadre}[style=exemple,titre={#1}]}{\end{cadre}}

\newenvironment{methode}[1]{\begin{cadre}[style=methode,titre={#1}]}{\end{cadre}}

\newenvironment{prop}[1]{\begin{cadre}[style=proposition,titre={#1}]}{\end{cadre}}

%---------
% Logos

\newcommand*\logoAttention
{%
	\begin{tikzpicture}
		\fill[white,rounded corners=1pt] (0,0) -- (-0.5ex,0) -- (-1ex,3ex) -- (1ex,3ex) -- (0.5ex,0) -- cycle;
		\fill[white] (0,-1ex) circle (0.5ex);
	\end{tikzpicture}
}

\newcommand*\logoRemarque
{%
	\begin{tikzpicture}[line width=4pt]
		\draw[white,->] (0,0)--(3ex,0);
	\end{tikzpicture}
}

%---------
% Définition des environnements : Remarque, Attention, ...

\newcommand*\DefineBoxLogo[4] % #1 : Nom (rem,rems,...) - #2 : Titre (Remarque, ...) - #3 : Couleur du trait - #4 : commande du logo
{%
	\newenvironment*{#1}
	{
		\begin{lrbox}{\PAS@texte}
			\begin{minipage}[t]{0.85\linewidth}
	}
	{
			\end{minipage}
		\end{lrbox}
		\AfficheRem{#2}{\usebox{\PAS@texte}}{#3}{#4}
	}
}

\newcommand*\AfficheRem[4] % #1 : Titre - #2 : Texte - #3 : Couleur du trait - #4 : commande du logo
{%
	\begin{tikzpicture}
		% On place le texte
		\node[inner xsep=5mm] (texte) at (0,0) {#2};
		% On dessine le trait du texte
		\fill[#3] ($(texte.south west)+(-1pt,0)$) rectangle ($(texte.north west)+(1pt,0)$);
		% On place le logo
		\node[fill=#3,text=white,rounded corners=3pt,inner sep=1em,above] (logo) at (texte.north west) {\large\bfseries\sffamily#4};
		% On place le titre
		\node[text=#3,inner sep=5pt,right] at (logo.east) {\bfseries\large#1};
		% On remet le texte
		\node[below right,inner xsep=5mm] at (texte.north west) {#2};
	\end{tikzpicture}
}

\DefineBoxLogo{rem}{Remarque}{shadow}{\logoRemarque}
\DefineBoxLogo{rems}{Remarques}{shadow}{\logoRemarque}
\DefineBoxLogo{att}{Attention}{shadow}{\logoAttention}

%---------
% Définition des exercices

%---------
% Environnement "exo"

\newcounter{PASexoscount} % compteur désignant le n° de l'exercice
\newcounter{tmpexo}
\newsavebox{\PASdiff}
\def\symb@diff{\bigstar}
\def\shape@symb{rectangle}
\def\scale@symb{1}
\newcommand*\symbDiff[1]{\def\symb@diff{#1}}
\newcommand*\shapeDiff[1]{\def\shape@symb{#1}}
\newcommand*\scaleDiff[1]{\def\scale@symb{#1}}

\newenvironment*{exo}[2][]
{%
	\setcounter{tmpexo}{0}
	\begin{lrbox}{\PASdiff}
		\multido{\i=1+1}{#1}
		{%
			\begin{tikzpicture}
				\ifnum\i=1
					\node[fill=exercice,text=black,shape=\shape@symb,scale=\scale@symb] (n1) {$\symb@diff$};
				\else
					\setcounter{tmpexo}{\i-1}
					\node[right,fill=exercice,text=black,shape=\shape@symb,scale=\scale@symb] (n\i) at ($(n\thetmpexo.east)+(2pt,0)$) {$\symb@diff$};
					\stepcounter{tmpexo}
				\fi
			\end{tikzpicture}
		}
	\end{lrbox}
	\stepcounter{PASexoscount}
	\def\titre{#2 \usebox{\PASdiff} - Corr. p. \pageref{corrige-\thechapter-\thePASexoscount}}
	\begin{cadre}[style=exercice,titre={\titre}]
}
{%
    \end{cadre}
}

%---------
% Environnement "correction"

%% Portion de code pour écrire dans un fichier %% Merci pg ...

\newwrite{\output@stream@corrige}
\newcommand{\OuvrirFlux}[1]{% ouvre le flux de fichier
  \immediate\openout\output@stream@corrige #1}
\newcommand{\FermerFlux}{% ferme le flux de fichier
  \immediate\closeout\output@stream@corrige}
\newcommand{\EcritureFlux}[1]{% écrit un court texte dans le fichier
  \immediate\write\output@stream@corrige{\unexpanded{#1}}}
\newcommand{\CommencerExportFlux}{% commence l'écriture prolongée
   \bgroup\@bsphack
   \let\do\@makeother\dospecials
   \catcode`\^^M\active
   \def\verbatim@processline{%
     \immediate\write\output@stream@corrige{\the\verbatim@line}}%
   \verbatim@start}
\ifdefined\ArreterExportFlux % arrête l'écriture prolongée
  \newcommand{\ArreterExportFlux}{\@esphack\egroup}
\else
  \def\ArreterExportFlux{\@esphack\egroup}
\fi

\newenvironment{correction}
  {\OuvrirFlux{Corrections/\thechapter-\thePASexoscount.tex}%
   \EcritureFlux{\begin{corrige}[\thechapter]{\i}}%
   \CommencerExportFlux}
  {\ArreterExportFlux%
   \EcritureFlux{\end{corrige}}%
   \FermerFlux}

\newenvironment{corrige}[2][]
  {\par\vspace*{1em}%
   \label{corrige-#1-#2}%\arrayrulecolor{shadow}
   \noindent%
	  \begin{tabularx}{\linewidth}{ScX|}
      \cellcolor{shadow}\textcolor{white}{\helv #2} &
      \helv\bfseries\textcolor{bleu}{Correction} \\
      \hline
   \end{tabularx}\par
   \vspace*{1em}}
  {%
  \par\vspace*{2em}%\arrayrulecolor{shadow}
  }

%---------
% Affiche les corrections

\newcommand*\AfficheCorrections
{%
	\multido{\i=1+1}\thePASexoscount{%
		\input{Corrections/\thechapter-\i}
	}
	\setcounter{PASexoscount}{0}
}
  
\makeatother