%% Copyright 2008 by Nicolas Vaughan
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN
%% archives in directory macros/latex/base/lppl.txt; either
%% version 1 of the License, or any later version.
%% Author: Nicolas Vaughan
%% e-mail: nivaca@gmail.com
%% status: supported
%% filedate 25/10/2008
%
\NeedsTeXFormat{LaTeX2e}
\def\filedate{25/10/2008}
\def\fileversion{v1.2}
\typeout{Package syllogism  \fileversion\space<\filedate>.}
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{syllogism}[\filedate\space\fileversion]
\RequirePackage{calc}
\RequirePackage{amssymb}
\RequirePackage{ifthen}
\RequirePackage{xspace}
%
\newlength{\SY@LenPA} % Length of longest premiss
\newlength{\SY@LenPB} % Length of shortest premiss
\newlength{\SY@LenC} % Length of the conclusion
\newlength{\SY@LenLab} % Length of the label
\newlength{\SY@LenRule} % Length of rule
\newlength{\SY@HiRule} % `Height' of rule
%
\newlength{\SY@LenAdjRule} % Adjust rule separation
\newlength{\SY@LenSepA} % Separation above syllogism
\newlength{\SY@LenSepB} % Separation below syllogism
%
\newlength{\SY@DeltaC} % Added to rule in case the longest it the conclusion
\newlength{\SY@SpLabel} % Hspace between label and syllogism
\newlength{\SY@SpConEG} % Hspace between conclusion and Ergo-Sign
\newlength{\SY@Pre} % Hspace between left margin and label
\newlength{\SY@LenBefSyl} % Stores the total length before the premisses
\newlength{\SY@availSpace} % Stores the available width for premisses
\newlength{\SY@maxPLen} % Length of widest premiss or conclusion
\newlength{\SY@tempindent}
\newif\if@SYParam\@SYParamfalse % Boolean for optional argument
\newif\if@SYpCtooW\@SYpCtooWfalse % If the conclusion is too wide
%
%%%%%%%%%%%%%%%%%%%%%%%%  Defaults  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setlength{\SY@LenPA}{\z@}% Used to store the length of 1st premiss
\setlength{\SY@LenPB}{\z@}% Used to store the length of 2nd premiss
\setlength{\SY@LenC}{\z@}% Used to store the length of conclusion
\setlength{\SY@maxPLen}{\z@} % Used to store the available length
\setlength{\SY@LenSepA}{\baselineskip}% V-space before syllogism
\setlength{\SY@LenSepB}{\baselineskip}% V-space after syllogism
\setlength{\SY@HiRule}{.3pt}% `Height' of the rule
%
\setlength{\SY@LenAdjRule}{-1ex}% Adjust height of rule
%
\setlength{\SY@DeltaC}{1ex}
\setlength{\SY@SpLabel}{1em}
\setlength{\SY@Pre}{2em}
\setlength{\SY@SpConEG}{2em}
%
\def\SY@PuncPA{;} % Punctuation after first premiss
\def\SY@PuncPB{.} % Punctuation after first premiss
\def\SY@PuncC{.} % Punctuation after conclusion
\def\SY@ErgoSign{$\therefore$} % three-dot sign after conclusion (needs amssymb)
%
\newcounter{c:syl} % Counter of syllogisms, if required
\setcounter{c:syl}{1}
\newcommand{\syl}{(S\arabic{c:syl})\xspace}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%  Package Options  %%%%%%%%%%%%%%%%%%%%%%%%%%
\DeclareOption{spanish}{%
  \def\SY@Sed{pero\ } % Syncategorema before second premiss
  \def\SY@Ergo{Por lo tanto,\ } % Syncategorema before conclusion
  }
\DeclareOption{english}{%
  \def\SY@Sed{but\ } % Syncategorema before second premiss
  \def\SY@Ergo{Therefore\ } % Syncategorema before conclusion
  }
\DeclareOption{latin1}{%
  \def\SY@Sed{sed\ } % Syncategorema before second premiss
  \def\SY@Ergo{Ergo\ } % Syncategorema before conclusion
  }
\DeclareOption{latin2}{%
  \def\SY@Sed{sed\ } % Syncategorema before second premiss
  \def\SY@Ergo{Igitur\ } % Syncategorema before conclusion
  }
\ExecuteOptions{english}% Default
\ProcessOptions*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  Macros  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\setpresyl}% Command for changing h-space before the label
  [1]{\setlength{\SY@Pre}{#1}}
%
\newcommand{\setsyllabel}% Command for changing h-space after the label
  [1]{\setlength{\SY@SpLabel}{#1}}
%
\newcommand{\setsylrh}% Command for changing the `height' the the rule
  [1]{\setlength{\SY@HiRule}{#1}}
%
\newcommand{\setsylsepa}% Command for changing v-space before the syl.
  [1]{\setlength{\SY@LenSepA}{#1}}
%
\newcommand{\setsylsepb}% Command changing v-space after the syl.
  [1]{\setlength{\SY@LenSepB}{#1}}
%
\newcommand{\setsylsep}% Command for changing BOTH \SY@LenSepA and \SY@LenSepB
  [1]{\setlength{\SY@LenSepA}{#1}%
  \setlength{\SY@LenSepB}{#1}}
%
%
\newcommand{\setsylergo}%
  [1]{\def\SY@Ergo{#1\ }}
%
\newcommand{\setsylsed}%
  [1]{\def\SY@Sed{#1\ }}
%
\newcommand{\setsylpuncpa}%
  [1]{\def\SY@PuncPA{#1}}
%
\newcommand{\setsylpuncpb}%
  [1]{\def\SY@PuncPB{#1}}
%
\newcommand{\setsylpuncc}%
  [1]{\def\SY@PuncC{#1}}
%
\newcommand{\setsylergosign}%
  [1]{\def\SY@ErgoSign{#1}}
%
\newcommand{\@SYRestoreBooleans}{%
  \@SYParamfalse
  \@SYpCtooWfalse
  }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Basic syllogism %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\syllog{%
 \@ifnextchar[%]
   {\@SYParamtrue\@syllog}
   {\@syllog[]}}
\def\@syllog[#1]#2#3#4{%
  \setlength{\SY@tempindent}{\parindent}%
  \parindent\z@%
  %
  \settowidth{\SY@LenPA}{#2}
  \settowidth{\SY@LenPB}{#3}
  \settowidth{\SY@LenC}{#4}
    \addtolength{\SY@LenC}{\widthof{\SY@ErgoSign}}%
    \addtolength{\SY@LenC}{\SY@SpConEG}%
  %
  \if@SYParam% If optional parameter is provided...
      \setlength{\SY@LenLab}{\widthof{#1}}%
      \setlength{\SY@LenBefSyl}{\SY@Pre}%
        \addtolength{\SY@LenBefSyl}{\SY@LenLab}%
        \addtolength{\SY@LenBefSyl}{\SY@SpLabel}%
    \else%
      \setlength{\SY@LenBefSyl}{\SY@Pre}%
  \fi%
  %
  \setlength{\SY@availSpace}{\textwidth-\SY@LenBefSyl}
  %
  % Algorithm to check longest proposition -----------------------------
  \ifdim\SY@LenPA>\SY@availSpace%
    \setlength{\SY@LenPA}{\textwidth-\SY@LenBefSyl}%
  \fi
  \ifdim\SY@LenPB>\SY@availSpace%
    \setlength{\SY@LenPB}{\textwidth-\SY@LenBefSyl}%
    \fi%
  \ifdim\SY@LenC>\SY@availSpace%
    \setlength{\SY@LenC}{\textwidth-\SY@LenBefSyl}%
    \fi%
  %
  % Define the propositions -----------------------------------------------
  \def\@SYLpropA{#2}%
  \def\@SYLpropB{#3\SY@PuncPB}%
  \def\@SYLpropC{#4\hspace{\SY@SpConEG}\SY@ErgoSign}%
  %
  \if@SYpCtooW
      \relax
    \else
      \addtolength{\SY@LenC}{\SY@DeltaC}
    \fi
  % Set the rule's length ----------------------------------------------
  \setlength{\SY@LenRule}{%
    \maxof{\maxof{\maxof{\SY@LenPA}{\SY@LenPB}}{\maxof{\SY@LenPA}{\SY@LenC}}}%
      {\maxof{\SY@LenPB}{\SY@LenC}}%
    }%
  %
  % Construction of the Syllogism ----------------------------------------
  \par\vspace{\SY@LenSepA}%                 V-space before the syllogism
  \if@SYParam%                              Conditional H-space before syllogism
      \hspace{\SY@Pre}#1\hspace{\SY@SpLabel}%
   \else%
      \hspace{\SY@Pre}%
  \fi%
  %
  \begin{minipage}{\SY@LenRule}
    \begin{list}{}
      {%
        \setlength{\parsep}{\z@}
        \setlength{\itemsep}{\z@}
        \addtolength{\leftmargin}{-1.5em}
        \setlength{\itemindent}{-1ex}
        \setlength{\listparindent}{\itemindent}
      }
      \item \@SYLpropA
      \item \@SYLpropB%
        \vspace*{\SY@LenAdjRule}
      \item \rule{\SY@LenRule}{\SY@HiRule}
      \item \@SYLpropC
    \end{list}
  \end{minipage}
  \par\vspace{\SY@LenSepB}%                 V-space after the syllogism
  %
  \setlength{\parindent}{\SY@tempindent}%   Restore default indent
  \@SYRestoreBooleans% Restore booleans
  }%
%
%%%%%%%%%%%%%%%%%%%%%%%%%% Basic syllogism with counter %%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\syllogC}[3]{%
  \syllog[\syl]{#1}{#2}{#3}
  \stepcounter{c:syl}%
  }%

%%%%%%%%%%%%%%%%%%%%%%%%% Syllogism with text added %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\syllogTA{%
 \@ifnextchar[%]
   {\@SYParamtrue\@syllogTA}
   {\@syllogTA[]}}
\def\@syllogTA[#1]#2#3#4{%
  \setlength{\SY@tempindent}{\parindent}%
  \parindent\z@%
  %
  \settowidth{\SY@LenPA}{#2}
    \addtolength{\SY@LenPA}{\widthof{\SY@PuncPA}}
  \settowidth{\SY@LenPB}{#3}
    \addtolength{\SY@LenPB}{\widthof{\SY@Sed}}
    \addtolength{\SY@LenPB}{\widthof{\SY@PuncPB}}
  \settowidth{\SY@LenC}{#4}
    \addtolength{\SY@LenC}{\widthof{\SY@Ergo}}
    \addtolength{\SY@LenC}{\widthof{\SY@PuncC}}
    \addtolength{\SY@LenC}{\widthof{\SY@ErgoSign}}
    \addtolength{\SY@LenC}{\SY@SpConEG}
  %
  \if@SYParam% If optional parameter is provided...
      \setlength{\SY@LenLab}{\widthof{#1}}%
      \setlength{\SY@LenBefSyl}{\SY@Pre}%
        \addtolength{\SY@LenBefSyl}{\SY@LenLab}%
        \addtolength{\SY@LenBefSyl}{\SY@SpLabel}%
    \else%
      \setlength{\SY@LenBefSyl}{\SY@Pre}%
  \fi%
  %
  %
  \setlength{\SY@availSpace}{\textwidth}
    \advance\SY@availSpace -\SY@LenBefSyl
    \advance\SY@availSpace -1.3ex
  %
  % Algorithm to check longest proposition -----------------------------
  \ifdim\SY@LenPA>\SY@availSpace
      \setlength{\SY@LenPA}{\textwidth - \SY@LenBefSyl}
    \fi
  \ifdim\SY@LenPB>\SY@availSpace
      \setlength{\SY@LenPB}{\textwidth - \SY@LenBefSyl}
    \fi
  % Is the conclusion too wide? -----------------------------------------
  \ifdim\SY@LenC>\SY@availSpace
      \setlength{\SY@LenC}{\SY@availSpace}
      \@SYpCtooWtrue
    \else
      \@SYpCtooWfalse
    \fi%
  % Define the propositions -----------------------------------------------
  \def\@SYLpropA{#2\SY@PuncPA}
  \def\@SYLpropB{\SY@Sed#3\SY@PuncPB}
  \def\@SYLpropC{\SY@Ergo#4\SY@PuncC\hspace{\SY@SpConEG}\SY@ErgoSign}
  %
  \if@SYpCtooW
      \relax
    \else
      \addtolength{\SY@LenC}{\SY@DeltaC}
    \fi
  %
  % Set the rule's length ----------------------------------------------
  \setlength{\SY@LenRule}{%
    \maxof{\maxof{\maxof{\SY@LenPA}{\SY@LenPB}}{\maxof{\SY@LenPA}{\SY@LenC}}}%
      {\maxof{\SY@LenPB}{\SY@LenC}}%
    }%
  %
  % Construction of the Syllogism ----------------------------------------
  \par\vspace{\SY@LenSepA}%                 V-space before the syllogism
  \if@SYParam%                              Conditional H-space before syllogism
      \hspace{\SY@Pre}#1\hspace{\SY@SpLabel}%
   \else%
      \hspace{\SY@Pre}%
  \fi%
  %
  \begin{minipage}{\SY@LenRule}
    \begin{list}{}
      {%
        \setlength{\parsep}{\z@}
        \setlength{\itemsep}{\z@}
        \addtolength{\leftmargin}{-1.5em}
        \setlength{\itemindent}{-1ex}
        \setlength{\listparindent}{\itemindent}
      }
      \item \@SYLpropA%
      \item \@SYLpropB%
        \vspace*{\SY@LenAdjRule}%
      \item \rule{\SY@LenRule}{\SY@HiRule}%
      \item \@SYLpropC%
    \end{list}
  \end{minipage}
  \par\vspace{\SY@LenSepB}%                 V-space after the syllogism
  %
  \setlength{\parindent}{\SY@tempindent}%   Restore default indent
  \@SYRestoreBooleans% Restore booleans
}%

%%%%%%%%%%%%%%%%%%%% Syllogism with text added and counter %%%%%%%%%%%%%%%%%%%%%
\newcommand{\syllogTAC}[3]{%
  \syllogTA[\syl]{#1}{#2}{#3}
  \stepcounter{c:syl}%
  }%