%  C A S E S . S T Y             ver 2.    June 1994
%
%  Copyright (C) 1993,1994 by Donald Arseneau
%  These macros may be freely transmitted, reproduced, or modified 
%  provided that this notice is left intact.  Sub-equation numbering
%  is based on subeqn.sty by Stephen Gildea; most of the rest is based
%  on LaTeX's \eqnarray by Leslie Lamport and the LaTeX3 team.
%
%  LaTeX environment {numcases} to produce multi-case equations with
%  a separate equation number for each case.  Also, {subnumcases}
%  numbers each case with the overall equation number plus a letter 
%  [8a, 8b, etc.]   The syntax is
%
%  \begin{numcases}{left_side}
%  case_1 & explanation_1 \\
%  case_2 & explanation_2 \\
%  ...
%  case_n & explanation_n
%  \end{numcases}
%
% 
%  Each case is a math formula, and each explanation is a piece of lr mode
%  text (which may contain math mode in $...$).  The explanations are
%  optional.  Equation numbers are inserted automatically, just as for the
%  eqnarray environment.  In particular, the \nonumber command suppresses
%  an equation number and the \label command allows reference to a
%  particular case.  In a subnumcases environment, a \label in the
%  left_side of the equation gives the overall equation number, without
%  any letter. 
%
%  To use this package, declare "\documentstyle [...cases...]{...}"  or
%  include  "\usepackage{cases}"  after \documentclass.  You may also
%  specify  "\usepackage[subnum]{cases}"  to force *all* numcases 
%  environments to be treated as subnumcases.
%
% - - - - -   
%  A simple example is:
%    \begin{numcases}{|x|=}
%    x, & for $x \geq 0$\\
%    -x, & for $x < 0$
%    \end{numcases}
%
%  Giving:
%                    / x   for x > 0              (1)
%             |x| = <            -
%                    \ -x  for x < 0              (2)
% 
% - - - - -   
% Another example is taking the square root of $c+id$. First compute
% \begin{subnumcases}{\label{w} w\equiv}
%  0    & $c = d = 0$\label{wzero}\\
% \sqrt{|c|}\,\sqrt{\frac{1 + \sqrt{1+(d/c)^2}}{2}}   & $|c| \geq |d|$ \\
% \sqrt{|d|}\,\sqrt{\frac{|c/d| + \sqrt{1+(c/d)^2}}{2}}   & $|c| < |d|$
% \end{subnumcases}
% Then, using $w$ from eq.~(\ref{w}), the square root is
% \begin{subnumcases}{\sqrt{c+id}=}
% 0                    & $w=0$ (case \ref{wzero})\\
% w+i\frac{d}{2w}      & $w \neq 0$, $c \geq 0$ \\
% \frac{|d|}{2w} + iw  & $w \neq 0$, $c < 0$, $d \geq 0$ \\
% \frac{|d|}{2w} - iw  & $w \neq 0$, $c < 0$, $d < 0$ 
% \end{subnumcases}     
%
%  Question: Is there a {numcases*} environment for unnumbered cases?
%  Answer: No. Just use \left\{ \begin{array}...\end{array} \right.
%
%  - - -  begin definitions - - - 

\ProvidesPackage{cases}[1994/06/16 ver 2. ]

\newenvironment{numcases}[1]%
{\displaymath
 \numc@opts
 \setbox\z@\hbox
  {\advance\c@equation\@ne\def\@currentlabel{\p@equation\theequation}% local
  $\displaystyle {#1\null}\m@th\mskip\medmuskip$}%
 \numc@setsub 
 \dimen@ii\displaywidth
 \setbox\tw@\vbox\bgroup
  \stepcounter{equation}\def\@currentlabel{\p@equation\theequation}% 
  \global\@eqnswtrue\m@th
  \everycr{}\tabskip\numc@left\let\\\@eqncr
  \halign to\dimen@ii \bgroup \kern\wd\z@ \kern14\p@ % assume width of brace
    \global\@eqcnt\@ne$\displaystyle\tabskip\z@skip{##}$\hfil 
   &\global\@eqcnt\tw@ \skip@10\p@ \advance\skip@\tw@\arraycolsep \hskip\skip@
    ##\unskip\hfil\tabskip\@centering 
   &\global\@eqcnt\thr@@\hbox to\z@\bgroup\hss##\egroup\tabskip\z@skip\cr
}%  (The \unskip removes space when there are no explanations.)
{\@@eqncr
 \egroup % end \halign, which does not contain first column or brace
 \global\advance\c@equation\m@ne 
 \unskip\unpenalty\unskip\unpenalty
 \setbox\z@\lastbox \nointerlineskip \copy\z@ % grab last line, then put it back
 \g@tboxedwidth\z@ % box \z@ destroyed, width of columns -> \dimen@i
\egroup% end \vbox (\box\tw@)
\hbox to\displaywidth{\m@th % assemble the whole equation
 \hskip\numc@left \hbox to\dimen@i{$\displaystyle \box\z@ % parameter #1
  \dimen@\ht\tw@ \advance\dimen@\dp\tw@ % get size of brace
  \left\{\vcenter to\dimen@{\vfil}\right.\n@space % make brace
  $\hfil}\hskip\@centering % finished first part (filled whole line)
 \kern-\displaywidth $\vcenter{\box\tw@}$% overlay the alignment
}% end the \hbox
\numc@resetsub
\enddisplaymath\global\@ignoretrue}

% Get width of all boxes on a line, ignoring the glue between them.
% Return total width in \dimen@i (global).
\def\g@tboxedwidth#1{\setbox#1\hbox{\unhbox#1\global\dimen@i\z@ 
  \G@tBoxedWidth}}
\def\G@tBoxedWidth{\unskip\unskip\unskip \setbox\z@\lastbox 
 \ifvoid\z@\else \global\advance\dimen@i\wd\z@ \expandafter\G@tBoxedWidth \fi}

\let\numc@setsub\relax
\let\numc@resetsub\relax

\def\subnumcases{\let\numc@setsub\subequations 
   \let\numc@resetsub\endsubequations  \numcases}
\let\endsubnumcases\endnumcases 

% These versions of subequations follow the style of Gildea's subeqn.sty,
% but are thoroughly rewritten.  

% You can change the labelling by redefining \thesubequation, but the names
% of the counters may be confusing: The sub-number is given by counter
% {equation}, while the overall equation number is given by {mainequation}.
% There are two ways to reference the overall equation number:
% through the value of \c@mainequation, as in \Roman{mainequation}, or
% through \themainequation, which gives the text of the normal \theequation.
% Refer to the local sub-number with \c@equation, as in \alph{equation}.
% The default numbering is like 13c, given by:

\def\thesubequation{\themainequation\alph{equation}}

% Some alternatives:
%   \def\thesubequation{\themainequation.\Alph{equation}}  %  13.C
%   \def\thesubequation{\themainequation-\roman{equation}}  %  13-iii
%   \def\thesubequation{(\arabic{mainequation}\alph{equation})} % (13c)
% The third alternative should be used if a document style has
% declared  \renewcommand{\@eqnnum}{\theequation} and
% \renewcommand{\theequation}{(\arabic{equation})}, which makes
% \ref put parentheses around regular equation numbers automatically.

\def\subequations{\refstepcounter{equation}%
  \mathchardef\c@mainequation\c@equation
  \let\@tempa\protect % * added protection
  \def\protect{\noexpand\protect\noexpand}% *
  \edef\themainequation{\theequation}\let\theequation\thesubequation
  \let\protect\@tempa % *
  \global\c@equation\z@}

\def\endsubequations{%
  \global\c@equation\c@mainequation
  \global\@ignoretrue}

% \begin{subeqnarray}  works like  \begin{subequations}\begin{eqnarray},
% with the added feature that a \label command given given at the very
% beginning of the first entry defines a label for the overall equation 
% number.

\def\subeqnarray{\subequations\eqnarray\@ifnextchar\label{\@lab@subeqnarray}{}}
\def\endsubeqnarray{\endeqnarray\endsubequations}
\def\@lab@subeqnarray#1#2{\begingroup 
  \let\@currentlabel\themainequation #1{#2}\endgroup}

\def\numc@left{\@centering}
\let\numc@opts\relax

\DeclareOption{fleqn}{
  \def\numc@left{\z@skip}
  \def\numc@opts{\displaywidth\linewidth \advance\displaywidth-\mathindent} }
\DeclareOption{subnum}{
  \let\numc@setsub\subequations 
  \let\numc@resetsub\endsubequations  }
\ProcessOptions 

% Send problem reports to asnd@Reg.TRIUMF.CA
%
% test integrity:
% brackets:  round, square, curly, angle:   () [] {} <>
% backslash, slash, vertical, at, dollar, and: \ / | @ $ &
% hat, grave, acute (apostrophe), quote, tilde, under:   ^ ` ' " ~ _
