% The following IF is for counters, etc, that differ in Report and
% Article styles:
\newif\ifartstyle \artstyletrue
\newif\ifrepstyle \repstylefalse

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The ENUMSENTENCE macro:
%
% \enumsentence{This is an example}
% produces
%  (1) This is an example
% while
% \enumsentence[(a)]{This is another example}
% produces
%  (a) This is another example
% 
% A related macro is
% \eenumsentence{\item[a.] A third example
%                \item[b.] A fourth example}
% which produces
%  (2) a. A third example
%      b. A fourth example
% 
% another macro is \exno{1} which produces the number of the following
% enumsentence (\exno{0} produces number of preceeding enumsentence and
% so on.)
% 
% The standard \label command also works within \enumsentence and
% \eenumsentence. 

% the \enumsentence counter.
\ifrepstyle \newcounter{enums}[chapter] \else \newcounter{enums} \fi
\newskip\enumsepdefault  \enumsepdefault=.30em% for 2-digit labels (XX)
\newskip\enumsep \enumsep=\enumsepdefault     % advance .4em per digit

\def\enumsentence{\@ifnextchar[{\@enumsentence}% %]
{\refstepcounter{enums}\@enumsentence[(\theenums)]}}

\long\def\@enumsentence[#1]#2{\begin{list}{}{
		\advance\leftmargin by\enumsep%
		\advance\labelwidth by\enumsep%
 		\advance\topsep by-.3ex}%{\makelabel{#1\hfil}}
 \item[#1] #2
 \end{list}}

% The \exno definition
\newcounter{tempcnt}\setcounter{tempcnt}{0}
\newcommand{\exno}[1]{\setcounter{tempcnt}{\value{enums}}%
\addtocounter{tempcnt}{#1}%
\arabic{tempcnt}}

% modified \@item command \unhbox\@tempboxa replaces \makelabel{#1}
% Used to get \eenumsentence to work correctly
\def\@item[#1]{\if@noparitem \@donoparitem
  \else \if@inlabel \indent \par \fi
         \ifhmode \unskip\unskip \par \fi 
         \if@newlist \if@nobreak \@nbitem \else
                        \addpenalty\@beginparpenalty
                        \addvspace\@topsep \addvspace{-\parskip}\fi
           \else \addpenalty\@itempenalty \addvspace\itemsep 
          \fi 
    \global\@inlabeltrue 
\fi
\everypar{\global\@minipagefalse\global\@newlistfalse 
          \if@inlabel\global\@inlabelfalse \hskip -\parindent \box\@labels
             \penalty\z@ \fi
          \everypar{}}\global\@nobreakfalse
\if@noitemarg \@noitemargfalse \if@nmbrlist \refstepcounter{\@listctr}\fi \fi
\setbox\@tempboxa\hbox{\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}

% for enumerated enumsentences. Internal enumeration is alpha if not
% otherwise defined.  
% counter for items within \eenumsentence. (might use enumi instead?)
\newcounter{enumsi}

% To use the standard label mechanism:
%
% \eenumsentence{\item[a.] A third example \label{bar}\toplabel{foo}
%                \item[b.] A fourth example \label{baz}}
% Testing references \ref{foo}, \ref{bar}, and \ref{baz}.
%
% will produce
%
%  (3) a. A third example
%      b. A fourth example
% Testing references 3, 3a, and 3b.
%
% To ensure that labels in \eenumsentence come out right:
\def\theenumsi{\theenums\alph{enumsi}}
\newskip\eenumsep \eenumsep=1.1em %(which is then advanced by \enumsep)

\def\@mklab#1{#1\hfil}
\def\@mklabr#1{\hfil#1}
\def\enummklab#1{\eelabel \hfil\hbox to.85em{\hfil#1}}
\def\enummakelabel#1{\enummklab{#1}\global\let\makelabel=\@mklabr}
\def\toplabel#1{{\edef\@currentlabel{\p@enums\theenums}\label{#1}}}

\def\eenumsentence{\@ifnextchar[{\@eenumsentence}% %]
{\refstepcounter{enums}\@eenumsentence[(\theenums)]}}

\long\def\@eenumsentence[#1]#2{\def\eelabel{#1}\let\holdlabel\makelabel%
\begin{list}{\alph{enumsi}.}{\usecounter{enumsi}%
	\advance\eenumsep by\enumsep%
	\advance\leftmargin by\eenumsep%
	\advance\labelwidth by\eenumsep%
	\advance\topsep by-.3ex%
	\itemsep 0pt%
	\let\makelabel=\enummakelabel}%
#2
\end{list}\let\makelabel\holdlabel}

%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following hack is for pictures and arrays occuring first thing within
% an enumsentence (so that number is aligned with top line of the array)
\newenvironment{topline}{\begin{tabular}[t]{@{}l@{}}\ \\*[-1em]}{\end{tabular}}

% see also \evenup below

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% parse tree macros:
%
% \smalltree{&a\\ b&&c\\ d&e&f}
% produces
%          a
%        b   c
%      d   e   f
%
% \modsmalltree{3}{&a\\ b&&c\\ d&e&f}
% produces the same output
%
% \mc{n}{item}
% within either will produce item centered across n number of columns

\long\def\smalltree#1{\leavevmode{\def\\{\cr\noalign{\vskip12pt}}%
\def\mc##1##2{\multispan{##1}{\hfil##2\hfil}}%
\tabskip=1em%
\hbox{\vtop{\halign{&\hfil##\hfil\cr
#1\crcr}}}}}

\def\modsmalltree{\@ifnextchar[{\@modsmalltree}{\@modsmalltree[2]}} %]

%\long\def\@modsmalltree[#1]#2#3{{\def\mc##1##2{\multicolumn{##1}{c}{##2}}%
%\def\arraystretch{#1}%
%\begin{tabular}[t]{@{}*{#2}{c}@{}}
%#3
%\end{tabular}}}

\long\def\@modsmalltree[#1]#2#3{{\def\mc##1##2{%
\multicolumn{##1}{c}{\def\arraystretch{1}##2}}%
\def\arraystretch{#1}%
\def\ns{\def\arraystretch{1}}%
\setbox0=\hbox{\begin{tabular}[t]{@{}*{#2}{c}@{}}
#3
\end{tabular}}%
\dimen0=\ht0
\advance\dimen0 by -\arraystretch \ht\strutbox
\advance\dimen0 by \ht\strutbox
\ht0=\dimen0
\dimen0=\dp0
\advance\dimen0 by -\arraystretch \dp\strutbox
\advance\dimen0 by \dp\strutbox
\dp0=\dimen0
\leavevmode\box0}}

% center lap creates a box of 0 width centered on the point
\def\clap#1{\hbox to 0pt{\hss#1\hss}}

\def\csigma{\clap{$\sigma$}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Short-example macros
%
% \shortex{4}{a & b & c & d}
%            {z & y & x & w}
%            {mnop}
% produces
%   a  b  c  d
%   z  y  x  w
%   mnop
%
% \shortexdt  takes two translation lines
% \shortexnt takes no translation lines
% Multiple translations can also be handled as follows:
%
% \shortex{4}{a & b & c & d}
%            {z & y & x & w}
%            {mnop \\ qrstu}
% produces
%   a  b  c  d
%   z  y  x  w
%   mnop
%   qrstu

\def\shortex#1#2#3#4{\begin{tabular}[t]{@{}*{#1}{l@{\ \ }}}
#2\\ #3\\
\multicolumn{#1}{@{}l@{}}{\begin{tabular}[t]{@{}l@{}}
#4\end{tabular}} \end{tabular}}

\def\shortexdt#1#2#3#4#5{\begin{tabular}[t]{@{}*{#1}{l@{\ \ }}}
#2\\ #3\\ \multicolumn{#1}{@{}l@{}}{#4}\\
\multicolumn{#1}{@{}l@{}}{#5}
\end{tabular}}

\def\shortexnt#1#2#3{\begin{tabular}[t]{@{}*{#1}{l@{\ \ }}}
#2\\ #3
\end{tabular}}

%%%%%%%%%
% Long-example macros
%
% \longex{4}{2}
%	{a & b & c & d}{e & f}
%       {z & y & x & w}{v & u}
%       {mnop}
% produces
%   a  b  c  d
%   z  y  x  w
%   e  f
%   v  u
%   mnop
%
% \longexdt takes two translation lines
% \longexnt takes no translation lines
% \longex can also handle multiple translations, as can \shortex above.

\def\longex#1#2#3#4#5#6#7{\begin{tabular}[t]{@{}l}
 \begin{tabular}[t]{@{}*{#1}{l@{\ \ }}}
 #3\\ #5 \end{tabular}\vspace{.5ex}\\
 \begin{tabular}[t]{@{}*{#2}{l@{\ \ }}}
 #4\\ #6\\[.5ex] \multicolumn{#2}{@{}l@{}}{\begin{tabular}[t]{@{}l@{}}
 #7 \end{tabular}} \end{tabular} \end{tabular}}

\def\longexdt#1#2#3#4#5#6#7#8{\begin{tabular}[t]{@{}l}
 \begin{tabular}[t]{@{}*{#1}{l@{\ \ }}}
 #3\\ #5 \end{tabular}\vspace{.5ex}\\
 \begin{tabular}[t]{@{}*{#2}{l@{\ \ }}}
 #4\\ #6\\[.5ex] \multicolumn{#2}{@{}l@{}}{#7}\\
 \multicolumn{#2}{@{}l@{}}{#8}
 \end{tabular} \end{tabular}}

\def\longexnt#1#2#3#4#5#6{\begin{tabular}[t]{@{}l}
 \begin{tabular}[t]{@{}*{#1}{l@{\ \ }}}
 #3\\ #5 \end{tabular}\vspace{.5ex}\\
 \begin{tabular}[t]{@{}*{#2}{l@{\ \ }}}
 #4\\ #6 \end{tabular} \end{tabular}}

