%%%
%%%
%%% Extensions : commun.sty
%%% Version : 1.0
%%% Date : 03/05/15
%%%
%%% Version : 2.0
%%% Date : 11/08/16
%%% Mods : Change some packages call to be compatible with LuaLaTeX
%%%
%%% Auteur : Jeff LANCE
%%%
%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{commun}[2015/05/03 Ensembles d'environnements et de fonctions
                                    utilisables partout, v2.0]


%
%
% PACKAGES
%
%

% development
\RequirePackage{xifthen}
\RequirePackage{ifmtarg}
\RequirePackage{ifpdf}
\RequirePackage{xstring}
\RequirePackage{multido}
\RequirePackage{xparse}
\RequirePackage{etoolbox, xpatch}
\RequirePackage{ifluatex}
\RequirePackage{import}
\RequirePackage{lipsum}
\RequirePackage{subfiles}

% language
\ifluatex
	\RequirePackage{polyglossia}    	% => babel
	\setdefaultlanguage{french}	      % => inputenc
	\RequirePackage{luatextra}	      % charge fontspec => fontenc
	% \defaultfontfeatures{Ligatures=TeX}
\else
	\RequirePackage[french]{babel}	  % => polyglossia
	\RequirePackage[utf8]{inputenc}		% => fontspec
	\RequirePackage[T1]{fontenc}		  % => fontspec
  % \RequirePackage[babel=true,kerning=true]{microtype}
\fi

% fonts
\RequirePackage{inconsolata}
% \RequirePackage{libertine}
\RequirePackage{pifont}
\RequirePackage{fontawesome}
\RequirePackage[right]{eurosym}
\RequirePackage{frcursive}
\RequirePackage{emerald}

% color and graphics
\ifluatex
	\RequirePackage{luacolor}
\fi
% \RequirePackage{tipfr}
%\RequirePackage[table,xcdraw]{xcolor} % there's a bug when used with some mdframed content
\RequirePackage[]{xcolor}
\RequirePackage[framemethod=tikz]{mdframed}
\RequirePackage{graphicx}
\RequirePackage{transparent}
\RequirePackage{fancybox}
\RequirePackage[tikz]{bclogo}

% maths
\RequirePackage{amsmath, amsthm, amsfonts, amssymb}
\RequirePackage{tkz-base}
\RequirePackage{tkz-euclide}
\usetikzlibrary{arrows, calc, patterns, positioning, shapes.geometric, shadows, snakes, tikzmark}
\RequirePackage{calc}
\RequirePackage{fourier}
\RequirePackage{pgfplots}
\pgfplotsset{compat=1.16}
\RequirePackage[load-configurations = abbreviations]{siunitx}
\RequirePackage{asymptote}
\RequirePackage{mathrsfs}
\RequirePackage{xlop}
% \RequirePackage{pst-plot,pst-tree,pstricks,pst-node}
% \RequirePackage{pst-eucl}
% \RequirePackage{pstricks-add}

% tableaux de signe
%\RequirePackage{tabvar}    % replaced by pdftabvar from pdfadd
\RequirePackage{fp}
\ifluatex
  \RequirePackage[luatex]{pict2e}
\fi
\RequirePackage{picture}
\RequirePackage{color}
\input pdftabvar
%

% tables
\RequirePackage{array}
\RequirePackage{tabularray}
\UseTblrLibrary{amsmath}
\UseTblrLibrary{booktabs}
\UseTblrLibrary{counter}
\UseTblrLibrary{diagbox}
\UseTblrLibrary{siunitx}
\UseTblrLibrary{varwidth}
\RequirePackage{tabularx}
\RequirePackage{makecell}
\RequirePackage{multirow}
\RequirePackage{boldline}
\RequirePackage{colortbl}

% layout
\RequirePackage{adjustbox}
\RequirePackage{lscape}
\RequirePackage{multicol}

% text
\RequirePackage{annee-scolaire}
\RequirePackage[dash]{dashundergaps}
\RequirePackage{comment}
\RequirePackage{caption}
\RequirePackage[outputdir=./build]{minted}
\RequirePackage{tasks}
\RequirePackage{dashrule}
\RequirePackage{qrcode}
\RequirePackage{verse}
\RequirePackage{nicefrac}
\RequirePackage{blindtext}
\RequirePackage{wasysym}
\RequirePackage[hybrid]{markdown}
%--



\makeatletter



%
%
% SETTINGS
%
%

% Set the default monospaced font
\ifluatex
  \setmonofont[StylisticSet=3]{Inconsolatazi4}
\fi

% tasks
\settasks{
  label-format = {\bfseries}
}

% siunitx
\sisetup{
  locale=FR,
  output-decimal-marker={,},
  inter-unit-product={\ensuremath{ { } \cdot { } }},
}

% caption
\captionsetup{font=small}
\captionsetup[figure]{labelfont=bf, textfont=it, name=Document}
\captionsetup[table]{labelfont=bf, textfont=it, name=Document}
%--



%
%
% MACROS
%
%

%
% Tikz
%
\tikzstyle{arrow} = [thick,->,>=stealth]

% Tableau
% Alignment commands of the content in the column with a defined width.
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{M}[1]{>{\centering}m{#1}}
%% lh{largeur} : gauche - haut
%\newcolumntype{lh}[1]{>{\raggedright}p{#1}}
%% ch{largeur} : centre - haut
%\newcolumntype{ch}[1]{>{\centering\arraybackslash}p{#1}}
%% rh{largeur} : droite - haut
%\newcolumntype{rh}[1]{>{\raggedleft}p{#1}}
%% lc{largeur} : gauche - centre
%\newcolumntype{lc}[1]{>{\raggedright}m{#1}}
%% cc{largeur} : centre - centre
% \newcolumntype{M}[1]{ >{\centering\arraybackslash} m{#1} }
%% rc{largeur} : droite - centre
%\newcolumntype{rc}[1]{>{\raggedleft}m{#1}}
%% lb{largeur} : gauche - bas
%\newcolumntype{lb}[1]{>{\raggedright}b{#1}}
%% cb{largeur} : centre - bas
%\newcolumntype{cb}[1]{>{\centering}b{#1}}
%% rb{largeur} : droite - bas
%\newcolumntype{rb}[1]{>{\raggedleft}b{#1}}

% Item in a tab.
\newcommand{\tabitem}{~~\llap{\textbullet}~~}

% \firsthline with \cline capabilities
\newcommand{\firstcline}[1]{%
  \multicolumn1c{%
    \global\backup@length\ht\@arstrutbox
    \global\advance\backup@length\dp\@arstrutbox
    \global\advance\backup@length\arrayrulewidth
     \raise\extratabsurround\copy\@arstrutbox
    }\\[-\backup@length]\cline{#1}
}
%--



%
%
% Texte
%
%

% \attrib
% Set author, source and date for a text
\newcommand{\attrib}[1]{
  \nopagebreak{\raggedleft\footnotesize #1\par}
}

% \hcancel[color][size]{text}
% Strike text with a color and a defined thickness.
\newcommand\hcancel[2][black]{
	\setbox0=\hbox{$#2$}%
	\rlap{\raisebox{.45\ht0}{\textcolor{#1}{\rule{\wd0}{1pt}}}}#2
}

% \circled{text}
% Circled text.
\newcommand*\circled[1]{
  \tikz[baseline=(char.base)]{
    \node[shape=circle,draw,inner sep=2pt] (char) {#1};
  }
}

% \kbd{text}
% Boxed text as a keyboard key.
% Why: menukeys and tipfr packages don't really run correctly with beamer.
\newcommand*\kbd[1]{%
    \tikz[baseline=(key.base)]
    \node[%
    draw,
    fill=white,
    drop shadow={shadow xshift=0.25ex,
                 shadow yshift=-0.25ex,
                 fill=black,
                 opacity=0.75},
    rectangle,
    rounded corners=2pt,
    inner sep=1pt,
    line width=0.5pt,
    font=\scriptsize\sffamily
    ](key) {#1\strut}
    ;
}

% \getrandomletter[*]{start}{end}
% Choose a letter between start and end in the alphabetical order.
% Produce a uppercased letter when called with a star.
\NewDocumentCommand \getrandomletter { s G{1} G{26} }{%
  \IfBooleanTF { #1 }{
    \pgfmathrandominteger{\zzz}{#2}{#3}\csname pgffor@Alpha\endcsname{\zzz}
  }{
    \pgfmathrandominteger{\zzz}{#2}{#3}\csname pgffor@alpha\endcsname{\zzz}
  }
}

% \@getletter{start}{end}
% Choose a letter between start an end.The letter maybe in uppercase or in lowercase.
\NewDocumentCommand \@getletter{ G{1} G{26} }{
	\pgfmathsetmacro{\random}{rnd}
	\pgfmathparse{\random >= 0.5 ?
                "\noexpand\getrandomletter{#1}{#2}" :
                "\noexpand\getrandomletter*{#1}{#2}"}
	\pgfmathresult
}

% NEED COMMENT
\newsavebox{\fminipagebox}
\NewDocumentEnvironment{fminipage}{m O{\fboxsep}}
{\par\kern#2\noindent\begin{lrbox}{\fminipagebox}
    \begin{minipage}{#1}\ignorespaces}
    {\end{minipage}\end{lrbox}%
  \makebox[#1]{%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
    \fbox{\usebox{\fminipagebox}}%
    \kern\dimexpr-\fboxsep-\fboxrule\relax
  }\par\kern#2
}

\NewDocumentCommand \colsep{ G{1} }{
    \setlength{\columnseprule}{#1}
}
%--



%
%
% MINTED
%
%

\definecolor{friendlybg}{HTML}{F0F0F0}
\newif\ifplastex
\plastexfalse
\ifplastex
\else
  \newminted[python]{python}{style=bw,
                             fontsize=\small,
                             bgcolor=white,
                             highlightcolor=white,
                             autogobble,
                             frame=none,
                             % breaklines,
                             escapeinside=||,
                             xleftmargin=0em,
                             xrightmargin=0em,
                             numbersep=.2em}

  \newmintinline{python}{style=bw,
                         fontsize=\small,
                         bgcolor=white,
                         highlightcolor=white,
                         autogobble,
                         encoding=utf8}

  \renewcommand{\theFancyVerbLine}{
    \sffamily\black{\bfseries{\scriptsize\arabic{FancyVerbLine}}}}

  \AtBeginEnvironment{minted}{\dontdofcolorbox}
  \def\dontdofcolorbox{\renewcommand\fcolorbox[4][]{##4}}
  \xpatchcmd{\inputminted}{\minted@fvset}{\minted@fvset\dontdofcolorbox}{}{}
\fi
%--



%
%
% BCLOGO
%
%

\renewcommand\bcStyleTitre[1]{\textbf{#1}}
\newcommand\bcexemple{\centering \normalsize \faPaperclip}
\newcommand\bcremarque{\centering \normalsize \faExclamationTriangle}
\newcommand\bclivre{\centering \normalsize \faBook}
\newcommand\bcaide{\centering \normalsize \faInfoCircle}
%--



%
%
% CAPTIONS
%
%

\DeclareCaptionFormat{source}{\textbf{\textit{source#2}} #3}

%---------------------------------------------
% \NewDocumentCommand \hcancel { O{black} O{1pt} } {
% 	\setbox0=\hbox{$#3$}%
% 	\rlap{\raisebox{.45\ht0}{\textcolor{#1}{\rule{\wd0}{#2}}}} {#3}
% }

% \NewDocumentCommand \solution { s O{solution} g } {
% 	\IfBooleanTF {#1}
%   {
% 		\setboolean{display}{true}
%   }
%     {}
%   \begin{#2}
%   \end{#2}
% }

%% \underdash[color]{text}
%\newcommand\underdash[1][black]{% <- règle la couleur du texte
%  \bgroup
%  \ifdim\ULdepth=\maxdimen\settodepth\ULdepth{(j}\advance\ULdepth.4pt\fi
%  \markoverwith{\kern0.2em% <- règle l'espacement avant un pointillé
%    \vtop{
%      \kern0.5ex% <- règle l'altitude des pointillés
%      {\color{black}% <- règle la couleur des pointillés
%       \hrule width.4em% <- règle la largeur des pointillés
%      }
%    }
%    \kern0.2em% <- règle l'espacement après un pointillé
%  }
%  \color{#1}\ULon
%}
%---------------------------------------------
%--



%
%
% FONTS
%
%

\ifluatex
  \newfontfamily\Annie[Ligatures=TeX]{Annie Use Your Telescope}
  \newfontfamily\Architects[Ligatures=TeX]{Architects Daughter}
  \newfontfamily\Bristol[Ligatures=TeX]{Bristol}
  \newfontfamily\ChelseaMarket[Ligatures=TeX]{Chelsea Market}
  \newfontfamily\DejaVuSans[Ligatures=TeX]{DejaVu Sans}
  \newfontfamily\dominofont[Ligatures=TeX]{DejaVu Sans}
  \newfontfamily\Delius[Ligatures=TeX]{Delius}
  \newfontfamily\DeliusSC[Ligatures=TeX]{Delius Swash Caps}
  \newfontfamily\FingerPaint[Ligatures=TeX]{Finger Paint}
  \newfontfamily\Handlee[Ligatures=TeX]{Handlee}
  \newfontfamily\Jd[Ligatures=TeX]{j.d.}
  \newfontfamily\Julee[Ligatures=TeX]{Julee}
  \newfontfamily\Morse[Ligatures=TeX]{Morse}
  \newfontfamily\NixieOne[Ligatures=TeX]{Nixie One}
  \newfontfamily\PenguinAttack[Ligatures=TeX]{PenguinAttack}
  \newfontfamily\PokemonHollow[Ligatures=TeX]{Pokemon Hollow}
  \newfontfamily\PokemonSolid[Ligatures=TeX]{Pokemon Solid}
  \newfontfamily\Purisa[Ligatures=TeX]{Purisa}
  \newfontfamily\ScratchedLetters[Ligatures=TeX]{Scratched Letters}
  \newfontfamily\Lato[Ligatures=TeX]{Lato}
\else
  \newcommand\newFontCommand[2]{%
    \expandafter\newcommand\csname #1\endcsname{\usefont{T1}{#2}{m}{n}}}

  \newFontCommand{Jd}{j.d.}
\fi

%--------------------------------------------------------
% Intent for a more general command.
% \NewDocumentCommand \setFont { O{\familydefault} O{\encodingdefault} }{%
% 	\fontencoding{#2}%
% 	\fontfamily{#1}%
% 	\selectfont
% }
%--------------------------------------------------------

%---
% Print dominos
\newcommand*\d@mino[3]{%
  {\dominofont\char
   \numexpr #1 + #2*7 + #3
   \relax
 }}
\newcommand*\domino{\@ifstar{\d@mino{127075}}{\d@mino{127025}}}
%---

%--



%
%
% COLORS
%
%

% \newColorCommand{color}
% Create a macro which can be accessed with the name "\color".
% This macro make the following text to be colored in color.
\newcommand\newColorCommand[1]{%
  \expandafter\newcommand\csname #1\endcsname{\color{#1}}}

% Create some color commands
\newColorCommand{red}
\newColorCommand{black}
\newColorCommand{blue}
\newColorCommand{gray}
\newColorCommand{green}
\newColorCommand{lapislazuli}
\newColorCommand{salmon}
\newColorCommand{tomato}
\newColorCommand{white}
%--



%
%
% MATHS
%
%

\newcommand\OPoval[3]{%
	\dimen1=#2\opcolumnwidth
	\ovalnode{#1}
		{\kern\dimen1 #3\kern\dimen1}}

%% horizontal or vertical p-uplet
%%
\ExplSyntaxOn

\NewDocumentCommand \uplet { s m } {
  \IfBooleanTF { #1 } {
    \ensuremath{
      \begin{pmatrix}
        \uplet_vertical_list:n {#2}
      \end{pmatrix}
    }
  }{
    \ensuremath{
      \left(
      \uplet_inline_list:n {#2}
      \right)
    }
  }
}
% define a sequence for storing the "massaged" items
\seq_new:N \uplet_seq

\cs_new_protected:Npn \uplet_inline_list:n #1
{
  % clear the sequence
  \seq_clear:N \uplet_seq
  % cycle through the arguments, storing "<arg>" in the sequence
  \clist_map_inline:nn { #1 }
  {
    \seq_put_right:Nn \uplet_seq { ##1 }
  }
  % output the sequence putting ";" in between elements
  \seq_use:Nnnn \uplet_seq { \ ;\  } { \ ;\  } { \ ;\  }
}

\cs_new_protected:Npn \uplet_vertical_list:n #1
{
  % clear the sequence
  \seq_clear:N \uplet_seq
  % cycle through the arguments, storing "<arg>" in the sequence
  \clist_map_inline:nn { #1 }
  {
    \seq_put_right:Nn \uplet_seq { ##1 }
  }
  % output the sequence putting ";" in between elements
  \seq_use:Nnnn \uplet_seq { \\ } { \\ } { \\ }
}

\ExplSyntaxOff

%--



% %
% %
% % FIGURES
% %
% %
%
% \NewDocumentCommand \fig { O{1} m }{
%   \IfFileExists{#2}{
%     \begin{center}
%       \includegraphics[scale=#1]{#2}
%     \end{center}
%   }{
%     \begin{center}
%       \textbf{Fichier {\normalfont\slshape #2} non trouvé !}
%     \end{center}
%   }
% }
%--



\makeatother