\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{math}

\RequirePackage{amsmath,amsfonts,amsthm,amssymb}
\RequirePackage{xkeyval}

\let\math@theorems r % a = absolus r = relatifs n = none
\let\math@lang f % f = français e = anglais
\newif\ifmath@unicode
\newif\ifmath@displaytrick \math@displaytricktrue

\def\if@eq#1#2#3#4{%
  \def\@tmp{#1}%
  \def\@tmq{#2}%
  \ifx\@tmp\@tmq #3\else #4\fi
}

\DeclareOptionX{theorems}[r]{
  \if@eq{#1}{r}{\let\math@theorems r}\relax
  \if@eq{#1}{a}{\let\math@theorems a}\relax
  \if@eq{#1}{n}{\let\math@theorems n}\relax
  \if@eq{#1}{relative}{\let\math@theorems r}\relax
  \if@eq{#1}{absolute}{\let\math@theorems a}\relax
  \if@eq{#1}{no}{\let\math@theorems n}\relax
}
\DeclareOption{notheorems}{\let\math@theorems n}
\DeclareOption{nounicode}{\math@unicodefalse}
\DeclareOptionX{unicode}[true]{
  \if@eq{#1}{true}{\math@unicodetrue}\relax
  \if@eq{#1}{false}{\math@unicodefalse}\relax
}
\DeclareOption{nodisplaytrick}{\math@displaytrickfalse}
\DeclareOptionX{displaytrick}[true]{%
  \if@eq{#1}{true}{\math@displaytricktrue}\relax
  \if@eq{#1}{false}{\math@displaytrickfalse}\relax
}
\DeclareOptionX{lang}[fr]{
  \if@eq{#1}{fr}{\let\math@lang f}\relax
  \if@eq{#1}{francais}{\let\math@lang f}\relax
  \if@eq{#1}{french}{\let\math@lang f}\relax
  \if@eq{#1}{en}{\let\math@lang e}\relax
  \if@eq{#1}{english}{\let\math@lang e}\relax
}
\ifx \DeclareInputText\@undefined % inputenc test
  \math@unicodetrue %unicode default
\else\ifx \DeclareUnicodeCharacter\@undefined \math@unicodefalse
  \else \math@unicodetrue\fi
\fi

\ProcessOptionsX

\@ifpackageloaded{babel}{}{%
  \ifx \math@lang f \RequirePackage[francais]{babel}
  \else \RequirePackage[english]{babel} \fi
}
\@ifpackageloaded{fontenc}{}{\RequirePackage[T1]{fontenc}}

\ifx \DeclareInputText\@undefined % inputenc test
  \ifmath@unicode \RequirePackage[utf8]{inputenc}
  \else \RequirePackage[latin1]{inputenc} \fi
\fi

\def\newtheorems#1#2{\newtheorem{#1}[thm]{#2}\newtheorem*{#1*}{#2}}

\ifx\math@theorems n\else
  \theoremstyle{plain}
  \ifx\math@lang f
    \ifx\math@theorems r \newtheorem{thm}{Théorème}[subsection]
    \else\newtheorem{thm}{Théorème}\fi
    \newtheorem*{thm*}{Théorème}
    \newtheorems{lem}{Lemme} \newtheorems{cor}{Corollaire}
    \newtheorems{prop}{Proposition}
    \theoremstyle{remark} \newtheorems{rmq}{Remarque}
    \theoremstyle{definition} \newtheorems{df}{Définition}
  \else
    \ifx\math@theorems r \newtheorem{thm}{Theorem}[subsection]
    \else\newtheorem{thm}{Theorem}\fi
    \newtheorem*{thm*}{Theorem}
    \newtheorems{lem}{Lemma} \newtheorems{cor}{Corollary}
    \newtheorems{prop}{Proposition}
    \theoremstyle{remark} \newtheorems{rmq}{Remark}
    \theoremstyle{definition} \newtheorems{df}{Definition}
  \fi
\fi
\theoremstyle{plain}

\def\mytheorem#1{\let\math@mytheorem\undefined
\newtheorem*{math@mytheorem}{#1}\math@mytheorem}
\let\endmytheorem\@endtheorem

% Définition des opérateurs (par une macro sale, sinon c'est moins
% drôle).
\def\math@ops#1{\ifx.#1\let\next\relax\else
\begingroup\escapechar\m@ne\xdef\@tmp##1{##1\noexpand#1{\string #1}}\endgroup
\@tmp\DeclareMathOperator\let\next\math@ops\fi\next}

\math@ops \End \Aut \Hom \Mor \Gal \Ker \Coker \Tr \Card \Vect \Span \Ad \ad
\Id \id \Frac \Spec \Fil.

% Limites projectives et injectives.
\DeclareMathOperator*{\limp}{\varprojlim}
\DeclareMathOperator*{\limi}{\varinjlim}

\DeclareMathOperator{\Ga}{\gras{Ga}}
\DeclareMathOperator{\Gm}{\gras{Gm}}

% \max{x1, ...} met vraiment une accolade

\long\def\@ifgroup#1#2{\def\reserved@a{#1}\def\reserved@b{#2}%
  \futurelet\@let@token\@ifgroup@}
\begingroup \catcode`\{=12 \catcode`\[=1
\global\def\@ifgroup@[%
  \ifx\@let@token\@sptoken \let\reserved@c\@xifgroup@ \else
    \if\@let@token { \let\reserved@c\reserved@a
    \else \let\reserved@c\reserved@b \fi
  \fi \reserved@c}
  \def\:[\@xifgroup@} \expandafter\gdef\: [\futurelet\@let@token\@ifgroup@}
\endgroup

\def\max{\@ifgroup{\max@g}{\max@}}
\def\max@g#1{\max@\acco{#1}}
\def\max@{\qopname\relax o{max}}


% Astuce de displaymath
\ifmath@displaytrick
  \let\oldendmath=\]
  \def\]{\catcode`\;=12\catcode`\:=12\afterassignment\doendmath\global\let\this}
  \def\doendmath{%
    \ifx\this\undefined\oldendmath\let\this\relax\else
    \ifx\this..\oldendmath\let\this\ignorespaces\else
    \ifx\this,,\oldendmath\let\this\ignorespaces\else
    \ifx\this;~;\oldendmath\let\this\ignorespaces\else
    \ifx\this:~:\oldendmath\let\this\ignorespaces\else
    \oldendmath\fi\fi\fi\fi\fi\this}
\fi

% Paires de délimiteurs.
\def\@delim#1#2#3{\def#1##1{{\left#2##1\right#3}}}
\@delim\pa()
\@delim\cro[]
\@delim\acco\{\}
\@delim\chev<>
\@delim\abs\vert\vert
\@delim\norme\Vert\Vert
\@delim\angle\langle\rangle
\@delim\floor\lfloor\rfloor
\@delim\ceil\lceil\rceil
\def\bcro#1{\cro{\cro{#1}}}

% Numérotation (i), (ii), etc.
\renewcommand{\labelenumi}{(\roman{enumi})}

% Échange de \phi et \varphi, et de \epsilon et \varepsilon.
\def\@swap#1#2{\let\@tempa#1\let#1#2\let#2\@tempa}
\@swap\phi\varphi
\@swap\epsilon\varepsilon

% Symboles divers
\let\ksi\xi
\let\leqhoriz\leq \let\leq\leqslant
\let\geqhoriz\geq \let\geq\geqslant
\let\vide\varnothing
\def\prive{{\!\smallsetminus}}

% Symboles.
% \ensuremath permet de parler de \C-algèbres, etc.
\let\gras=\mathbf
\let\grast=\mathbb
\let\go=\mathfrak
\let\ro=\mathcal
\def\oldnos#1{\ensuremath{\mathcode`\,="013B\fam\@ne#1}}
\def\C{{\ensuremath{\grast{C}}}}
\def\R{{\ensuremath{\grast{R}}}}
\def\Q{{\ensuremath{\grast{Q}}}}
\def\Z{{\ensuremath{\grast{Z}}}}
\def\N{{\ensuremath{\grast{N}}}}
\def\F{{\ensuremath{\grast{F}}}}
\def\P{{\ensuremath{\grast{P}}}}
\def\Qp{{\ensuremath{\grast{Q}_p}}}
\def\Zp{{\ensuremath{\grast{Z}_p}}}
\def\Fp{{\ensuremath{\grast{F}_p}}}

% Les flèches \vers et \depuis
% \vers = \rightarrow en mode math, \longrightarrow en mode displaymath
% \vers^f = flèche étiquetée f, avec la bonne longueur
% Pour \depuis, c'est la même chose avec des flèches vers la gauche.

\newcommand{\sidispl@y}[2]{\mathchoice{#1}{#2}{#2}{#2}}%

\def\if@empty#1#2#3{%
  \def\@tempa{#1}%
  \ifx\@tempa\@empty#2\else#3\fi%
}

\def\@fl#1{\def\fl@dir{#1arrow}\let\fl@haut\relax\let\fl@bas\relax
  \futurelet\fl@arg\fl@}
\def\fl@{%
  \ifx ^\fl@arg \let\next\fl@haut@\else
  \ifx _\fl@arg \let\next\fl@bas@ \else
  \ifx [\fl@arg \let\next\fl@cro@ \else % temporaire
  \let\next\fl@trace \fi\fi\fi \next}
\def\fl@haut@^#1{\def\fl@haut{#1}\futurelet\fl@arg\fl@}
\def\fl@bas@_#1{\def\fl@bas{#1}\futurelet\fl@arg\fl@}
\def\fl@cro@[#1]{\def\fl@haut{#1}\futurelet\fl@arg\fl@} % temporaire
\def\fl@trace{\ifx\fl@haut\relax\ifx\fl@bas\relax
  \sidispl@y{\:\csname long\fl@dir\endcsname\:}{\:\csname\fl@dir\endcsname}%
  \else\fl@long\fi\else\fl@long\fi}
\def\fl@long{\edef\fl@sp{\sidispl@y{\;\;}{\:}}\csname x\fl@dir\endcsname
  [\fl@sp\fl@bas\fl@sp]{\fl@sp\fl@haut\fl@sp}}

\def\vers{\@fl{right}}
\def\depuis{\@fl{left}}

% Abréviations
\def\ssi#1{si et seulement s\ifx i#1'\else i \fi#1}

% \newenvironment{preuve}{\emph{Preuve. ---}}{\unskip$\Box$}

\ifmath@unicode
\def\@Mathchars{\@ifnextchar\end\@gobble\@Mathchar@}
\def\@Mathchar@#1#2#3#4#5{%
  \DeclareUnicodeCharacter{#1#2#3#4}{#5}%
  \@Mathchars}

\@Mathchars
00A0~ 00B1\pm 00B7\cdot 00D7\times
0393\Gamma 0394\Delta 0398\Theta 039B\Lambda 039E\Xi 03A0\Pi
03A3\Sigma 03A6\Phi 03A8\Psi 03A9\Omega
03B1\alpha 03B2\beta 03B3\gamma 03B4\delta 03B5\epsilon 03B6\zeta
03B7\eta 03B8\theta 03B9\iota 03BA\kappa 03BB\lambda 03BC\mu 03BD\nu 03BE\xi
03C0\pi 03C1\rho 03C2\varsigma 03C3\sigma 03C4\tau 03C5\upsilon 03C6\phi
03C7\chi 03C8\psi 03C9\omega
03D1\vartheta 03D5\varphi 03D6\varpi  03F0\varkappa
2020\dagger 2021\ddagger
210F\hbar 2111\Im
2113\ell 2127\mho 2132\Finv 2135\aleph 2136\beth 2137\gimel 2138\daleth
2141\Game 214B\invamp
2191\uparrow 2193\downarrow 2194\leftrightarrow 2195\updownarrow
2196\nwarrow 2197\nearrow 2198\searrow 2199\swarrow
219A\nleftarrow 219B\nrightarrow
219E\twoheadleftarrow 21A0\twoheadrightarrow
21A2\leftarrowtail 21A3\rightarrowtail
21A4\mapsfrom% 21A6\mapsto
21A9\hookleftarrow 21AA\hookrightarrow
21AB\looparrowleft 21AC\looparrowright
21B6\curvearrowleft 21B7\curvearrowright
21BA\circlearrowleft 21BB\circlearrowright
21BC\leftharpoonup 21BD\leftharpoondown
21BE\upharpoonright 21BF\upharpoonleft
21C4\rightleftarrows 21C6\leftrightarrows
21C7\leftleftarrows 21C8\upuparrows 21C9\rightrightarrows 21CA\downdownarrows
21CB\leftrightharpoons 21CC\rightleftharpoons
21CD\nLeftarrow 21CE\nLeftrightarrow 21CF\nRightarrow
21D0\Leftarrow 21D1\Uparrow 21D2\Rightarrow 21D3\Downarrow
21D4\Leftrightarrow 21D5\Updownarrow
21DA\Lleftarrow 21DB\Rrightarrow
21DC\leftsquigarrow 21DD\rightsquigarrow
21E0\dashleftarrow 21E1\dashrightarrow
21F0\Mapsto 21FD\leftarrowtriangle 21FE\rightarrowtriangle
21FF\leftrightarrowtriangle
2200\forall 2201\complement 2202\partial 2203\exists 2204\nexists
2205\varnothing 2207\nabla 2208\in 2209\notin 220B\ni 220C\notni 220F\prod
2210\coprod 2211\sum 2213\mp 2216\prive 2217\ast 2218\circ
2219\bullet 221A\sqrt 221D\propto 221E\infty
2220\angle 2221\measuredangle 2222\sphericalangle 2223\mid 2224\nmid
2225\parallel 2226\nparallel
2227\wedge 2228\vee 2229\cap 222A\cup 222B\int 222C\iint 222D\iiint
222E\oint 222F\oiint
2230\oiiint 2232\ointclockwise 2233\oiintctrclockwise
2234\therefore 2235\because 2238\dotminus 2239\eqcolon
223C\sim 223D\backsim 223F\AC
2240\wr 2241\nsim 2243\simeq 2244\nsimeq 2245\cong 2247\ncong 2248\approx
224D\asymp 224E\Bumpeq 224F\bumpeq
2250\doteq 2241\doteqdot 2252\fallingdotseq 2253\risingdotseq
2254\coloneqq 2255\eqqcolon 2256\eqcirc
2260\neq 2261\equiv 2264\leq 2265\geq 2266\leqq 2267\geqq
2268\lneqq 2269\gneqq 226A\ll 226B\gg 226C\between 226D\nasymp
226E\nless 226F\ngtr 2270\nleq 2271\ngeq 2272\lesssim 2273\gtrsim
2274\nlesssim 2275\ngtrsim 2276\lessgtr 2277\gtrless 2278\nlessgtr 2279\ngtrless
227A\prec 227B\succ 227C\preccurlyeq 227D\preccurlyeq 227E\precsim 227F\succsim
2282\subset 2283\supset 2284\nsubset 2285\nsupset
2286\nsubseteq 2287\nsupseteq
2295\oplus 2297\otimes 2299\odot
22C2\bigcap 22C3\bigcup 22C5\cdot 22C6\star
22D4\pitchfork
22EE\vdots 22EF\ldots 22F1\ddots
2A00\bigodot 2A01\bigoplus 2A02\bigotimes
2A1D\join 2A3F\amalg
\end
% 0081\pm 0087\cdot
\DeclareUnicodeCharacter{21A6}{\sidispl@y{\mapsto}{\longmapsto}}
\DeclareUnicodeCharacter{00B0}{\textsuperscript{o}}
\DeclareUnicodeCharacter{2026}{\ifmmode\dots\else\textellipsis\fi}
\DeclareUnicodeCharacter{2102}{\ensuremath{\grast{C}}}
\DeclareUnicodeCharacter{2115}{\ensuremath{\grast{N}}}
\DeclareUnicodeCharacter{211A}{\ensuremath{\grast{Q}}}
\DeclareUnicodeCharacter{211D}{\ensuremath{\grast{R}}}
\DeclareUnicodeCharacter{2124}{\ensuremath{\grast{Z}}}
\DeclareUnicodeCharacter{2146}{\,d} % dx
\DeclareUnicodeCharacter{2190}{\depuis}
\DeclareUnicodeCharacter{2192}{\vers}
\fi
