% \iffalse  % -*- LaTeX -*-
%% This is the thmbox package.
%% (c) Emmanuel Beffara, 2002--2005 (LPPL)
%%
%<*driver>
\documentclass{ltxdoc}
\usepackage[latin1]{inputenc}
\usepackage{url,multicol,alltt}
\usepackage{thmbox}
\title{Package \texttt{thmbox}}
\author{Emmanuel \textsc{Beffara}\\\url{manu@beffara.org}}
\date{version 2.0, April 2005}
\newtheorem[L]{thm}{Theorem}[section]
\newtheorem{cor}[thm]{Corollary}
\renewcommand\{{\char`\{}
\renewcommand\}{\char`\}}
\newcommand\mac[1]{\texttt{\string#1}}
\newcommand\kv[2]{\texttt{#1=}$\langle$\textmd{\itshape#2}$\rangle$}
\newenvironment{changelog}{%
  \def\version##1##2{%
    \stoplist%
    \par\smallskip\noindent Version ##1 (##2)%
    \let\stoplist\dostoplist%
    \startlist}
  \def\startlist{%
    \begin{list}{-- }{\topsep=0pt\parsep=0pt\itemsep=0pt}}%
  \let\stoplist\relax%
  \def\dostoplist{\end{list}}%
}{\stoplist}
\begin{document}
\DocInput{thmbox.dtx}
\end{document}
%</driver>
% \fi
%
%\maketitle
%
% \begin{abstract}
%   This package defines an environment \texttt{thmbox} aimed at presenting
%   theorems, definitions and similar objects in boxes decorated with frames
%   and various aesthetic features. The standard macro |\newtheorem| is
%   redefined to use this format.
% \end{abstract}
%
% \section{Documentation}
%
% The package is loaded like any other, by writing
% \begin{quote}
%   \ttfamily \string\usepackage[\textrm{\itshape options}]\{thmbox\}
% \end{quote}
% The option \texttt{nothm} prevents the command |\newtheorem| from being
% redefined, so theorems defined with this command keep their traditional
% aspect. All other options are considered as default formatting options, they
% can be redefined at any time using the command |\thmboxoptions|. The
% argument of this macro is a list of |key=value| pairs in the \texttt{keyval}
% style, as defined in section~\ref{sec:options}.
%
% \subsection{Examples}
%
% The package defines an environment |thmbox| that is used as follows:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{thmbox\}[L]\{The title\}\\
%   \strut~~Some text, some more text, a\\
%   \strut~~sufficient amount to get a\\
%   \strut~~full box with several lines.\\
%   \string\end\{thmbox\}}
% \columnbreak
%   \begin{thmbox}[L]{The title}
%     Some text, some more text, a sufficient amout to get a full box
%     with several lines.
%   \end{thmbox}
% \end{multicols}
% \noindent
% The argument |[L]| indicates the style of the box. The two other defined
% styles are |[M]| and |[S]|, which make respectively:
% \begin{multicols}{2}
%   \begin{thmbox}[M]{The title}
%     Some text, some more text, a sufficient amout to get a full box
%     with several lines.
%   \end{thmbox}
% \columnbreak
%   \begin{thmbox}[S]{The title}
%     Some text, some more text, a sufficient amout to get a full box
%     with several lines.
%   \end{thmbox}
% \end{multicols}
% \noindent
% Any other option from the previous list can be used in the optional
% argument.
%
% \medskip
%
% By default, loading the package \texttt{thmbox} replaces the definition of
% the \LaTeX\ command |\newtheorem|. This feature can be turned off by the
% option \texttt{nothm}. The new version has the same syntax as the standard
% one with an extra optional argument at the beginning. This argument can be
% used to specify formatting options for the optional argument of the
% \texttt{thmbox} environment. For instance, saying
% \begin{quote}
%   \ttfamily \string\newtheorem[L]\{thm\}\{Theorem\}[section]
% \end{quote}
% will produce the following aspect for the environment \texttt{thm}:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{thm\}\\
%   \strut~~Any continuous function over
%   \strut~~\$\string\mathbf\{R\}\$ is measurable.\\
%   \string\end\{thm\}}
% \columnbreak
%   \begin{thm}
%     Any continuous function over $\mathbf{R}$ is measurable.
%   \end{thm}
% \end{multicols}
% \noindent And with a title, with the defaut style, we get:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{thm\}[G\string\"odel]\ \string\label\{tg\}\%\\
%   \strut~~Any theory that contains first\\
%   \strut~~order arithmetics is undecidable.\\
%   \string\end\{thm\}}
% \columnbreak
%   \begin{thm}[Gödel]
%     \label{tg}Any theory that contains first order arithmetics is
%     undecidable.
%   \end{thm}
% \end{multicols}
% \noindent
% The default style for theorem boxes is ``|M|'', so writing
% \begin{quote}
%   \ttfamily \string\newtheorem\{cor\}[thm]\{Corollary\}
% \end{quote}
% will lead to this:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{cor\}\\
%   \strut~~Second order arithmetics\\
%   \strut~~is undecidable.\\
%   \string\end\{cor\}}
% \columnbreak
%   \begin{cor}
%     Second order arithmetics is undecidable.
%   \end{cor}
% \end{multicols}
% The package also redefines the |proof| environment. The text inside such an
% enivronment is written smaller, with extra margins, with a black square sign
% at the end. The aspect is the following:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{proof\}\\
%   \strut~~This is a consequence of the\\
%   \strut~~inclusion of PA into AF2,\\
%   \strut~~since Peano arithmetics is
%   \strut~~undecidable.\\
%   \string\end\{proof\}}
% \columnbreak
%   \begin{proof}
%     This is a consequence of the inclusion of PA into AF2,
%     since Peano arithmetics is undecidable.
%   \end{proof}
% \end{multicols}
% \noindent The |proof| environment takes an optional argument:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{proof\}[of \string\ref\{tg\}]\\
%   \strut~~This is a rather technical\\
%   \strut~~story of encodings.\\
%   \string\end\{proof\}}
% \columnbreak
%   \begin{proof}[of \ref{tg}]
%     This is a rather technical story of encodings.
%   \end{proof}
% \end{multicols}
% \noindent The |example| environment has mostly the same behaviour as
% |proof|:
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{example\}\\
%   \strut~~An approach consists in\\
%   \strut~~encoding Turing machines.\\
%   \string\end\{example\}}
% \par\columnbreak
%   \noindent Bla bla bla.
%   \begin{example}
%     An approach consists in encoding Turing machines.
%   \end{example}
%   Ploum plam.
% \end{multicols}
% \noindent Its optional argument can be used to write something else than
% ``Example''. The alternative method is to redefine |\examplename|.
% \begin{multicols}{2}
%   {\ttfamily\noindent%
%   \string\begin\{example\}[Idea]\\
%   \strut~~One could also proceed by\\
%   \strut~~encoding \$\string\lambda\$-calculus.\\
%   \string\end\{example\}}
% \columnbreak
%   \begin{example}[Idea]
%     One could also proceed by encoding $\lambda$-calculus.
%   \end{example}
% \end{multicols}
%
% \begin{leftbar}
%   As an extra, on the model of the |thmbox| environment, the package
%   provides an environment |leftbar| that formats its contents with an extra
%   margin and a running vertical rule in the left.
% \end{leftbar}
%
% \subsection{Options}
% \label{sec:options}
%
% The following general options are available:
% \begin{description}
% \item[\kv{style}{letter}]
%   indicates which style should be used when drawing the boxes. The letter
%   may be one of the following:
%   \begin{description}
%   \item[\tt S]: a vertical bar on the left of the text
%   \item[\tt M]: a bar on the left and a short horizontal bar at the
%     bottom of the text
%   \item[\tt L]: a vertical bar on each side and a horizontal bar at the
%     bottom
%   \end{description}
%   The default value for this parameter is |M|. The options |S|, |M|, |L| are
%   shortcuts for |style=S|, |style=M| and |style=L|.
% \item[\kv{cut}{bool}]
%   indicates if boxes may be cut at page breaks (true by default),
%   \texttt{nocut} is equivalent to \texttt{cut=false}
% \end{description}
% The following options are used to change style of the header and contents:
% \begin{description}
% \item[\kv{underline}{bool}]
%   indicates if the title of boxes should be underlined (true by default),
%   \texttt{nounderline} is equivalent to \texttt{underline=false}
% \item[\kv{headstyle}{text}]
%   defines how the header of the theorems is formatted. In the text, |#1|
%   represents the environment name (i.e. ``Theorem'') and |#2| represent the
%   number. The default value is ``|\bfseries\boldmath#1 #2|''.
% \item[\kv{titlestyle}{text}]
%   defines how the optional title of theorems is formatted. In the text,
%   |#1| represents the title. The default value is ``| (\textit{#1})|''.
% \item[\kv{bodystyle}{text}]
%   defines how the text of theorems and similar environments is formatted, it
%   is inserted before the text in those environments. The default value is
%   ``|\slshape\noindent|''.
% \end{description}
% The following options define the various spacings:
% \begin{description}
% \item[\kv{leftmargin}{dim}, \kv{rightmargin}{dim}]
%   defines the horizontal space between the margin of the surrounding text
%   and that of the text inside the box (defaut value is |\parskip| for the
%   left margin and 0pt for the right margin)
% \item[\kv{hskip}{dim}, \kv{vskip}{dim}]
%   defines the horizontal and vertical space between the frame of the box
%   and its contents (default value is~0.4em for both)
% \item[\kv{thickness}{dim}]
%   defines the thickness of the bars (defaut value is~0.6pt)
% \end{description}
%
% \subsection{Change log}
%
% \begin{changelog}
% \version{2.0}{2005/04/24}
%   \item first CTAN release
%   \item replaced |preskip| and |postskip| by |left|/|rightmargin| and
%     |h|/|vskip|
%   \item new option |headstyle|
% \version{1.91}{2004/06/08}
%   \item translated everything to english
%   \item cleaned interface, switched to |keyval|, added parameters
% \version{1.3}{2002/09/09}
%   \item added option |nothm|
% \version{1.2}{2002/05/24}
%   \item added option |nocut|
% \version{1.1}{2002/03/08}
%   \item proper \LaTeX\ interface with |\newtheorem|
% \version{1.0}{around 2001}
%   \item first version as an independent package
% \end{changelog}
%
%
% \pagebreak
% \section{Implementation}
%
% First of all, we have to repect the protocol of \LaTeXe\ packages, by
% checking the format and introducing ourselves:
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{thmbox}[2005/04/24 v2.0 fancy theorem outlining]
%    \end{macrocode}
%
% \subsection{Formatting options}
%
% \begin{macro}{\thmboxoptions}
% Parameters are set using the \texttt{keyval} mechanism. All options are in
% the set named \texttt{thmbox}. The macro |\thmboxoptions| can be used to
% change the default parameters at any time.
%    \begin{macrocode}
\RequirePackage{keyval}
\newcommand\thmboxoptions{\setkeys{thmbox}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@style}
% The style of the current box is stored by a |\let| in the macro
% |\thmbox@style|, it can be changed using the \texttt{style} option.
%    \begin{macrocode}
\let\thmbox@style=M
\define@key{thmbox}{style}{\let\thmbox@style=#1}
%    \end{macrocode}
% For compatibility with previous versions, we provide shortcuts for each
% style:
%    \begin{macrocode}
\define@key{thmbox}{S}[]{\let\thmbox@style=S}
\define@key{thmbox}{M}[]{\let\thmbox@style=M}
\define@key{thmbox}{L}[]{\let\thmbox@style=L}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@leftmargin}
% \begin{macro}{\thmbox@rightmargin}
% \begin{macro}{\thmbox@hskip}
% \begin{macro}{\thmbox@vskip}
% \begin{macro}{\thmbox@thickness}
% Next we have presentation parameters. The variables |\thmbox@leftmargin| and
% |\thmbox@rightmargin| are the extra margins to put between the surrounding
% text and the text of the box. The thickness of the frame is defined by
% |\thmbox@thickness|, its spacing with the contents is |\thmbox@hskip|
% horizontally and |\thmbox@vskip| vertically.
%    \begin{macrocode}
\newdimen\thmbox@leftmargin  \thmbox@leftmargin=\parindent
\newdimen\thmbox@rightmargin \thmbox@rightmargin=0pt
\newdimen\thmbox@hskip       \thmbox@hskip=.4em
\newdimen\thmbox@vskip       \thmbox@vskip=.4em
\newdimen\thmbox@thickness   \thmbox@thickness=.6pt
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% These parameters can be set using the keyval interface:
%    \begin{macrocode}
\define@key{thmbox}{leftmargin}{\thmbox@leftmargin=#1\relax}
\define@key{thmbox}{rightmargin}{\thmbox@rightmargin=#1\relax}
\define@key{thmbox}{hskip}{\thmbox@hskip=#1\relax}
\define@key{thmbox}{vskip}{\thmbox@vskip=#1\relax}
\define@key{thmbox}{thickness}{\thmbox@thickness=#1\relax}
%    \end{macrocode}
%
% \begin{macro}{\ifthmbox@cut}
% The boolean |\ifthmbox@cut| indicates whether the boxes may be cut on page
% breaks or if they must be kept in one block. By default, cutting boxes is
% allowed.
%    \begin{macrocode}
\newif\ifthmbox@cut
\thmbox@cuttrue
%    \end{macrocode}
% This can be changed using the \texttt{cut} option, the option \texttt{nocut}
% is equivalent to \texttt{cut=false}.
%    \begin{macrocode}
\define@key{thmbox}{cut}[true]{%
  \expandafter\let\expandafter\ifthmbox@cut\csname if#1\endcsname}
\define@key{thmbox}{nocut}[]{\thmbox@cutfalse}
\DeclareOption{cut}{\thmbox@cuttrue}
\DeclareOption{nocut}{\thmbox@cutfalse}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ifthmbox@underline}
% The boolean |\ifthmbox@underline| indicates if the title of boxes should be
% underlined. It is activated by default and can be changed using the option
% \texttt{underline}.
%    \begin{macrocode}
\newif\ifthmbox@underline
\thmbox@underlinetrue
\define@key{thmbox}{underline}[true]{%
  \expandafter\let\expandafter\ifthmbox@underline\csname if#1\endcsname}
\define@key{thmbox}{nounderline}[]{\thmbox@underlinefalse}
\DeclareOption{underline}{\thmbox@underlinetrue}
\DeclareOption{nounderline}{\thmbox@underlinefalse}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@headstyle}
% The macro |\thmbox@headstyle| defines how the header of theorems is
% formatted, the arguments are the kind of enviromnent (i.e. ``theorem'') and
% its number.
%    \begin{macrocode}
\newcommand\thmbox@headstyle[2]{\bfseries\boldmath#1 #2}
%    \end{macrocode}
% This macro can be redefined using the option \texttt{headstyle}:
%    \begin{macrocode}
\define@key{thmbox}{headstyle}{\def\thmbox@headstyle##1##2{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@titlestyle}
% The macro |\thmbox@titlestyle| defines how the title of theorems is
% formatted, after the theorem number. Its argument is the text to be
% formatted.
%    \begin{macrocode}
\newcommand\thmbox@titlestyle[1]{ (\textit{#1\/})}
%    \end{macrocode}
% This macro can be redefined using the option \texttt{titlestyle}:
%    \begin{macrocode}
\define@key{thmbox}{titlestyle}{\def\thmbox@titlestyle##1{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@bodystyle}
% The macro |\thmbox@bodystyle| defines formatting options for the body of
% theorem-like environments. It is inserted before the text.
%    \begin{macrocode}
\newcommand\thmbox@bodystyle{\slshape\noindent}
%    \end{macrocode}
% This macro can be redefined using the option \texttt{bodystyle}:
%    \begin{macrocode}
\define@key{thmbox}{bodystyle}{\def\thmbox@bodystyle{#1}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Package options}
%
% The only package option is \texttt{nothm}, which prevents from redefining
% the standard |\newtheorem| command.
%    \begin{macrocode}
\newif\ifthmbox@newtheorem
\thmbox@newtheoremtrue
\DeclareOption{nothm}{\thmbox@newtheoremfalse}
%    \end{macrocode}
% All other package options are considered to be formatting options and are
% parsed using the \texttt{keyval} package.
%    \begin{macrocode}
\DeclareOption*{\expandafter\thmboxoptions\expandafter{\CurrentOption}}
\ProcessOptions\relax
%    \end{macrocode}
%
% \subsection{Formatting}
%
% We now define the code for formatting boxes.
% When a box is to be cut, the idea is the following: we assume the box
% |\thmbox@box| to contain the whole text, with an aribtrary height. First we
% compare the height of the box to the available space on the current page. If
% there is enough space, we simply place the box at once with the decorations
% around it. Otherwise, we cut the box at the available height, place the
% first part on the page, and proceed with the remaining text. The following
% steps are similar except that the available space is the height of the whole
% page.
%
% The drawback with this method, apart from the fact that it cannot produce
% page breaks as good as \TeX's page builder does, is that the construction
% cannot be nested. Putting a box of that kind into another works as long as
% there is no page break, but has unpredictable results when a page break
% occurs. If we wanted to allow nested boxes, we would have to completely
% rethink the system, and it would result in a much heavier code. Is this
% really necessary?
%
% \begin{macro}{\thmbox@box}
% \begin{macro}{\thmbox@box@}
% \begin{macro}{\thmbox@dim}
% We first introduce internal variables. We will use two boxes and a length
% register for computations during formatting.
%    \begin{macrocode}
\newbox\thmbox@box
\newbox\thmbox@box@
\newdimen\thmbox@dim
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\thmbox@put}
% Placing a box after appropriate cutting is performed by the macro
% |\thmbox@put|. Its argument is the register that holds the box to be placed.
% The effect is simply to insert the appropriate spaces and rules, according
% to the value of |\thmbox@style|, and switch back to vertical mode.
%    \begin{macrocode}
\def\thmbox@put#1{
  \vskip\z@%
  \noindent%
  \hbox{%
    {\dimen0=\thmbox@leftmargin%
     \advance\dimen0-\thmbox@hskip%
     \advance\dimen0-\thmbox@thickness%
     \hskip\dimen0}%
    \vrule width \thmbox@thickness%
    \hskip\thmbox@hskip%
    \box#1%
    \ifx\thmbox@style L%
      \hskip\thmbox@hskip%
      \vrule width \thmbox@thickness%
    \fi}%
  \par\nobreak}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@start}
% The first step as described above is always applied to the box
% |\thmbox@box|, and |\thmbox@box@| is reserved for the part that is cut out
% by a |\vsplit|. The macro |\thmbox@start| performs the first step in the
% process of formatting a complete box. A difficult point occurs here: if the
% box is to be inserted at the beginning of a page, it may happen that
% |\output| has not been called yet, so the height of the current vertical
% material (the value of |\pagetotal|) may exceed the page height (contained
% in |\vsize|). In this case, the available space on the page must be computed
% as $2\times|\vsize|-|\pagetotal|$ instead of $|\vsize|-|\pagetotal|$. This
% explains the conditional at the beginning of the macro.
%    \begin{macrocode}
\def\thmbox@start{%
  \ifthmbox@cut%
    \ifdim\pagetotal>\vsize%
      \thmbox@dim=2\vsize%
    \else%
      \thmbox@dim=\vsize%
    \fi%
    \advance\thmbox@dim -\pagetotal%
    \ifdim\thmbox@dim>\ht\thmbox@box%
      \thmbox@put\thmbox@box%
    \else%
      \setbox\thmbox@box@=\vsplit\thmbox@box to \thmbox@dim%
      \thmbox@put\thmbox@box@%
      \thmbox@page%
    \fi%
  \else%
    \thmbox@put\thmbox@box%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@page}
% The following steps are for the case when a box is placed at the beginning
% of an empty page. They are handled by the macro |\thmbox@page|, which
% systematically inserts a page break before the box (unless the box is empty,
% which may happen after cutting near the end of a box).
%    \begin{macrocode}
\def\thmbox@page{%
  \ifvoid\thmbox@box%
  \else%
    \eject%
    \ifdim\vsize<\ht\thmbox@box%
      \setbox\thmbox@box@=\vsplit\thmbox@box to \vsize%
      \thmbox@put\thmbox@box@%
      \thmbox@page%
    \else%
      \thmbox@put\thmbox@box%
    \fi%
  \fi}
%    \end{macrocode}
% \end{macro}
%
% After the formatting of text boxes, we have to handle the header of the box
% as well as the part below the last box, where there may be a horizontal
% rule.
%
% \begin{macro}{\thmbox@head}
% The macro |\thmbox@head| produces a header with the text in its argument. A
% horizontal rule is possibly placed below, after a space of
% |\thmbox@vskip|. This space is produced by adding an invisible vertical
% rule in the title so that the size of the space does not depend on the text.
% Finally we add an invisible horizontal rule to switch back to vertical mode
% without making a new paragraph.
%    \begin{macrocode}
\def\thmbox@head#1{%
  \par\noindent\vbox{%
    \setbox\thmbox@box@=\hbox{%
      \vrule width 0mm height 0mm depth \thmbox@vskip%
      #1}%
    \copy\thmbox@box@%
    \ifthmbox@underline%
      \hrule width \wd\thmbox@box@ height \thmbox@thickness%
    \fi}%
  \hrule height 0mm\relax}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\thmbox@tail}
% The terminal par of the box is produced by the macro |\thmbox@tail|. As
% opposed to the previous one, this one is very dependent on the style of the
% box. However we assume is is called only with a style equal to |M| or |L|,
% and not for the style |S|.
%    \begin{macrocode}
\def\thmbox@tail{%
  \hrule height 0mm%
  \ifx\thmbox@style M%
    \thmbox@dim=1cm%
  \else\ifx\thmbox@style L%
    \thmbox@dim=\hsize%
    \advance\thmbox@dim-\thmbox@leftmargin%
    \advance\thmbox@dim-\thmbox@rightmargin%
    \advance\thmbox@dim2\thmbox@hskip%
    \advance\thmbox@dim2\thmbox@thickness%
  \fi\fi%
  \noindent%
  {\dimen0=\thmbox@leftmargin%
   \advance\dimen0-\thmbox@hskip%
   \advance\dimen0-\thmbox@thickness%
   \hskip\dimen0}%
  \vrule width \thmbox@dim height \thmbox@thickness%
  \par}
%    \end{macrocode}
% \end{macro}
%
%
% \subsection{\LaTeX\ interface}
%
% \subsubsection{Environnements}
%
% \begin{macro}{thmbox}
% Now that the previous commands are defined, we can actually define the
% \texttt{thmbox} environment. The prefix part is used to set the formatting
% options and produce the header, then start a vertical box with appropriate
% width for the contents of the text. The postfix part closes this box,
% formats it using the previously defined commands, and possibly inserts
% material at the end.
%
% The code contains groupings that may appear unnecessary, around |#2| in the
% header and around the environment's contents. They are here to obtain a
% satisfactory behaviour when using colours.
%    \begin{macrocode}
\newenvironment{thmbox}[2][]{%
  \parskip\z@%
  \setkeys{thmbox}{#1}%
  \ifx\thmbox@style S\else\ifx\thmbox@style M\else\ifx\thmbox@style L\else%
    \PackageWarning{thmbox}{\thmbox@style\ is not a valid style for
      \string\thmbox, using M}%
    \let\thmbox@style=M%
  \fi\fi\fi%
  \thmbox@head{{#2}}\nobreak\relax%
  \thmbox@dim=\hsize%
  \advance\thmbox@dim-\leftskip%
  \advance\thmbox@dim-\rightskip%
  \setbox\thmbox@box=\vbox\bgroup%
    \hsize=\thmbox@dim%
    \advance\hsize -\thmbox@leftmargin%
    \advance\hsize -\thmbox@rightmargin%
    \textwidth=\hsize%
    \linewidth=\hsize%
    \vskip\thmbox@vskip%
    \begingroup}{\endgroup%
    \vskip\thmbox@vskip%
  \egroup%
  \thmbox@start%
  \ifx\thmbox@style S\else\thmbox@tail\fi%
  \@endparenv}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{leftbar}
% The definitions used for wrapping boxes allow for easily defining an
% evironment that places a vertical bar on the side of the text. The code of
% this enivonment is basically that of \texttt{thmbox} without the header and
% tail material:
%    \begin{macrocode}
\newenvironment{leftbar}[1][]{%
  \setkeys{thmbox}{#1}%
  \par\vskip\thmbox@vskip%
  \setbox\thmbox@box=\vbox\bgroup%
    \hsize=\textwidth%
    \advance\hsize -\thmbox@leftmargin%
    \advance\hsize -\thmbox@rightmargin%
    \begingroup}{\endgroup%
    \vskip\thmbox@vskip%
  \egroup%
  \thmbox@start%
  \vskip\thmbox@vskip\par}
%    \end{macrocode}
% \end{macro}
%
% \subsubsection{\LaTeX\ theorems}
%
% \begin{macro}{\newboxtheorem}
% In order to use this package transparently in a \LaTeX\ text, we define the
% command |\newboxtheorem| on the model of the standard |\newtheorem| command.
% The code of this version respects the semantics of the original one, with
% two possible syntaxes:
% \begin{itemize}
% \item |\newtheorem{|\textit{environment}|}[|\ignorespaces%
%   \textit{counter}|]{|\textit{title}|}|
%   to use an already existing counter,
% \item |\newtheorem{|\textit{environment}|}{|\ignorespaces%
%   \textit{title}|}[|\textit{coupter}|]|
%   to create a new counter that possibly depends on an existsing one.
% \end{itemize}
% We add an optional first argument that contains formatting options for the
% boxes used in a particular kind of theorem. The definition of this macro is
% in a very \LaTeX ish style\dots
%    \begin{macrocode}
\def\newboxtheorem{%
  \@ifnextchar[{\thmbox@newA}{\thmbox@newA[]}}%
%    \end{macrocode}
% The sub-macros eventually call |\thmbox@new|, which performs the actual
% theorem definition according to four arguments, respectively the formatting
% options, the environment name, the title used and the name of \LaTeX\
% counter.
% \end{macro}
%
% The macro |\thmbox@newA| is used when options are specified. We store the
% options in the macro |\thmbox@temp| for stability. This is needed because it
% may contain an option of the form |titlestyle={...#1...}| which could cause
% problems in a |\def|.
%    \begin{macrocode}
\def\thmbox@newA[#1]#2{%
  \def\thmbox@temp##1{#1}%
  \@ifnextchar[{\thmbox@newC{#2}}{\thmbox@newD{#2}}}
%    \end{macrocode}
% The macro |\thmbox@newC| corresponds to the case where an existing counter
% is used for the new environment.
%    \begin{macrocode}
\def\thmbox@newC#1[#2]#3{%
  \expandafter\thmbox@new\expandafter{\thmbox@temp{####1}}{#1}{#3}{#2}}
%    \end{macrocode}
% The macro |\thmbox@newD| corresponds to the case where a new counter is
% created.
%    \begin{macrocode}
\def\thmbox@newD#1#2{%
  \@ifnextchar[{\thmbox@newE{#1}{#2}}{%
    \newcounter{#1}%
    \expandafter\thmbox@new\expandafter{\thmbox@temp{####1}}{#1}{#2}{#1}}}
%    \end{macrocode}
% The macro |\thmbox@newE| corresponds to the case where the new counter
% depends on another counter.
%    \begin{macrocode}
\def\thmbox@newE#1#2[#3]{%
  \newcounter{#1}[#3]%
  \expandafter\def\csname the#1\endcsname{%
    \csname the#3\endcsname.\arabic{#1}}
  \expandafter\thmbox@new\expandafter{\thmbox@temp{####1}}{#1}{#2}{#1}}
%    \end{macrocode}
%
% The macro |\thmbox@new| performs the actual environment definition.
% Because of the way we handle the environment's optional argument, we
% don't define the new environment using \LaTeX\ macros. The drawback is that
% there is no error checking.
%    \begin{macrocode}
\def\thmbox@new#1#2#3#4{%
  \expandafter\def\csname#2\endcsname{%
    \setkeys{thmbox}{#1}%
    \@ifnextchar[{\thmbox@beginA{#3}{#4}}{%
      \thmbox@begin{#3}{#4}{}}}%
  \expandafter\def\csname end#2\endcsname{%
    \endthmbox\smallbreak}}
%    \end{macrocode}
% Like previously, the macro |\thmbox@beginA| is used when the optional
% argument is present.
%    \begin{macrocode}
\def\thmbox@beginA#1#2[#3]{%
  \thmbox@begin{#1}{#2}{\thmbox@titlestyle{#3}}}
%    \end{macrocode}
%
% The macro |\thmbox@begin| is responsible for opening a theorem environment
% as defined by the new version of |\newtheorem|. Its arguments contain
% respectively options for the box, the name of the element (e.g.
% ``Theorem''), the counter used and the text to insert after the number.
%    \begin{macrocode}
\def\thmbox@begin#1#2#3{%
  \medbreak%
  \refstepcounter{#2}%
  \thmbox{\thmbox@headstyle{#1}{\csname the#2\endcsname}#3}%
  \thmbox@bodystyle\ignorespaces}
%    \end{macrocode}
%
% Optionally (see the package option \texttt{nothm}), this macro replaces the
% original |\newtheorem| so that the package can be used without modifying an
% already prepared text.
%    \begin{macrocode}
\ifthmbox@newtheorem
\let\newtheorem\newboxtheorem
\fi
%    \end{macrocode}
%
%
% \subsection{Examples and proofs}
%
% \begin{macro}{example}
% For examples, we write the text in a smaller face, with an extra left margin
% and a mark at the beginning.
%    \begin{macrocode}
\def\example{}
\@ifundefined{examplename}{\def\examplename{Example}}{}
\renewenvironment{example}[1][\examplename]{%
  \par\smallbreak\small%
  \list{\hspace\labelsep\textbf{#1\,:}}{%
    \leftmargin=\parindent%
    \labelwidth=\parindent}%
  \item\relax}{%
  \endlist}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{proof}
% The format of proofs is mostly that of examples, with two differences: the
% right margin is also extended, and a black square is placed in this extra
% margin at the end of the last line.
%    \begin{macrocode}
\def\proof{}
\@ifundefined{proofname}{\def\proofname{Proof}}{}
\renewenvironment{proof}[1][]{%
  \small%
  \list{\hspace\labelsep\textbf{\proofname\ #1\unskip\,:}}{%
    \topsep=\smallskipamount%
    \partopsep=0pt%
    \leftmargin=\parindent%
    \rightmargin=\parindent%
    \listparindent=\parindent%
    \labelwidth=\parindent}%
  \item\relax\ignorespaces}%
%    \end{macrocode}
%    \begin{macrocode}
 {\parskip\z@%
  \par\noindent%
  \setbox\thmbox@box=\hbox{%
    \kern .5em\vbox{%
      \hrule width .7em height .7em
      \vskip\baselineskip}}%
  \wd\thmbox@box=0mm%
  \ht\thmbox@box=0mm%
  \hfill\box\thmbox@box%
  \endlist\par}
%    \end{macrocode}
% \end{macro}
%
%
% \Finale
