%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Package file SwissKnife 2 (sk2) - Vincent HUGOT      %
%   2.0                                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{/home/tex-server/sk2}[2009/10/24 Generic tools to make one's life easier, version 2.]


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\OPTcommon{0}
\DeclareOption{common}{
  \def\OPTcommon{1}
  
\let\nc\newcommand
\let\rc\renewcommand

%% MISCELLANEOUS
\newcommand{\todo}[1]{\textsf{\textcolor{red}{\textbf{[TODO]} #1}}}
\setcounter{tocdepth}{5}
\newcommand\compactDisplay{\abovedisplayskip=3.5pt \belowdisplayskip=3.5pt}

%%%% SHORTCUTS
\newcommand{\bk}{\bigskip}
\newcommand{\mk}{\medskip}
\newcommand\sk\smallskip
\newcommand\ie{\textrm{ie.}\;}
\newcommand\eg{\textrm{eg.}\;}
\newcommand\cf{\textrm{cf.}\;}
\newcommand\aka{\textrm{aka.}\;}
\newcommand\tst{\;\x{\textrm{st.}}\ }
\newcommand\tif{\;\xx{\textrm{if}}\ }
\newcommand\resp{\textrm{resp.}\;}
\newcommand\wrt{\textrm{wrt.}\;}
\newcommand\noi\noindent
\nc\os\oldstylenums

\newcommand\Parag[1]{\noi\textbf{{#1}.\;}}
\newcommand\ParagX[1]{\noi\textsc{{#1}.\;}}

%% compat with LLNCS.CLS
\providecommand{\ts}{\textstyle}
\renewcommand{\ts}{\textstyle}
\newcommand{\ds}{\displaystyle}

% \nc\fs{\enspace} % formula space
\nc\tlbl[2]{{#1}_{\text{#2}}} %textlabel

%% force line return
\newcommand\fl{\mbox{}\\}

%%%% COSMETIC IMPROVEMENTS
\renewcommand{\thefootnote}{\,(\alph{footnote})}

\providecommand\labelitemi{{$\diamond$}}
\providecommand\labelitemii{{\boldmath$\triangleright$}}
\providecommand\labelitemiii{{\boldmath$\star$}}
\providecommand\labelitemiv{{\boldmath$\circ$}}

\providecommand\labelenumi{\textbf{\theenumi.}}
\providecommand\labelenumii{\textbf{(\theenumii)}}
\providecommand\labelenumiii{\textbf{\theenumiii.}}
\providecommand\labelenumiv{\textbf{\theenumiv.}}


\renewcommand\labelitemi{{$\diamond$}}
\renewcommand\labelitemii{{\boldmath$\triangleright$}}
\renewcommand\labelitemiii{{\boldmath$\star$}}
\renewcommand\labelitemiv{{\boldmath$\circ$}}

\renewcommand\labelenumi{\textbf{\theenumi.}}
\renewcommand\labelenumii{\textbf{(\theenumii)}}
\renewcommand\labelenumiii{\textbf{\theenumiii.}}
\renewcommand\labelenumiv{\textbf{\theenumiv.}}

%%%% Smart references
\newcommand\pref[1]{\ref{#1}${}_{[\x{p\pageref{#1}}]}$}
\newcommand\peqref[1]{\eqref{#1}${}_{[\x{p\pageref{#1}}]}$}
% \newcommand\point[1]{\textbf{\upshape(\oldstylenums{#1})}} % make a point in text
\newcommand\point[2][]{\textbf{\upshape(\oldstylenums{#2}%
  \ifthenelse{\equal{#1}{}}% no arg?
    {}% nothing
    { : {#1}})% the arg
}} % make a point in text

\newcommand\pointl[1]{{\upshape(\oldstylenums{#1})}}
\newcommand\pointref[1]{{\upshape(\ref{#1})}}

% tags
\nc\tagm[1]{\tag{\ensuremath{{#1}}}}

\newcounter{lorem@}
\newcommand\lorem[1][5]{%
  \forloop{lorem@}{0}{\value{lorem@} < #1}{%
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 
    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 
    exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute 
    irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla 
    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia 
    deserunt mollit anim id est laborum. }}

} % END OPTION


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\OPTmath{0}
\DeclareOption{math}{
  \def\OPTmath{1}
} % END OPTION


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\OPTthm{0}
\DeclareOption{thm}{
  \def\OPTthm{1}
} % END OPTION


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\OPTthmBlack{0}
\DeclareOption{thmBlack}{
  \def\OPTthmBlack{1}
  \ExecuteOptions{thm}
} % END OPTION

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\OPTpstricks{0}
\DeclareOption{pstricks}{
  \def\OPTpstricks{1}
} % END OPTION


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\OPTbeamer{0}
\DeclareOption{beamer}{
  \def\OPTbeamer{1}
} % END OPTION


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MATH ABBREVIATIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\DeclareOption{mathAbbrev} {
\ExecuteOptions{math}

%%%% MISCELLANEOUS

% build xud stacks u and d up and down x.
\newcommand\build[3]{\ensuremath{\mathrel{\mathop{#1}\limits_{#2}^{#3}}}}
\nc\xlbl[2]{\ensuremath{{#1}_{\text{#2}}}} % text label

% restriction of a term or function
\newcommand{\restr}[2]{\left.#1\right|_{#2}}

% ghost : adjusts the delimiters (useful for nested formulaes)
\newcommand{\g}{\vphantom}
\newcommand{\gi}{\g{\bigl( \bigr.}}
\newcommand{\gii}{\g{\Bigl( \bigr.}}
\newcommand{\giii}{\g{\biggl( \biggr.}}
\newcommand{\giv}{\g{\Biggl( \Biggr.}}
%\newcommand{\gv}{\g{\M{1\\1\\1}}}
%\newcommand{\gvi}{\g{\M{1\\1\\1\\1}}}
%\newcommand{\gvii}{\g{\M{1\\1\\1\\1\\1}}}
%\newcommand{\gviii}{\g{\M{1\\1\\1\\1\\1\\1}}}
%\newcommand{\gix}{\g{\M{1\\1\\1\\1\\1\\1\\1}}}
%\newcommand{\gx}{\g{\M{1\\1\\1\\1\\1\\1\\1\\1}}}

%%%% COMMON SETS
\newcommand{\B}{\ensuremath{\mathbb{B}}} 
\newcommand{\R}{\ensuremath{\mathbb{R}}} 
\newcommand{\Q}{\ensuremath{\mathbb{Q}}} 
\newcommand{\C}{\ensuremath{\mathbb{C}}} 
\newcommand{\N}{\ensuremath{\mathbb{N}}}
\newcommand{\Z}{\ensuremath{\mathbb{Z}}} 
\newcommand{\K}{\ensuremath{\mathbb{K}}}
\newcommand{\T}{\ensuremath{\mathbb{T}}}
\let\paragraphSign\P
\renewcommand{\P}{\ensuremath{\mathbb{P}}}
\newcommand{\Pb}[1]{\ensuremath{\mathbb{P}\pb{#1}}} % probability

%%%% TYPOGRAPHY
\newcommand\mc[1]{\ensuremath{\mathcal{#1}}}
\newcommand\mec[1]{\ensuremath{\mathscr{#1}}} % Euler Cal
\newcommand{\mf}{\mathfrak}
\newcommand\mb[1]{\ensuremath{\mathbb{#1}}}
\newcommand\x\text
\newcommand\xx[1]{\quad\text{#1}\quad}
\newcommand\xxx[1]{\qquad\text{#1}\qquad}
\nc\mcom{\;\x,} % text in math punctuation
\nc\msemi{\;\x;}
\nc\mstop{\;\x.}

%%%% SYMBOLS
\newcommand{\eps}{\ensuremath{\varepsilon}}
\newcommand{\st}{\,\mid\,} % such that...
\renewcommand{\t}{\cdot} % times...
\let\oldemptyset\emptyset
\renewcommand{\emptyset}{\varnothing}
\newcommand{\affect}{\ensuremath{\leftarrow}}
\newcommand{\affectEQ}{\vcentcolon=}%\coloneqq
\nc\gizmo{\ensuremath{\divideontimes}}

%Greek letters
\newcommand{\ga}{\ensuremath{\alpha}}
\newcommand{\gb}{\ensuremath{\beta}}
\newcommand{\gc}{\ensuremath{\gamma}}
\newcommand{\gch}{\ensuremath{\chi}}
\newcommand{\gd}{\ensuremath{\delta}}
%\newcommand{\ge}{\ensuremath{\epsilon}}
\newcommand{\gz}{\ensuremath{\zeta}}
\newcommand{\gth}{\ensuremath{\theta}}
\newcommand{\gt}{\ensuremath{\tau}}
\newcommand{\gm}{\ensuremath{\mu}}
\newcommand{\go}{\ensuremath{\omega}}
\newcommand{\gs}{\ensuremath{\sigma}}
\newcommand{\gu}{\ensuremath{\upsilon}}
\newcommand{\gr}{\ensuremath{\rho}}
\newcommand{\gvr}{\ensuremath{\varrho}}
\newcommand{\gl}{\ensuremath{\lambda}}
\newcommand{\gp}{\ensuremath{\varphi}}
\newcommand{\gpi}{\ensuremath{\pi}}
\newcommand{\gpii}{\ensuremath{\varpi}}
\newcommand{\gpv}{\ensuremath{\phi}}
\newcommand{\gps}{\ensuremath{\psi}}
% capitals....
\newcommand{\gD}{\ensuremath{\Delta}}
\newcommand{\gO}{\ensuremath{\Omega}}
\newcommand{\gS}{\ensuremath{\Sigma}}
\newcommand{\gT}{\ensuremath{\Theta}}
\newcommand{\gP}{\ensuremath{\Phi}}
\newcommand{\gPs}{\ensuremath{\Psi}}
\newcommand{\gX}{\ensuremath{\Xi}}
\newcommand{\gU}{\ensuremath{\Upsilon}}

\let\oldell\ell
\renewcommand{\ell}{\ensuremath{\oldell}}


%%%% DELIMITERS
\newcommand{\parentheses}[1]{\ensuremath{\left( #1 \right)}}
\newcommand \p \parentheses

\newcommand{\brackets}[1]{\left[ #1 \right]}
\newcommand \pb \brackets
\newcommand \eqClass \brackets % equivalence class
\newcommand \range [2] {\brackets{#1,#2}}

\newcommand\curlyBraces[1]{\ensuremath{\left\{ #1 \right\}}}
\newcommand\curlyBraceLeft[1]{\ensuremath{\left\{ #1 \right.}}
\newcommand\pc\curlyBraces
\newcommand\set[1]{\curlyBraces{\,#1\,}}
% set such that (the mid bar expands with the contents)
\newcommand\setst[2]{\set{#1\;{\left\lvert\vphantom{#1 #2}\right.}\;#2}}
\newcommand\setstBreak[2]{{\left\{\vphantom{#1 #2}\right.}#1\linebreak[0]\;{\left\lvert\vphantom{#1 #2}\right.}\;#2{\left.\vphantom{#1 #2}\right\}}}
% set without space... for singletons.
\newcommand\sset[1]{\curlyBraces{#1}}

\newcommand\doubleBrackets[1]{\ensuremath{\left\llbracket #1 \right\rrbracket}}
\newcommand\doubleBracketsReversed[1]{\ensuremath{\left\rrbracket #1 \right\llbracket}}
\newcommand\intInterval[2]{\doubleBrackets{#1,#2}}
\newcommand\intRange\intInterval
\newcommand\enum[3]{\ensuremath{#1_{#2},\dotsc,#1_{#3}}}
% enum with subscript
\newcommand\enumSub[4]{\ensuremath{#1_{#2_{#3}},\dotsc,#1_{#2_{#4}}}}
% enum with function
\newcommand\enumFun[4]{\ensuremath{#1(#2_{#3}),\dotsc,#1(#2_{#4})}}

\newcommand\verticalBars[1]{\ensuremath{\left\lvert #1 \right\rvert}}
\newcommand\pvb\verticalBars
\newcommand\card\verticalBars
\newcommand{\tcard}[1]{\mathop{\mathrm{Card}}\p{#1}}

\newcommand\doubleVerticalBars[1]{\ensuremath{\left\lVert #1 \right\rVert}}
\newcommand\pVb\doubleVerticalBars
\newcommand\size\pVb

\newcommand\leftVerticalBar[1]{\ensuremath{\left\lvert #1 \right.}}

\newcommand\parenCeiling[1]{\ensuremath{\left\lceil #1 \right\rceil}}

\newcommand\parenAngle[1]{\ensuremath{\left\langle #1 \right\rangle}}
\newcommand\pa\parenAngle
\newcommand\tuple\parenAngle

%% fractions and variants
\newcommand\f[2]{\ensuremath{\frac{#1}{#2}}}

\newcommand{\quotient}[2]{{\raise0.5ex\hbox{$\scriptstyle {#1}$} \kern-0.1em/\kern-0.15em %
  \lower0.25ex\hbox{$\scriptstyle {#2}$}}}
\newcommand{\Quotient}[2]{\raise0.45ex\hbox{$#1$\,}%
  \!\mathord{\left/{\vphantom {#1 #2}}\right.%
  \kern-\nulldelimiterspace}%
  \!\lower0.67ex\hbox{\,$#2$}}

%%%% OPERATORS

\newcommand\twith{\text{\ with\ }}
\newcommand\tand{\text{\ and\ }}
\newcommand\ttand{\text{\quad and\quad}}
\newcommand\tor{\text{\ or\ }}
\newcommand\ttor{\text{\quad or\quad}}

\newcommand\eqdef{\;\smash{\build{=}{}{\x{\tiny \textsf{def}}}}\;}
\newcommand\equdef{\;\smash{\build{\equiv}{}{\x{def}}}\;}

\newcommand\nmodels{\models\!\!\!\!\!/\;\,}

\newcommand{\<}{\leqslant} \renewcommand{\>}{\geqslant}

% powerset
\nc\powerset[1]{\wp\!\p{#1}}
\nc\powersetX[1]{\wp\!{#1}}

% inclusions and supersets (No/Strict)
\newcommand\sinc\subset
\newcommand\ssup\supset
\newcommand\incl\subseteq
\newcommand\sups\supseteq
\newcommand\ninc\nsubseteq
\newcommand\nsup\nsupseteq
%\newcommand\nsinc
%\newcommand\nssup

% union & intersection & such
\newcommand\union\cup
\newcommand\inter\cap
\nc\dunion\uplus

\newcommand\simplies{\Rightarrow}
\newcommand\rimplies{\mathop{\Longleftarrow}}
\newcommand\srimplies{\mathop{\Leftarrow}}
\nc\siff\Leftrightarrow

\newcommand{\limit}[3][]{\lim_{#2 \underset{#1}{\rightarrow}#3}}

%%%% BIG OPERATORS
% auto index
\newcommand\Sum[3]{\sum_{#1=#2}^{#3}}
\newcommand\Prod[3]{\prod_{#1=#2}^{#3}}
\newcommand{\Und}[3]{\bigwedge_{#1=#2}^{{#3}}}
\newcommand{\Oder}[3]{\bigvee_{#1=#2}^{#3}}

% raw
\newcommand\lAnd\bigwedge
\newcommand\lAndX\Und
\newcommand\lOr\bigvee
\newcommand\lOrX\Oder
\newcommand\Union\bigcup
\newcommand\UnionX[3]{\Union_{#1=#2}^{#3}}
\newcommand\DUnion\biguplus
\newcommand\Inter\bigcap
\newcommand\InterX[3]{\Inter_{#1=#2}^{#3}}

% systems
\newcommand\sys[2]{\curlyBraceLeft{\begin{array}{#1}#2\end{array}}}
\newcommand\sysb[2]{\leftVerticalBar{\begin{array}{#1}#2\end{array}}}
\newcommand\stack[2]{{\begin{array}{#1}#2\end{array}}}

\newcommand\M[1]{\begin{matrix}#1\end{matrix} }
\newcommand\mM[1]{\begin{smallmatrix}#1\end{smallmatrix}}

\newcommand\mapping[4]{\leftVerticalBar{\M{ #1 & \longrightarrow & #2 \\ #3 & \longmapsto & #4}}}
\newcommand\mappingX[6]{\leftVerticalBar{\M{ #1 & \longrightarrow & #2 \\ #3 & \longmapsto & #4 \\ #5 & \longmapsto & #6}}}
\newcommand\mappingXX[8]{\leftVerticalBar{\M{ #1 & \longrightarrow & #2 \\ #3 & \longmapsto & #4 \\ #5 & \longmapsto & #6 \\ #7 & \longmapsto & #8}}}

%multiple equations per line
\nc*\twoEqus[3][equation]{%
\noi\begin{minipage}{0.477\linewidth}%
 \begin{#1}%
 #2%
 \end{#1}%
 \end{minipage}%
 \hfill%
 \begin{minipage}{0.477\linewidth}%
 \begin{#1}%
 #3%
 \end{#1}%
 \end{minipage}\\\mbox{}
}

} % END MATH ABBREVIATIONS


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% COMMON OPTIONS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ExecuteOptions{common,mathAbbrev}

\ProcessOptions\relax

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifnum \OPTcommon=1
  
\fi

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ifnum \OPTmath=1
  \usepackage[]{amsmath,mathtools}
  \allowdisplaybreaks
  \usepackage{amssymb}
  \usepackage[heavycircles]{stmaryrd} %extra logic symbols
  \DeclareMathOperator{\dom}{dom}
  \DeclareMathOperator{\ran}{ran}
  
  \let\oldtilde\tilde
  \let\oldhat\hat
  \renewcommand\tilde[1]{\ensuremath{\widetilde{#1}}}
  \renewcommand\hat[1]{\ensuremath{\widehat{#1}}}
  \let\oldbar\bar
  \renewcommand \bar \overline

\fi

\ifnum \OPTthm=1
  %% conflict with LLNCS.CLS
  \let\proof\@undefined
  \let\endproof\@undefined

  \usepackage{amsthm}
  
  \ifnum \OPTthmBlack=1
    \definecolor{lemcolor}{rgb}{0,0,0}
    \definecolor{thmcolor}{rgb}{0,0,0}
    \definecolor{corcolor}{rgb}{0,0,0}
    \definecolor{cnjcolor}{rgb}{0,0,0}
    \definecolor{defncolor}{rgb}{0,0,0}
  \else
    \definecolor{lemcolor}{rgb}{.4,0,.2}
    \definecolor{thmcolor}{rgb}{.4,0,0}
    \definecolor{corcolor}{rgb}{.4,.2,0}
    \definecolor{cnjcolor}{rgb}{.3,.1,1}
    \definecolor{defncolor}{rgb}{0,.4,0}
  \fi
  \newlength{\@@thmskip}
  \setlength{\@@thmskip}\topsep%{5.0pt plus 1.0pt minus 1.0pt} %topsep
  \newtheoremstyle{slanted}% name
    {\@@thmskip}%      Space above
    {\@@thmskip}%      Space below
    {\slshape}%         Body font
    {}%         Indent amount (empty = no indent, \parindent = para indent)
    {\sffamily\bfseries\slshape}% Thm head font
    {\textbf{.}}%        Punctuation after thm head
    { }%     Space after thm head: " " = normal interword space;
    {\thmname{#1}\thmnumber{ #2}\thmnote{\mdseries{}\upshape{} (\textit{#3\/})}}%         Thm head spec (can be left empty, meaning `normal')
  \newtheoremstyle{straight}% name
    {\@@thmskip}%      Space above
    {\@@thmskip}%      Space below
    { }%         Body font
    {}%         Indent amount (empty = no indent, \parindent = para indent)
    {\sffamily\bfseries}% Thm head font
    {.}%        Punctuation after thm head
    { }%     Space after thm head: " " = normal interword space;
    {}%         Thm head spec (can be left empty, meaning `normal')
  \theoremstyle{slanted}
  \newtheorem{thm}{\color{thmcolor}Theorem}%[section]
  \newtheorem{prp}[thm]{\color{thmcolor}Proposition}%[section]
  \newtheorem{lem}[thm]{\color{lemcolor}Lemma}%[section]
  \newtheorem{cor}[thm]{\color{corcolor}Corollary}%[section]
  \newtheorem{cnj}[thm]{\color{cnjcolor}Conjecture}%[section]
  \newtheorem{fcnj}[thm]{\color{cnjcolor}False Conjecture}%[section]
  %\newtheorem{prpn}{Proposition}[section]
  \theoremstyle{straight}
  \newtheorem{defn}[thm]{\color{defncolor}Definition}%[section]
  \newtheorem{rem}[thm]{\color{lemcolor}Remark}%[section]
\fi

\ifnum \OPTpstricks=1
  \usepackage{pst-3d,pst-coil,pst-eps,pst-fill,pst-grad,pst-node,pst-plot,pst-text,pst-tree}
  \psset{treesep=0.7cm,levelsep=2cm,treemode=B,nodesep=4pt,radius=2pt,treefit=tight,linewidth=.8pt,xunit=1cm,yunit=1cm}
  \newcommand\armedge{\ncdiag[armA=0,angleB=180,armB=1cm]}
\fi