%%% This package is experimental and not (yet) finished;
%%% use at your own risks
\def\filedate{2010/01/07}
\def\fileversion{alpha version 2}
\def\basename{customtheorem}
\ProvidesPackage{\basename}[\filedate \space\fileversion]

\RequirePackage{environ}
\RequirePackage{calc}
\RequirePackage{xcolor}

% Commands for error handling

\newcommand{\TestIfCmdExistsFromPackageWhenUsing}[3]{
  \ifx#1\undefined
    \PackageError{customtheorem}{#2 package needed for #3 theorems}{}
  \fi
}
\newcommand{\TestIfTikZShapeLibraryLoadedFor}[1]{
  \ifx\pgf@sh@s@ellipse\undefined
    \PackageError{customtheorem}{You need to say \string\usetikzlibrary{shapes} for #1 theorems}{}\
  \fi
}

\newcommand{\TestIfCounterExists}[1]{
\ifcsname c@#1\endcsname
  \PackageError{customtheorem}{counter #1 already exists ; change the theorem's name or use \string\newtheorem{...}[#1]{...}}{}
\fi
}

% if a commande \renewtheorem is added, redefining \def should not be possible
\newcommand{\TestIfNameAlreadyExists}[1]{
\ifcsname #1\endcsname
  \PackageError{customtheorem}{name #1 is forbidden ; choose another}{}
  \@@end % stops compilation (seems a bit harsh, but redefining \def can result in much more unclear error messsages)
\fi
}

% useful lengths

\newlength{\saveparindent}
\AtBeginDocument{% captures the value of indentation at begin{document}
\setlength{\saveparindent}{\parindent}
}

\newlength{\temptheoremlength}
\newlength{\temptheoremlengthbis}
\newsavebox{\temptheoremtextbox}

\newif\ifthetheoremnoteempty

% TO DO:
% - robustly incorporate color into the commands
% - put an explicit error message if counter already defined or if environment already defined
% - don't forget the ignorespaces commands ; this might need to add a \nobreak after \ignorespaces so that theorems don't break where they shouldn't

%
% FOR PERFECT FORMATTING OF COMMANDS
% (taken from \@trivlist definition of latex.ltx)
%

\def\clearbeforetheorem{
  \if@noskipsec \leavevmode \fi
  \@topsepadd \topsep
  \ifvmode
    \advance\@topsepadd \partopsep
  \else
    \unskip \par
  \fi
  \if@inlabel
    \@noparitemtrue
    \@noparlisttrue
  \else
    \if@newlist \@noitemerr \fi
    \@noparlistfalse
    \@topsep \@topsepadd
  \fi
}

%
% DEFINING A NON BREAKING ITEM FOR THE BREAK STYLE
%

\def\nobreakitem{%
  \@inmatherr\item
  \@ifnextchar [\@nobreakitem{\@noitemargtrue \@nobreakitem[\@itemlabel]}}
\def\@nobreakitem[#1]{%
  \if@noparitem
    \@donoparitem
  \else
    \if@inlabel
      \indent \par\nobreak
    \fi
    \ifhmode
      \unskip\unskip \par\nobreak
    \fi
    \if@newlist
      \if@nobreak
        \@nbitem
      \else
        \addpenalty\@beginparpenalty
        \addvspace\@topsep
        \addvspace{-\parskip}%
      \fi
    \else
      \addpenalty\@M % <-- difference with \@item is here
      \addvspace\itemsep
    \fi
    \global\@inlabeltrue
  \fi
  \everypar{%
    \@minipagefalse
    \global\@newlistfalse
    \if@inlabel
      \global\@inlabelfalse
      {\setbox\z@\lastbox
       \ifvoid\z@
         \kern-\itemindent
       \fi}%
      \box\@labels
      \penalty\z@
    \fi
    \if@nobreak
      \@nobreakfalse
      \clubpenalty \@M
    \else
      \clubpenalty \@clubpenalty
      \everypar{}%
    \fi}%
  \if@noitemarg
    \@noitemargfalse
    \if@nmbrlist
      \refstepcounter\@listctr
    \fi
  \fi
  \sbox\@tempboxa{\makelabel{#1}}%
  \global\setbox\@labels\hbox{%
    \unhbox\@labels
    \hskip \itemindent
    \hskip -\labelwidth
    \hskip -\labelsep
    \ifdim \wd\@tempboxa >\labelwidth
      \box\@tempboxa
    \else
      \hbox to\labelwidth {\unhbox\@tempboxa}%
    \fi
    \hskip \labelsep}%
  \ignorespaces}

%
% MACROS FOR CHECKING IF (NOT) EMPTY (from amsthm.sty)
%

\long\def\@ifempty#1{\@xifempty#1@@..\@nil}
\long\def\@xifempty#1#2@#3#4#5\@nil{%
  \ifx#3#4\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
\long\def\@ifnotempty#1{\@ifempty{#1}{}}

%
% FOR ALLOWING GREEK IN COUNTERS
% (taken from ntheorem.sty)

\def\theorem@checkbold{\if b\expandafter\@car\f@series\@nil\boldmath\fi}
\def\@greek#1{\theorem@checkbold%
 \ifcase#1\or$\alpha$\or$\beta$\or$\gamma$\or$\delta$\or$\varepsilon$%
  \or$\zeta$\or$\eta$\or$\vartheta$\or$\iota$\or$\kappa$\or$\lambda$\or$%
  \mu$\or$\nu$\or$\xi$\or$ o$\or$\varpi$\or$\varrho$\or$\varsigma$\or$\tau$%
  \or$\upsilon$\or$\varphi$\or$\chi$\or$\psi$\or$\omega$\else\@ctrerr\fi}
\def\@Greek#1{\theorem@checkbold%
 \ifcase#1\or A\or B\or$\Gamma$\or$\Delta$\or E%
  \or Z\or H\or$\Theta$\or I\or K\or$\Lambda$\or M%
  \or N\or$\Xi$\or O\or$\Pi$\or P\or$\Sigma$\or T%
  \or$\Upsilon$\or$\Phi$\or X\or$\Psi$\or$\Omega$\else\@ctrerr\fi}
\def\greek#1{\@greek{\csname c@#1\endcsname}}
\def\Greek#1{\@Greek{\csname c@#1\endcsname}}

%
% SETTING THEOREM STYLE
%

\def\standardtheoremverticalrule{%
    \vrule width \thetheorembodylinewidth}
\def\standardtheoremhorizontalrule{%
    \rule[0pt]{\linewidth}{\thetheorembodylinewidth}}

% is it possible to use \let with a second expression with csname?
% it would probably shorten some definitions below
\def\theoremformat#1{%
   \def\theorem@begin{\expandafter\csname #1theorembegin\endcsname}
   \def\theorem@end{\expandafter\csname #1theoremend\endcsname}
}
\def\theoremnumbering#1{\def\theorem@numbering{\csname #1\endcsname}}
\def\theorempreskip#1{\def\theorem@preskip{#1}}
\def\theorempostskip#1{\def\theorem@postskip{#1}}
\def\theoremprework#1{\def\theorem@prework{#1}}
\def\theorempostwork#1{\def\theorem@postwork{#1}}
\def\theoremskip#1{\theorempreskip{#1}\theorempostskip{#1}}
\def\theoremheadfont#1{\def\theorem@headfont{#1}}
\def\theoremheadpunct#1{\def\theorem@headpunct{#1}}
\def\theoremheadspace#1{\def\theorem@headspace{#1}}
\def\theoremheadsize#1{\def\theorem@headsize{#1}}
\def\theoremheadsep#1{\def\theorem@headsep{#1}}
\def\theorembodyfont#1{\def\theorem@bodyfont{#1}}
\def\theorembodysize#1{\def\theorem@bodysize{#1}}
\def\theoremleftmargin#1{\def\theorem@leftmargin{#1}}
\def\theoremrightmargin#1{\def\theorem@rightmargin{#1}}
\def\theoremheadindent#1{\def\theorem@headindent{#1}}
\def\theorembodyindent#1{\def\theorem@bodyindent{#1}}
\def\theorembodyfirstindent#1{\def\theorem@bodyfirstindent{#1}}
\def\theoremmargin#1{\theoremleftmargin{#1}\theoremrightmargin{#1}}
\def\theoremsize#1{\theorembodysize{#1}\theoremheadsize{#1}}
\def\theoremheadcolor#1{\def\theorem@headcolor{#1}}
\def\theoremheadfill#1{\def\theorem@headfill{#1}}
\def\theoremheadborder#1{\def\theorem@headborder{#1}}
\def\theorembodycolor#1{\def\theorem@bodycolor{#1}}
\def\theorembodyfill#1{\def\theorem@bodyfill{#1}}
\def\theorembodyborder#1{\def\theorem@bodyborder{#1}}
\def\theorembodyxsep#1{\def\theorem@bodyxsep{#1}}
\def\theorembodyysep#1{\def\theorem@bodyysep{#1}}
\def\theorembodylinewidth#1{\def\theorem@bodylinewidth{#1}}
\def\theoremcountersep#1{\def\theorem@countersep{#1}}
\def\theoremframe#1{\def\theorem@frame{#1}}
\def\theoremsidebarsymbol#1{\def\theorem@sidebarsymbol{#1}}
\def\theoremsidebarbefore#1{\def\theorem@sidebarbefore{#1}}
\def\theoremsidebarafter#1{\def\theorem@sidebarafter{#1}}
\def\theoremsidebarcolor#1{\def\theorem@sidebarcolor{#1}}
\def\theoremleftrule#1{\def\theorem@leftrule{#1}}
\def\theoremrightrule#1{\def\theorem@rightrule{#1}}
\def\theoremtoprule#1{\def\theorem@toprule{#1}}
\def\theorembottomrule#1{\def\theorem@bottomrule{#1}}
\long\def\thmname#1{\long\def\theorem@name##1{#1}}
\long\def\thmnumber#1{\long\def\theorem@number##1{#1}}
\long\def\thmnote#1{\long\def\theorem@note##1{#1}}
\long\def\thmtitleformat#1{% rename to titlestructure
  \long\def\theorem@titleformat##1##2##3##4{#1}%
}%
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% there should only be one command \theoremcolor which defines
% \theoremfilledcolor and \theoremnonfilledcolor;
% the theorem should then, depending on its type, use one or
% the other style
\def\theoremfilledcolor#1{\theorembodyfill{#1!25}%
                          \theorembodyborder{#1}%
                          \theorembodycolor{black}%
                          \theoremheadfill{#1}%
                          \theoremheadborder{#1}%
                          \theoremheadcolor{white}%
  }
\def\theoremnonfilledcolor#1{\theorembodyfill{#1!25}%
                          \theorembodyborder{#1}%
                          \theorembodycolor{black}%
                          \theoremheadfill{#1!25}%
                          \theoremheadborder{#1}%
                          \theoremheadcolor{#1}%
  }

%
% DEFINITION OF NEW THEOREMS
%

% note: we use very explicit names (e.g. \newtheorem@nopt@opt instead of things like \xnthm) both for clarity and to avoid any conflict if the user loads by mistake theorem.sty, ntheorem.sty or amsthm.sty before customtheorem package.
% see 

% for better error handling of theorem counters, consider using if's
% e.g. \newtheorem{Th}{Theorem} puts \ifdefinecounter to true and then
% \new@theorem takes care of defining the counter

\def\newtheorem{%
  \@ifstar{\newtheorem@star}{\newtheorem@nostar}}
\def\newtheorem@star#1#2{\new@theorem{#1}{#2}{}}
\def\newtheorem@nostar#1{%
  \@ifnextchar[{\newtheorem@opt{#1}}{\newtheorem@nopt{#1}}}
\def\newtheorem@nopt#1#2{%
  \@ifnextchar[{\newtheorem@nopt@opt{#1}{#2}}{\newtheorem@nopt@nopt{#1}{#2}}}
\def\newtheorem@opt#1[#2]#3{\new@theorem{#1}{#3}{#2}}
\def\newtheorem@nopt@opt#1#2[#3]{%
\TestIfCounterExists{#1}
\newcounter{#1}[#3]%
\global\expandafter\let\csname theorem@countersep@#1\endcsname=%
   \theorem@countersep
\global\expandafter\let\csname theorem@numbering@#1\endcsname=%
   \theorem@numbering
  \expandafter\gdef\csname the#1\endcsname{\expandafter\csname the#3\endcsname\expandafter\csname theorem@countersep@#1\endcsname\csname theorem@numbering@#1\endcsname{#1}}
  \new@theorem{#1}{#2}{#1}}
\def\newtheorem@nopt@nopt#1#2{
\TestIfCounterExists{#1}
\newcounter{#1}%
\global\expandafter\let\csname theorem@numbering@#1\endcsname=%
   \theorem@numbering
  \expandafter\gdef\csname the#1\endcsname{\csname theorem@numbering@#1\endcsname{#1}}
   \new@theorem{#1}{#2}{#1}}
\def\new@theorem#1#2#3{%
% #1 is theorem command name
% #2 is theorem body name
% #3 is counter name
%
% check if name #1 is available (e.g. \def and \th can not be used)
\TestIfNameAlreadyExists{#1}% this is not enough for \def
% capture of command definition when \newtheorem is called
\global\expandafter\let\csname theorem@begin@#1\endcsname=\theorem@begin
\global\expandafter\let\csname theorem@end@#1\endcsname=\theorem@end
\global\expandafter\let\csname theorem@preskip@#1\endcsname=%
   \theorem@preskip
\global\expandafter\let\csname theorem@postskip@#1\endcsname=%
   \theorem@postskip
\global\expandafter\let\csname theorem@prework@#1\endcsname=%
   \theorem@prework
\global\expandafter\let\csname theorem@postwork@#1\endcsname=%
   \theorem@postwork
\global\expandafter\let\csname theorem@headfont@#1\endcsname=%
   \theorem@headfont
\global\expandafter\let\csname theorem@headpunct@#1\endcsname=%
   \theorem@headpunct
\global\expandafter\let\csname theorem@headspace@#1\endcsname=%
   \theorem@headspace
\global\expandafter\let\csname theorem@headsize@#1\endcsname=%
   \theorem@headsize
\global\expandafter\let\csname theorem@headsep@#1\endcsname=%
   \theorem@headsep
\global\expandafter\let\csname theorem@bodyfont@#1\endcsname=%
   \theorem@bodyfont
\global\expandafter\let\csname theorem@bodysize@#1\endcsname=%
   \theorem@bodysize
\global\expandafter\let\csname theorem@leftmargin@#1\endcsname=%
   \theorem@leftmargin
\global\expandafter\let\csname theorem@rightmargin@#1\endcsname=%
   \theorem@rightmargin
\global\expandafter\let\csname theorem@headindent@#1\endcsname=%
   \theorem@headindent
\global\expandafter\let\csname theorem@bodyindent@#1\endcsname=%
   \theorem@bodyindent
\global\expandafter\let\csname theorem@bodyfirstindent@#1\endcsname=%
   \theorem@bodyfirstindent
\global\expandafter\let\csname theorem@headcolor@#1\endcsname=%
   \theorem@headcolor
\global\expandafter\let\csname theorem@headfill@#1\endcsname=%
   \theorem@headfill
\global\expandafter\let\csname theorem@headborder@#1\endcsname=%
   \theorem@headborder
\global\expandafter\let\csname theorem@bodycolor@#1\endcsname=%
   \theorem@bodycolor
\global\expandafter\let\csname theorem@bodyfill@#1\endcsname=%
   \theorem@bodyfill
\global\expandafter\let\csname theorem@bodyborder@#1\endcsname=%
   \theorem@bodyborder
\global\expandafter\let\csname theorem@bodyxsep@#1\endcsname=%
   \theorem@bodyxsep
\global\expandafter\let\csname theorem@bodyysep@#1\endcsname=%
   \theorem@bodyysep
\global\expandafter\let\csname theorem@bodylinewidth@#1\endcsname=%
   \theorem@bodylinewidth
\global\expandafter\let\csname theorem@frame@#1\endcsname=%
   \theorem@frame
\global\expandafter\let\csname theorem@name@#1\endcsname=%
   \theorem@name
\global\expandafter\let\csname theorem@number@#1\endcsname=%
   \theorem@number
\global\expandafter\let\csname theorem@note@#1\endcsname=%
   \theorem@note
\global\expandafter\let\csname theorem@titleformat@#1\endcsname=%
   \theorem@titleformat
\global\expandafter\let\csname theorem@sidebarsymbol@#1\endcsname=%
   \theorem@sidebarsymbol
\global\expandafter\let\csname theorem@sidebarbefore@#1\endcsname=%
   \theorem@sidebarbefore
\global\expandafter\let\csname theorem@sidebarafter@#1\endcsname=%
   \theorem@sidebarafter
\global\expandafter\let\csname theorem@sidebarcolor@#1\endcsname=%
   \theorem@sidebarcolor
\global\expandafter\let\csname theorem@leftrule@#1\endcsname=%
   \theorem@leftrule
\global\expandafter\let\csname theorem@rightrule@#1\endcsname=%
   \theorem@rightrule
\global\expandafter\let\csname theorem@toprule@#1\endcsname=%
   \theorem@toprule
\global\expandafter\let\csname theorem@bottomrule@#1\endcsname=%
   \theorem@bottomrule
%
\expandafter\gdef\csname define@theorem@commands@#1\endcsname{%
      \gdef\thetheorempreskip{%
         \expandafter\csname theorem@preskip@#1\endcsname}%
      \gdef\thetheorempostskip{%
         \expandafter\csname theorem@postskip@#1\endcsname}%
      \gdef\thetheoremleftmargin{%
         \expandafter\csname theorem@leftmargin@#1\endcsname}%
      \gdef\thetheoremrightmargin{%
         \expandafter\csname theorem@rightmargin@#1\endcsname}%
      \gdef\thetheoremheadindent{%
         \expandafter\csname theorem@headindent@#1\endcsname}%
      \gdef\thetheorembodyindent{%
         \expandafter\csname theorem@bodyindent@#1\endcsname}%
      \gdef\thetheorembodyfirstindent{%
         \expandafter\csname theorem@bodyfirstindent@#1\endcsname}%
      \gdef\thetheoremheadcolor{%
         \expandafter\csname theorem@headcolor@#1\endcsname}%
      \gdef\thetheoremheadsep{%
         \expandafter\csname theorem@headsep@#1\endcsname}%
      \gdef\thetheoremheadfill{%
         \expandafter\csname theorem@headfill@#1\endcsname}%
      \gdef\thetheoremheadborder{%
         \expandafter\csname theorem@headborder@#1\endcsname}%
      \gdef\thetheorembodycolor{%
         \expandafter\csname theorem@bodycolor@#1\endcsname}%
      \gdef\thetheorembodyfill{%
         \expandafter\csname theorem@bodyfill@#1\endcsname}%
      \gdef\thetheorembodyborder{%
         \expandafter\csname theorem@bodyborder@#1\endcsname}%
      \gdef\thetheorembodyxsep{%
         \expandafter\csname theorem@bodyxsep@#1\endcsname}%
      \gdef\thetheorembodyysep{%
         \expandafter\csname theorem@bodyysep@#1\endcsname}%
      \gdef\thetheorembodylinewidth{%
         \expandafter\csname theorem@bodylinewidth@#1\endcsname}%
      \gdef\thetheoremheadpunct{%
         \expandafter\csname theorem@headpunct@#1\endcsname}
      \gdef\thetheoremheadspace{%
         \expandafter\csname theorem@headspace@#1\endcsname}
% note: headpunct and headspace musn't be always put in a unique command or else problem will arise when a label is used after \begin{th} in some cases (standard and breakbeforelist)
      \gdef\aftertheoremhead{%
         \expandafter\csname theorem@headpunct@#1\endcsname
         \expandafter\csname theorem@headspace@#1\endcsname}%
      \gdef\thetheoremsidebarsymbol{%
         \expandafter\csname theorem@sidebarsymbol@#1\endcsname}%
      \gdef\thetheoremsidebarbefore{%
         \expandafter\csname theorem@sidebarbefore@#1\endcsname}%
      \gdef\thetheoremsidebarafter{%
         \expandafter\csname theorem@sidebarafter@#1\endcsname}%
      \gdef\thetheoremsidebarcolor{%
         \expandafter\csname theorem@sidebarcolor@#1\endcsname}%
      \gdef\formattheoremhead{%
%          \color{\expandafter\csname theorem@headcolor@#1\endcsname}% is not here because of side effects with breakbox
          \expandafter\csname theorem@headfont@#1\endcsname
          \expandafter\csname theorem@headsize@#1\endcsname}%
      \gdef\formattheorembody{%
          \expandafter\csname theorem@bodyfont@#1\endcsname
          \expandafter\csname theorem@bodysize@#1\endcsname}%
      \gdef\thetheoremframe{%
          \expandafter\csname theorem@frame@#1\endcsname}%
      \gdef\thetheoremleftrule{%
          \expandafter\csname theorem@leftrule@#1\endcsname}%
      \gdef\thetheoremrightrule{%
          \expandafter\csname theorem@rightrule@#1\endcsname}%
      \gdef\thetheoremtoprule{%
          \expandafter\csname theorem@toprule@#1\endcsname}%
      \gdef\thetheorembottomrule{%
          \expandafter\csname theorem@bottomrule@#1\endcsname}%
      \gdef\thetheoremnamestructure####1{%
          \expandafter\csname theorem@name@#1\endcsname{####1}}%
      \gdef\thetheoremnumberstructure####1{%
          \expandafter\csname theorem@number@#1\endcsname{####1}}%
      \gdef\thetheoremnotestructure####1{%
          \expandafter\csname theorem@note@#1\endcsname{####1}}%
      \gdef\thetheoremtitleformat####1####2####3####4{%
          \expandafter\csname theorem@titleformat@#1\endcsname{####1}{####2}{####3}{####4}}
      }%
%
% it must be tested whether or not \#1 exists or else a user doing
% \newtheorem{def}{Definition} might encounter infinite loops when
% defining other theorems
\ifcsname#1\endcsname
  \expandafter\newcommand\csname#1\endcsname{}
\else
  \expandafter\gdef\csname#1\endcsname{%
  \@ifnextchar[{\expandafter\csname#1@opt\endcsname}%
               {\expandafter\csname#1@nopt\endcsname}%
  }
%
% BUG TO CLEAR: refstepcounter should be placed correctly
%               putting it inside \thetheoremnumber will make
%               references fail
%
\expandafter\gdef\csname#1@nopt\endcsname{%
  \expandafter\csname theorem@prework@#1\endcsname
%  note: the refstepcounter here may cause some trouble with hyperref
  \@ifnotempty{#3}{\refstepcounter{#3}}%
  \expandafter\csname define@theorem@commands@#1\endcsname
  \gdef\thetheoremname{\thetheoremnamestructure{#2}}%
  \@ifempty{#3}%
     {\gdef\thetheoremnumber{}}%
     {\gdef\thetheoremnumber{\thetheoremnumberstructure
         {\expandafter\csname the#3\endcsname}}}%
  \gdef\thetheoremnote{}%
  \thetheoremnoteemptytrue
  \expandafter\csname theorem@begin@#1\endcsname
% DON'T PUT a \leavevmode here or else, in some cases, there will be a spurious space if a \label is just after the theorem style (e.g. in break style)
% (but a \leavevmode here would avoid the hyperref \label bug, so it may be a good idea to put a \leavevmode in some of the styles (all except break?))
  \ignorespaces %\@ifnextchar\[{\vspace{-12pt}}{}
  }%
\expandafter\gdef\csname#1@opt\endcsname[##1]{%
  \expandafter\csname theorem@prework@#1\endcsname
%  putting a refstepcounter here may cause some trouble with hyperref
  \@ifnotempty{#3}{\refstepcounter{#3}}%%% commented out
  \expandafter\csname define@theorem@commands@#1\endcsname
  \gdef\thetheoremname{\thetheoremnamestructure{#2}}%
  \@ifempty{#3}%
     {\gdef\thetheoremnumber{}}%
     {\gdef\thetheoremnumber{\thetheoremnumberstructure
                             {\expandafter\csname the#3\endcsname}}}%
  \gdef\thetheoremnote{\thetheoremnotestructure{##1}}%
  \thetheoremnoteemptyfalse
  \expandafter\csname theorem@begin@#1\endcsname
% DON'T PUT a \leavevmode here or else, in some cases, there will be a spurious space if a \label is just after the theorem style (e.g. in break style)
  \ignorespaces %\@ifnextchar\[{\vspace{-12pt}}{}
  }%
\expandafter\gdef\csname end#1\endcsname{\par\expandafter\csname theorem@end@#1\endcsname\expandafter\csname theorem@postwork@#1\endcsname}% a \par is needed here so that a font size change also changes the baselineskip
\fi
}

%
% FRAMING COMMANDS
%

\def\nobox{}
\def\endnobox{}

%
% DEFINITION OF THEOREM STYLES
%

% TO DO: incorporate a theoremnotefont (?), etc. in the style

% standard style
% current bugs:
% - too much space after theorem if it ends by a displaymath
% fixed bugs:
% - nothing should be after \thetheoremheadspace or else if \begin{theorem} is followed by a label, an undue space will be inserted; this concerns both the \strut and the \color{...} command and explains why \thetheoremheadpunct and \thetheoremheadspace are used separately instead of a simple \aftertheoremhead.
\leftmargin=0cm
\rightmargin=0cm
\def\standardtheorembegin{%
  \addtolength\leftmargin\thetheoremleftmargin
  \addtolength\rightmargin\thetheoremrightmargin
  \@totalleftmargin=\leftmargin
  \advance\linewidth-\thetheoremleftmargin
  \advance\linewidth-\thetheoremrightmargin
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent
  \parshape 1 \leftmargin \linewidth
  \everypar{\parshape 1 \leftmargin \linewidth}%
  \begin{\thetheoremframe}%
  {\formattheoremhead
     \textcolor{\thetheoremheadcolor}{%
        \thetheoremtitleformat{\thetheoremname}
                              {\thetheoremnumber}
                              {\thetheoremnote}
                              {\thetheoremheadpunct\strut}\par \nobreak%
     }%
  }%
  \color{\thetheorembodycolor}%
  \formattheorembody
  \thetheoremheadspace % should be empty if \thetheoremname, \thetheoremnumber and \thetheoremnote are empty
}%
\def\standardtheoremend{\end{\thetheoremframe}%
\par\@endpefalse\addvspace{\thetheorempostskip}}
\leftmargin=0cm
\rightmargin=0cm

% plain style, like in amsthm or ntheorem
\def\plaintheorembegin{%
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \begin{\thetheoremframe}\begin{list}{}%
    {\topsep=0pt
     \leftmargin=\thetheoremleftmargin
     \rightmargin=\thetheoremrightmargin
     \labelwidth=0cm
     \itemindent=\thetheoremheadindent
     \parsep=\parskip
     \listparindent=\parindent}%
 \item[\hspace{\labelsep}{\formattheoremhead\textcolor{\thetheoremheadcolor}{\thetheoremtitleformat{\thetheoremname}{\thetheoremnumber}{\thetheoremnote}{\aftertheoremhead\strut}}}\hspace{-\labelsep}]\par\nobreak%
     \color{\thetheorembodycolor}\formattheorembody}
\def\plaintheoremend{\end{list}\end{\thetheoremframe}\@endpefalse\addvspace{\thetheorempostskip}}

% style which breaks before lists
% remark: if used in a quotation environment, will have an indent
% using \leavevmode\vspace{-\baselineskip} does not always work well (e.g. after a \paragraph{} command with nothing else after)
\def\breakbeforelisttheorembegin{%
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \begin{\thetheoremframe}\begin{list}{}%
    {\@beginparpenalty=10000% to avoid break before a list in the theorem
     \topsep=0pt
     \leftmargin=\thetheoremleftmargin
     \rightmargin=\thetheoremrightmargin
     \labelwidth=0cm
     \itemindent=\thetheoremheadindent
     \parsep=\parskip
     \listparindent=\parindent}%
     \item\relax{\formattheoremhead\textcolor{\thetheoremheadcolor}{\thetheoremtitleformat{\thetheoremname}{\thetheoremnumber}{\thetheoremnote}{\thetheoremheadpunct\strut}}}\nobreak
     \color{\thetheorembodycolor}%
     \formattheorembody\thetheoremheadspace
}
\def\breakbeforelisttheoremend{\end{list}\end{\thetheoremframe}\@endpefalse\addvspace{\thetheorempostskip}}

% break style, like in ntheorem
%%%% -> base it on the standard style and use a \par after the theorem title
\def\breaktheorembegin{%
  \clearbeforetheorem\addvspace{\thetheorempreskip}
  \begin{\thetheoremframe}\begin{list}{}%
    {\@beginparpenalty=10000% to avoid break before a list in the theorem
     \topsep=5pt
     \leftmargin=\thetheoremleftmargin
     \rightmargin=\thetheoremrightmargin
     \labelwidth=0cm
     \itemsep=\thetheoremheadsep % to adjust vertical space after theorem
%     \itemindent=\thetheoremheadindent
     \parsep=\parskip
     \listparindent=\parindent
    }%
    \item\relax
    {\hspace{\thetheoremheadindent}%
     \formattheoremhead
     \textcolor{\thetheoremheadcolor}{%
        \thetheoremtitleformat{\thetheoremname}
                              {\thetheoremnumber}
                              {\thetheoremnote}
                              {\thetheoremheadpunct\strut}
     }%
    }%
    \formattheorembody % --> to avoid any \color putting a breaking point, put it at the \item above ? (and cancel its action for the theorem head)
    \nobreakitem[\hspace{\thetheorembodyfirstindent}]\relax %beware: using \color{\thetheorembodycolor} could introduce a break point here
}
\def\breaktheoremend{\end{list}\end{\thetheoremframe}\@endpefalse\addvspace{\thetheorempostskip}}

% sidebar theorem

\def\sidebartheorembegin{%
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent\hspace{\thetheoremleftmargin}%
  \setlength{\temptheoremlength}%
        {\columnwidth-\thetheoremleftmargin-\thetheoremrightmargin}%
  \noindent\begin{minipage}{\temptheoremlength}%
  \begin{lrbox}{\temptheoremtextbox}%
    \setlength{\temptheoremlength}%
      {\columnwidth-\thetheoremsidebarbefore-\thetheoremsidebarafter-\widthof{$\left\thetheoremsidebarsymbol\vbox to 48pt{\hbox{}}\right.$\,}}
     \begin{minipage}{\temptheoremlength}%
     \parindent=\saveparindent\color{\thetheorembodycolor}\formattheorembody}
\def\sidebartheoremend{%
   \end{minipage}\end{lrbox}%
   {\formattheoremhead\textcolor{\thetheoremheadcolor}{\thetheoremname\thetheoremnumber\thetheoremnote\aftertheoremhead}}\par\nobreak\vspace{6pt}%
   \noindent\hspace{\thetheoremsidebarbefore}\color{\thetheoremsidebarcolor}$\delimitershortfall=0pt\left\thetheoremsidebarsymbol\hspace{\thetheoremsidebarafter} \usebox{\temptheoremtextbox}\right.$\end{minipage}\par
   \addvspace{\thetheorempostskip}%
}

% sidebar with page break style (needs customframed)

\def\sidebarwithpagebreaktheorembegin{%
  \TestIfCmdExistsFromPackageWhenUsing{\leftsidebarbox}%
    {customframed}{sidebarwithpagebreak}
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \renewcommand{\leftsidebarBoxsymbol}{\thetheoremsidebarsymbol}%
  \setlength{\leftsidebarBoxleftmargin}{\thetheoremsidebarbefore}%
  \setlength{\leftsidebarBoxinnersep}{\thetheoremsidebarafter}%
  \renewcommand{\leftsidebarBoxsymbolcolor}{\thetheoremsidebarcolor}%
  \renewcommand{\leftsidebarBoxbeforematerialhook}%
    {\color{\thetheorembodycolor}}%
  \begin{leftsidebarbox}{\formattheoremhead
                         \textcolor{\thetheoremheadcolor}{%
                            \thetheoremname
                            \thetheoremnumber
                            \thetheoremnote\aftertheoremhead}}
  \parindent=\saveparindent
  \color{\thetheorembodycolor}\formattheorembody}

\def\sidebarwithpagebreaktheoremend{%
   \end{leftsidebarbox}%
   \par\addvspace{\thetheorempostskip}%
}

% framed theorems

\def\framedtheorembegin{%
 \TestIfCmdExistsFromPackageWhenUsing{\framed}{framed}{framed}
  \addtolength\leftmargin\thetheoremleftmargin
  \addtolength\rightmargin\thetheoremrightmargin
  \@totalleftmargin=\leftmargin
  \advance\linewidth-\thetheoremleftmargin
  \advance\linewidth-\thetheoremrightmargin
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent
  \parshape 1 \leftmargin \linewidth
  \everypar{\parshape 1 \leftmargin \linewidth}%
  \MakeFramed{\advance\hsize-\width\advance\hsize-\@totalleftmargin \FrameRestore}\noindent
  {\formattheoremhead
     \textcolor{\thetheoremheadcolor}{%
        \thetheoremtitleformat{\thetheoremname}
                              {\thetheoremnumber}
                              {\thetheoremnote}
                              {\thetheoremheadpunct\strut}%
     }%
  }%
  \color{\thetheorembodycolor}%
  \formattheorembody
  \thetheoremheadspace % should be empty if \thetheoremname, \thetheoremnumber and \thetheoremnote are empty
}
\def\framedtheoremend{\endMakeFramed\addvspace{\thetheorempostskip}}

% for theorem styles using tikz, make a command \defineenvironmentfromcommand{...} which uses either the lrbox, the Collect@Body or the external file trick, depending on what is wanted (the lrbox trick should be enough in most cases).

\def\leftellipsetheorembegin{%
  \TestIfCmdExistsFromPackageWhenUsing{\tikzpicture}{tikz}{leftellipse}
  \TestIfTikZShapeLibraryLoadedFor{leftellipse}
% there should be a warning if used in two or more columns style
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent\hspace{\thetheoremleftmargin}%
  \setlength{\temptheoremlength}%
        {\linewidth-\thetheoremleftmargin-\thetheoremrightmargin}%
  \noindent\begin{minipage}{\temptheoremlength}%
  \begin{lrbox}{\temptheoremtextbox}%
     \begin{minipage}{0.95\columnwidth}%
     {\formattheoremhead\thetheoremnote}%
     \parindent=\saveparindent\color{\thetheorembodycolor}\formattheorembody}
\def\leftellipsetheoremend{%
   \end{minipage}\end{lrbox}%
   \begin{tikzpicture}
     \tikzstyle{titlebox}=[fill=\thetheorembodyfill, rectangle, inner sep=10pt, inner ysep=10pt]
     \tikzstyle{title}=[left=0.1cm,color=\thetheoremheadborder,fill=\thetheoremheadfill,text=\thetheoremheadcolor,ellipse,draw,very thick]
     \node[titlebox] (textbox) at (0,0) {\usebox{\temptheoremtextbox}};
     \useasboundingbox (textbox);
      \draw[color=\thetheorembodyborder,very thick] (textbox.north west)--(textbox.south west);
     \node[title] at (textbox.west) {\formattheoremhead\thetheoremname\thetheoremnumber};
   \end{tikzpicture}\end{minipage}\par
   \addvspace{\thetheorempostskip}}

% rectangle with title followed by shadowed rectangle with content
\def\gluedrectangletheorembegin{%
 \TestIfCmdExistsFromPackageWhenUsing{\tikzpicture}{tikz}{gluedrectangle}
  % needs also the calc tikzlibrary, but tikz' error message is clear enough 
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent\hspace{\thetheoremleftmargin}%
  \setlength{\temptheoremlength}%
        {\linewidth-\thetheoremleftmargin-\thetheoremrightmargin}%
  \noindent\begin{minipage}{\temptheoremlength}%
  \begin{lrbox}{\temptheoremtextbox}%
     \begin{minipage}{\columnwidth-10pt-10pt}%
     \parindent=\saveparindent\color{\thetheorembodycolor}\formattheorembody}
\def\gluedrectangletheoremend{%
   \end{minipage}\end{lrbox}%
   \begin{tikzpicture}
      \tikzstyle{titlebox} = [draw=blue!80,
 rectangle, rounded corners, inner sep=10pt, inner ysep=10pt, text=\thetheorembodycolor]%
% la boîte du nom du théorème
\tikzstyle{title}=[inner sep=5pt,text=white,fill=blue!50,rectangle,rounded corners]%
      \node[titlebox] (box){\usebox{\temptheoremtextbox}};
%  \useasboundingbox (box);
%  \draw[color=\thetheorembodyborder,very thick] ($ (box.north west) + (0.8pt,-0.2pt)$) -- ($(box.south west) + (0.8pt,0.2pt)$);
   \node[right,title]    (boxon)   at    ($   (box.north    west)   +
      (0,10pt-1.8pt)$){\begin{minipage}{80pt}\formattheoremhead\fontfamily{ugq}\selectfont\thetheoremname\thetheoremnumber\end{minipage}};
%le titre
 \node[right,text=blue!70] at ($ (boxon.east) $){\begin{minipage}{\columnwidth-5pt-5pt}\formattheoremhead\fontfamily{ugq}\selectfont\thetheoremnote\end{minipage}};
  \end{tikzpicture}\end{minipage}\par
   \addvspace{\thetheorempostskip}%
}

% framed theorem with title centered on the above rule
\def\centeredframedtheorembegin{%
 \TestIfCmdExistsFromPackageWhenUsing{\tikzpicture}{tikz}{centeredframed}
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent\hspace{\thetheoremleftmargin}%
  \setlength{\temptheoremlength}%
        {\columnwidth-\thetheoremleftmargin-\thetheoremrightmargin}%
  \noindent\begin{minipage}{\temptheoremlength}%
  \begin{lrbox}{\temptheoremtextbox}%
     \begin{minipage}{\columnwidth-10pt-10pt}%
     \parindent=\saveparindent\medskip\color{\thetheorembodycolor}\formattheorembody}
\def\centeredframedtheoremend{%
   \end{minipage}\end{lrbox}%
   \begin{tikzpicture}
     \tikzstyle{titlebox}=[color=\thetheorembodyborder, rectangle, inner sep=10pt, inner ysep=10pt, draw, fill=\thetheorembodyfill, text=\thetheorembodycolor]%
     \tikzstyle{title}=[color=\thetheoremheadborder, fill=\thetheoremheadfill, text=\thetheoremheadcolor, draw]
     \node[titlebox] (box) {\usebox{\temptheoremtextbox}};
     \node[title] at (box.north) {\formattheoremhead\thetheoremname\thetheoremnumber\thetheoremnote};
   \end{tikzpicture}\end{minipage}\par
   \addvspace{\thetheorempostskip}%
}

% upwards theorem title to the left of the theorem
\def\leftrotatedtheorembegin{%
 \TestIfCmdExistsFromPackageWhenUsing{\tikzpicture}{tikz}{leftrotated}
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent\hspace{\thetheoremleftmargin}%
  \setlength{\temptheoremlength}%
        {\columnwidth-\thetheoremleftmargin-\thetheoremrightmargin}%
  \noindent\begin{minipage}{\temptheoremlength}%
  \begin{lrbox}{\temptheoremtextbox}%
     \begin{minipage}{\columnwidth-10pt-10pt}%
     {\formattheoremhead\thetheoremnote}%
     \parindent=\saveparindent\color{\thetheorembodycolor}%
     \formattheorembody}
\def\leftrotatedtheoremend{%
   \end{minipage}\end{lrbox}%
   \begin{tikzpicture}
     \tikzstyle{titlebox} = [fill=\thetheorembodyfill, rectangle, inner sep=10pt, inner ysep=10pt, text=\thetheorembodycolor]
     \tikzstyle{title}=[text=\thetheoremheadcolor,above,rotate=90]
     \node[titlebox] (box){\usebox{\temptheoremtextbox}};
     \useasboundingbox (box);
     \draw[color=\thetheorembodyborder,very thick] (box.north west)--(box.south west);
     \node[title] at (box.west) {\formattheoremhead\thetheoremname\thetheoremnumber};
   \end{tikzpicture}\end{minipage}\par
   \addvspace{\thetheorempostskip}%
}

% upwards boxed theorem title to the left of the theorem
\def\leftboxedrotatedtheorembegin{%
 \TestIfCmdExistsFromPackageWhenUsing{\tikzpicture}{tikz}{leftboxedrotated}
  \clearbeforetheorem\addvspace{\thetheorempreskip}%
  \noindent\hspace{\thetheoremleftmargin}%
  \setlength{\temptheoremlength}%
        {\columnwidth-\thetheoremleftmargin-\thetheoremrightmargin-\widthof{\begin{tikzpicture}\node[rotate=90, inner sep=0pt, outer sep=0pt] {\formattheoremhead\thetheoremname\thetheoremnumber};\end{tikzpicture}}}%
  \noindent\begin{minipage}{\temptheoremlength}%
  \begin{lrbox}{\temptheoremtextbox}%
     \begin{minipage}[t]{\columnwidth-10pt-10pt-5pt-5pt}%
     {\formattheoremhead\thetheoremnote}%
     \parindent=\saveparindent\color{\thetheorembodycolor}%
     \formattheorembody}
\def\leftboxedrotatedtheoremend{%
   \end{minipage}\end{lrbox}%
\setlength{\temptheoremlengthbis}{\widthof{\begin{tikzpicture}\node[rotate=90, draw, inner sep=10pt] {\formattheoremhead\thetheoremname\thetheoremnumber};\end{tikzpicture}}-10pt}%
   \begin{tikzpicture}
     \tikzstyle{titlebox} = [draw, color=\thetheorembodyborder, fill=\thetheorembodyfill, rectangle, inner sep=10pt, inner ysep=10pt, text=\thetheorembodycolor]
     \tikzstyle{title}=[text=\thetheoremheadcolor, rotate=90, inner sep=5pt, inner ysep=5pt]
     \node[titlebox,below right] (box) at (0,0) {\setlength{\temptheoremlength}{\heightof{\begin{tikzpicture}\node[rotate=90, inner sep=0pt, outer sep=0pt] {\formattheoremhead\thetheoremname\thetheoremnumber};\end{tikzpicture}}-10pt}\vtop to \temptheoremlength{\hbox{}}\usebox{\temptheoremtextbox}};
%     \useasboundingbox (box);
     \draw[color=\thetheoremheadborder, fill=\thetheoremheadfill] ($(box.north west) - (0.2pt,0.2pt)$) rectangle ($(box.south west) - (\temptheoremlengthbis,0cm) + (0.2pt,0.2pt)$);
     \node[title,above left] (titlebox) at (0,0) {\formattheoremhead\thetheoremname\thetheoremnumber};
   \end{tikzpicture}\end{minipage}\par
   \addvspace{\thetheorempostskip}%
}

% theorem title above the upper left corner of the framed content
\def\titleaboveframetheorembegin{%
 \TestIfCmdExistsFromPackageWhenUsing{\breakbox}{boites}{titleaboveframe}
  \def\breakboxparindent{\saveparindent}
  \def\bkvz@before@breakbox{\ifhmode\par\fi\vskip\thetheorempreskip}%
  \def\bkvz@set@linewidth{\advance\linewidth-\thetheorembodyxsep
                          \advance\linewidth-\thetheorembodyxsep
                          \advance\linewidth-\thetheoremleftmargin
                          \advance\linewidth-\thetheoremrightmargin}%
  \def\bkvz@left{\thetheoremleftrule}
  \def\bkvz@right{\thetheoremrightrule}
  \def\bk@line{\hbox to \linewidth{%
     \begingroup\hspace{\thetheoremleftmargin}%
     \color{\thetheorembodyborder}
     \bkvz@left
     \endgroup
     \fboxsep=0pt\colorbox{\thetheorembodyfill}{%
          \hspace{\thetheorembodyxsep}%
          \box\bk@bxa\hspace{\thetheorembodyxsep}}%
     \begingroup\hspace{-\thetheorembodylinewidth}%
     \color{\thetheorembodyborder}%
     \bkvz@right
     \endgroup}}
  \def\bkvz@top{\noindent
    {\hspace{\thetheoremheadindent}\formattheoremhead\textcolor{\thetheoremheadcolor}{\thetheoremtitleformat{\thetheoremname}{\thetheoremnumber}{\thetheoremnote}{\aftertheoremhead}}}%
    \par\nobreak\vspace{\thetheoremheadsep}%
    \advance\linewidth-\thetheoremleftmargin
    \advance\linewidth-\thetheoremrightmargin
    \advance\linewidth\thetheorembodylinewidth
    \noindent\hspace{\thetheoremleftmargin}%
    \begingroup\color{\thetheorembodyborder}%
    \thetheoremtoprule
    \endgroup\par\nobreak}
  \def\bkvz@bottom{\advance\linewidth-\thetheoremleftmargin
    \advance\linewidth-\thetheoremrightmargin
    \advance\linewidth\thetheorembodylinewidth
    \noindent\hspace{\thetheoremleftmargin}%
    \begingroup\color{\thetheorembodyborder}%
    \thetheorembottomrule
    \endgroup}
  \breakbox\vspace{\thetheorembodyysep}}
\def\titleaboveframetheoremend{\vspace{\thetheorembodyysep}\endbreakbox\addvspace{\thetheorempostskip}}

% old lefthanged definition (uses TikZ)
%\def\lefthangedtheorembegin{%
%  \clearbeforetheorem\addvspace{\thetheorempreskip}%
%  \noindent\hspace{\thetheoremleftmargin}%
%  \setlength{\temptheoremlength}%
%        {\columnwidth-\thetheoremleftmargin-\thetheoremrightmargin}%
%  \noindent\begin{minipage}{\temptheoremlength}%
%  \begin{lrbox}{\temptheoremtextbox}%
%     \begin{minipage}{\columnwidth-10pt-10pt}%
%     \parindent=\saveparindent\color{\thetheorembodycolor}\formattheorembody}
%\def\lefthangedtheoremend{%
%   \end{minipage}\end{lrbox}%
%   \begin{tikzpicture}
%     \tikzstyle{titlebox} = [color=\thetheorembodyborder, fill=\thetheorembodyfill, rectangle, inner sep=10pt, inner ysep=10pt,draw, text=\thetheorembodycolor]
%     \tikzstyle{title}=[text=\thetheoremheadcolor, above right]
%     \node[titlebox] (box){\usebox{\temptheoremtextbox}};
%     \useasboundingbox (box.south west) rectangle ($ (box.north east) + (0cm,10pt+0.15cm)$);
%     \node[title] (titlebox) at ($ (box.north west) + (-1cm,0.15cm)$) {\formattheoremhead\thetheoremname\thetheoremnumber\thetheoremnote};
%     \node at (titlebox.east) {\formattheoremhead\thetheoremnote};
%  \end{tikzpicture}\end{minipage}\par
%   \addvspace{\thetheorempostskip}%
%}

%
% DEFINE DEFAULT THEME
%

\def\swapnumbers{
\thmtitleformat{##2##1##3##4}
\thmnumber{\textup{##1}~}}

\def\marginnumbers{
\thmtitleformat{\llap{##2}##1##3##4}
\thmnumber{\textup{##1}~}}

\def\theoremstyle#1{
   \csname theorem@style@#1\endcsname}

% this style resets everything to the default values
\def\theorem@style@reset{
\theoremformat{standard}
\theoremnumbering{arabic}
\theorempreskip{\topsep}
\theorempostskip{\topsep}
\theoremheadfont{\bfseries}
\theoremheadpunct{.}
\theoremheadspace{\nobreakspace}
\theoremheadsize{\normalsize}
\theoremheadsep{4pt}
\theorembodyfont{\itshape}
\theorembodysize{\normalsize}
\theoremleftmargin{0cm}
\theoremrightmargin{0cm}
\theoremheadindent{0cm}
\theorembodyfirstindent{\parindent}
\theorembodyindent{\parindent}
\theoremheadcolor{black}
\theoremheadfill{white}
\theoremheadborder{white}
\theorembodycolor{black}
\theorembodyfill{white}
\theorembodyborder{black}
\theorembodyxsep{5pt}
\theorembodyysep{2pt}
\theorembodylinewidth{0.4pt}
\theoremcountersep{.}
\theoremframe{nobox}
\theoremprework{}
\theorempostwork{}
\theoremsidebarsymbol{|}
\theoremsidebarbefore{0.75cm}
\theoremsidebarafter{0.75cm}
\theoremsidebarcolor{black}
\theoremleftrule{\standardtheoremverticalrule}
\theoremrightrule{\standardtheoremverticalrule}
\theoremtoprule{\standardtheoremhorizontalrule}
\theorembottomrule{\standardtheoremhorizontalrule}
\thmname{##1}
\thmnumber{~\textup{##1}}% numbers should be upright by default
\thmnote{~(##1)}
\thmtitleformat{##1##2##3##4}
}

% this style is similar to the reset style, but keeps thmnote, thmnumber, thmname and thmtitleformat commands
\def\theorem@style@standard{
\theoremformat{standard}
\theorempreskip{\topsep}
\theorempostskip{\topsep}
\theoremheadfont{\bfseries}
\theoremheadpunct{.}
\theoremheadspace{\nobreakspace}
\theoremheadsize{\normalsize}
\theoremheadsep{4pt}
\theorembodyfont{\itshape}
\theorembodysize{\normalsize}
\theoremleftmargin{0cm}
\theoremrightmargin{0cm}
\theoremheadindent{0cm}
\theoremheadcolor{black}
\theoremheadfill{white}
\theoremheadborder{white}
\theorembodycolor{black}
\theorembodyfill{white}
\theorembodyborder{white}
\theorembodyxsep{5pt}
\theorembodyysep{2pt}
\theorembodylinewidth{0.4pt}
\theoremcountersep{.}
\theoremframe{nobox}
\theoremprework{}
\theorempostwork{}
\theoremsidebarsymbol{|}
\theoremsidebarbefore{0.5cm}
\theoremsidebarafter{0.25cm}
\theoremsidebarcolor{0.3white}
\theoremleftrule{\standardtheoremverticalrule}
\theoremrightrule{\standardtheoremverticalrule}
\theoremtoprule{\standardtheoremhorizontalrule}
\theorembottomrule{\standardtheoremhorizontalrule}
}

\def\theorem@style@breakbeforelist{
\theoremformat{breakbeforelist}
}

\def\theorem@style@titleaboveframe{
\theoremstyle{standard}
\theoremformat{titleaboveframe}
\theoremheadpunct{}
\theoremheadsep{6pt}
\theoremheadindent{-1cm}
\theorembodyxsep{5pt}
\theorembodyysep{2pt}
}

% LEGACY STYLES COMMON TO THEOREM, NTHEOREM AND AMSTHM PACKAGES

\def\theorem@style@plain{
\theoremstyle{standard}
\theoremformat{plain}
\theoremheadspace{\hskip 5pt plus1pt minus1pt\relax}
}
%should there be an option to select the theorem/ntheorem version instead ?

% LEGACY STYLES FOR AMSTHM

\def\theorem@style@definition{
\theoremstyle{standard}
\theorembodyfont{\upshape}
\theoremheadspace{\hskip 5pt plus1pt minus1pt\relax}
}

\def\theorem@style@remark{
\theoremstyle{standard}
\theorembodyfont{\upshape}
\theoremheadfont{\itshape}
\setlength{\temptheoremlength}{\topsep/2}
\theorempreskip{\temptheoremlength}
\theorempostskip{\temptheoremlength}
\theoremheadspace{\hskip 5pt plus1pt minus1pt\relax}
}

% LEGACY STYLES COMMON TO THEOREM AND NTHEOREM PACKAGES

\def\theorem@style@break{
\theoremstyle{standard}
\theoremformat{break}
\theoremheadsep{0pt}
\theorembodyfirstindent{0pt}
}

\def\theorem@style@change{
\theoremstyle{standard}
\swapnumbers
}

\def\theorem@style@changebreak{
\theoremstyle{break}
\swapnumbers
}

\def\theorem@style@margin{
\theoremstyle{standard}
\marginnumbers
}

\def\theorem@style@marginbreak{
\theoremstyle{break}
\marginnumbers
}

% LEGACY STYLES FOR NTHEOREM

\def\theorem@style@nonumberplain{
\theoremstyle{standard}
\thmtitleformat{##1##3##4}
}

\def\theorem@style@nonumberbreak{
\theoremstyle{break}
\thmtitleformat{##1##3##4}
}

\def\theorem@style@empty{
\theoremstyle{standard}
\theoremheadspace{}
\thmnote{##1}
\thmtitleformat{\ifthetheoremnoteempty\else##3##4\fi}
}

% NEW STYLES

\def\theorem@style@marginhead{
\theoremstyle{standard}
\thmtitleformat{\llap{##1##2}##3##4}
\thmnumber{~\textup{##1}~~}
\thmnote{(##1)~}
\theoremheadpunct{}
\theoremheadspace{}
}

% we now select the default style
\theoremstyle{reset}

\endinput
