\usepackage{amstext}
\usepackage{ifthen}


%**********************************************************************
%*** Lengths (used throughout)
%**********************************************************************
\newlength{\ldBaseLineSkip}
\newlength{\ldDescHeight}
\newlength{\ldDescSpace}


%**********************************************************************
%*** LogicalDivision (no descriptions
%**********************************************************************

\newenvironment{LogicalDivision}
    { $ \left\{ \begin{array}{l} }
    { \end{array} \right. $ }

\newcommand{\DivEntry}[1]
    { \textit{#1} \\ }

\newcommand{\DivEntrySimpleDesc}[2]
    { \textit{#1} & \text{#2} \\ }


%**********************************************************************
%*** LogicalDivisionDesc  (single description)
%**********************************************************************
% description may be multiple lines

\newenvironment{LogicalDivisionDesc}
    { \setlength{\ldBaseLineSkip}{\baselineskip} $ \left\{ \begin{array}{ll} }
    { \end{array} \right. $ }


\newcommand{\DivEntryDesc}[2]
    { 
    \textit{#1} & \hspace{-0.5em} 
    \settoheight{\ldDescHeight}{\begin{tabular}{l}#2\end{tabular}}
    \text{
    \begin{tabular}{l}
        % add a little space before, if it's a big description
        \ifthenelse
            { \lengthtest{\ldDescHeight > \ldBaseLineSkip} }     % condition
            {\vspace{-.75\ldBaseLineSkip}\\ }                    % then
            {\settowidth{\ldDescSpace}{ }\hspace{-\ldDescSpace}} % else
    #2
        % add a little space before, if it's a big description
        \ifthenelse
            { \lengthtest{\ldDescHeight > \ldBaseLineSkip} }     % condition
            {\\ \ \vspace{-.75\ldBaseLineSkip}}                  % then
            {} %{ ldDescHeight=\the\ldDescHeight }               % else
    \end{tabular}} 
    \\ 
    }

%{\text{\the\ldDescHeight $>$ \the\ldBaseLineSkip}} % then
%{\text{\the\ldDescHeight}} % else



%**********************************************************************
%*** LogicalDivisionDoubleDesc  (double description)
%**********************************************************************
% first description must be a single line ... could be relaxed, not today
% second description may be multiple lines

\newenvironment{LogicalDivisionDoubleDesc}
    { \setlength{\ldBaseLineSkip}{\baselineskip} $ \left\{ \begin{array}{lll} }
    { \end{array} \right. $ }


\newcommand{\DivEntryDoubleDesc}[3]
    { 
    \textit{#1} & \text{#2} & \hspace{-0.5em} 
    \settoheight{\ldDescHeight}{\begin{tabular}{l}#3\end{tabular}}
    \text{
    \begin{tabular}{l}
        % add a little space before, if it's a big description
        \ifthenelse
            { \lengthtest{\ldDescHeight > \ldBaseLineSkip} }     % condition
            {\vspace{-.75\ldBaseLineSkip}\\ }                    % then
            {\settowidth{\ldDescSpace}{ }\hspace{-\ldDescSpace}} % else
    #3
        % add a little space before, if it's a big description
        \ifthenelse
            { \lengthtest{\ldDescHeight > \ldBaseLineSkip} }     % condition
            {\\ \ \vspace{-.75\ldBaseLineSkip}}                  % then
            {} %{ ldDescHeight=\the\ldDescHeight }               % else
    \end{tabular}} 
    \\ 
    }




%**********************************************************************
%*** Euphemisms
%**********************************************************************

%----------------------------------------------------------------------
\newcommand{\twodiv}[2]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\twodivdesc}[4]{
    \setlength{\ldBaseLineSkip}{\baselineskip}
    $ \left\{
        \begin{array}{ll}
        \DivEntryDesc{#1}{#2} 
        \DivEntryDesc{#3}{#4}
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\twodivsimpledesc}[4]{
    \setlength{\ldBaseLineSkip}{\baselineskip}
    $ \left\{
        \begin{array}{ll}
        \DivEntrySimpleDesc{#1}{#2} 
        \DivEntrySimpleDesc{#3}{#4}
        \end{array}
    \right. $
}


%----------------------------------------------------------------------
% OLD DEFINITION
%\newcommand{\twodivdesc}[4]{
%    \twodiv{#1}{#3}
%    \hspace{-0.2in}
%    \begin{tabular}{l} #2 \\ #4 \end{tabular}
%}


%----------------------------------------------------------------------
\newcommand{\threediv}[3]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \end{array}
    \right. $
}


%----------------------------------------------------------------------
\newcommand{\threedivsimpledesc}[6]{
    $ \left\{
        \begin{array}{ll}
        \DivEntrySimpleDesc{#1}{#2} 
        \DivEntrySimpleDesc{#3}{#4} 
        \DivEntrySimpleDesc{#5}{#6} 
        \end{array}
    \right. $
}


%----------------------------------------------------------------------
\newcommand{\threedivdesc}[6]{
    \setlength{\ldBaseLineSkip}{\baselineskip}
    $ \left\{
        \begin{array}{ll}
        \DivEntryDesc{#1}{#2}
        \DivEntryDesc{#3}{#4}
        \DivEntryDesc{#5}{#6}
        \end{array}
    \right. $
}


%----------------------------------------------------------------------
\newcommand{\fourdiv}[4]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \DivEntry{#4} 
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\fourdivdesc}[8]{
    \setlength{\ldBaseLineSkip}{\baselineskip}
    $ \left\{
        \begin{array}{ll}
        \DivEntryDesc{#1}{#2}
        \DivEntryDesc{#3}{#4}
        \DivEntryDesc{#5}{#6}
        \DivEntryDesc{#7}{#8}
        \end{array}
    \right. $
}



%----------------------------------------------------------------------
\newcommand{\fivediv}[5]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \DivEntry{#4} 
        \DivEntry{#5} 
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
%% Can only have 9 parameters ... the last one doesn't get a description
\newcommand{\fivedivdesc}[9]{
    \setlength{\ldBaseLineSkip}{\baselineskip}
    $ \left\{
        \begin{array}{ll}
        \DivEntryDesc{#1}{#2}
        \DivEntryDesc{#3}{#4}
        \DivEntryDesc{#5}{#6}
        \DivEntryDesc{#7}{#8}
        \DivEntryDesc{#9}{}
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\sixdiv}[6]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \DivEntry{#4} 
        \DivEntry{#5} 
        \DivEntry{#6} 
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\sevendiv}[7]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \DivEntry{#4} 
        \DivEntry{#5} 
        \DivEntry{#6} 
        \DivEntry{#7} 
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\eightdiv}[8]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \DivEntry{#4} 
        \DivEntry{#5} 
        \DivEntry{#6} 
        \DivEntry{#7} 
        \DivEntry{#8} 
        \end{array}
    \right. $
}

%----------------------------------------------------------------------
\newcommand{\ninediv}[9]{
    $ \left\{
        \begin{array}{l}
        \DivEntry{#1} 
        \DivEntry{#2} 
        \DivEntry{#3} 
        \DivEntry{#4} 
        \DivEntry{#5} 
        \DivEntry{#6} 
        \DivEntry{#7} 
        \DivEntry{#8} 
        \DivEntry{#9} 
        \end{array}
    \right. $
}



%**********************************************************************
%*** Really old stuff ...
%**********************************************************************

\newcommand{\twotwodiv}[6]{
    $ \left\{
        \begin{array}{l}
        \text{#1} 
            \left\{
            \begin{array}{l}
            \textit{#2} \\
            \textit{#3} \\
            \end{array}
            \right.
            \\
        \hspace{0.1in} \\
        \text{#4} 
            \left\{
            \begin{array}{l}
            \textit{#5} \\
            \textit{#6} \\
            \end{array}
            \right.
            \\
        \end{array}
    \right. $
}


%----------------------------------------------------------------------
\newcommand{\twotwodivX}[6]{
    $ \left\{
        \begin{array}{l}
        \text{#1} 
            \left\{
            \begin{array}{l}
            \textit{#2} \\
            \textit{#3} \\
            \end{array}
            \right.
            \\
        \hspace{0.1in} \\
        \text{#4} 
            \left.
            \begin{array}{l}
            \textit{#5} \\
            \textit{#6} \\
            \end{array}
            \right.
            \\
        \end{array}
    \right. $
}


