%%
%% This is file `calculation.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% calculation.dtx  (with options: `exit')
%%    Calculations, in the style of Feijen-Dijkstra
%%    Copyright 2014, Maarten Fokkinga (m.m.fokkinga@gmail.com)
%% 
%%    This work may be distributed and/or modified under the
%%    conditions of the LaTeX Project Public License, either version 1.3
%%    of this license or (at your option) any later version.
%%    The latest version of this license is in
%%      http://www.latex-project.org/lppl.txt
%%    and version 1.3 or later is part of all distributions of LaTeX
%%    version 2005/12/01 or later.
%% 
%%    This work has the LPPL maintenance status `maintained'.
%% 
%%    The Current Maintainer of this work is Maarten Fokkinga.
%% 
%%    This work consists of the files calculation.dtx and calculation.ins
%%    and the derived file calculation.sty.

%% ==================== Usage ====================================
%%
%%    \begin{calculation}[SYMBOL]
%%       EXPRESSION   <-- may contain \doNumber and \comment{TEXT}
%%    \step*[SYMBOL]{HINT}
%%       EXPRESSION   <-- may contain \doNumber and \comment{TEXT}
%%    \step*[SYMBOL]{HINT}
%%       EXPRESSION   <-- may contain \doNumber and \comment{TEXT}
%%       ...
%%    \end{calculation}
%%    Each asterisk "*" and each part "[...]" is optional.
%%
%% -------------- DOCUMENT STYLE PARAMETERS ------------------------
%% options:
%%    fleqn, leqno, block
%% commands:
%%    \newcommand{\stepsymbol}{=}
%%    \newcommand{\Hblockopen}{|}
%%    \newcommand{\Hblockclose}{.}
%%    \newcommand{\Hlineopen}{\lbrace}
%%    \newcommand{\Hlineclose}{\rbrace}
%%    \newcommand{\Hindent}{1em}
%%    \newcommand{\Hsep}{1ex}
%%    \newcommand{\calculcolsep}{\arraycolsep}
%%    \newcommand{\Hposv}{t}
%%     % vertical position of the step symbol in front of a block hint
%% These default values may be redefined by "\renewcommand".
%%
\iffalse ============================== code =======================\fi

\def\fileversion{1.00}
\def\filedate{5 Jan 2015}
\def\docdate{5 Jan 2015}

\ProvidesPackage{calculation}[2014/12/05 Format reasoned calculations]



\DeclareOption{fleqn}{\calc@fleqn}
\DeclareOption{leqno}{\calc@leqno}
\DeclareOption{block}{\calc@blocktrue}


\newcommand{\calc@fill}{\relax}
\newcommand{\calc@eqnoLfil}{\relax}
\newcommand{\calc@eqnoRfil}{\relax}


\newcommand{\calc@leqno}
    {\def\calc@eqnoLfil{}\def\calc@eqnoRfil{\hfil}}


\newcommand{\calc@reqno}
    {\def\calc@eqnoRfil{}\def\calc@eqnoLfil{\hfil}}


\newcommand{\calc@ceqn}{\def\calc@fill{fil}}


\newcommand{\calc@fleqn}
   {\@ifundefined{mathindent}
         {\calc@indent\leftmargini}{\calc@indent\mathindent}
    \def\calc@fill{fill}}


\calc@ceqn
\calc@reqno
\newif\ifcalc@block \calc@blockfalse
\newdimen\calc@indent\calc@indent\z@skip


\ProcessOptions


\RequirePackage{delarray}


\newcommand\@ifmtarg[3]
   {{\def\myempty{}\def\myarg{#1}\ifx\myempty\myarg{#2}\else{#3}\fi}}
   %% usage:  \@ifmtarg {arg} {then} {else}


\newcommand{\stepsymbol}{=}
\newcommand{\Hblockopen}{|}
\newcommand{\Hblockclose}{.}
\newcommand{\Hlineopen}{\lbrace}
\newcommand{\Hlineclose}{\rbrace}
\newcommand{\Hindent}{1em}
\newcommand{\Hsep}{1ex}
\newcommand{\Hposv}{t}
\newcommand{\calculcolsep}{\arraycolsep}


\newcommand{\Hsepskip}{\hbox to \Hsep {}}
\newcommand{\HlineOPEN}
  {\ifcalc@block\else\makebox[0pt][r]{\m@th$\Hlineopen$\Hsepskip}\fi
   \ignorespaces}
\newcommand{\HlineCLOSE}
  {\ifcalc@block\else\makebox[0pt][l]{\m@th\Hsepskip$\Hlineclose$}\fi}


\newcommand{\HblockOPEN}{\ifcalc@block\Hblockopen\else.\fi}
\newcommand{\HblockCLOSE}{\ifcalc@block\Hblockclose\else.\fi}

\newcommand{\calc@origmath}{\relax}
\newcommand{\calc@stepsymbol}{\relax}
\newlength\calc@math       % to store the value of \mathsurround
\newif\ifcalc@emptyH       % for temporary local use only
\newif\ifcalc@numberedstep % true if currently in \step*
\newif\ifcalc@eqnoswitch   % true if an eqno will be produced
\newcommand{\calc@emptyHskip}{-0.5\baselineskip}% for empty hints
\calc@eqnoswitchfalse
\newcommand{\calc@doNumber}
    {\ifcalc@eqnoswitch \else
       \global\calc@eqnoswitchtrue
       \stepcounter{equation}
       \gdef\@currentlabel{\p@equation\theequation}
     \fi}

\iffalse ================= calculation environment ===================\fi

\newenvironment{calculation}[1][\stepsymbol]
 {\setlength\calc@math\mathsurround
  \def\calc@origmath{\mathsurround\calc@math}%
  \abovedisplayskip\topsep
  \ifvmode\advance\abovedisplayskip\partopsep\fi
  \belowdisplayskip\abovedisplayskip
  \belowdisplayshortskip\abovedisplayskip
  \abovedisplayshortskip\abovedisplayskip
  \def\calc@stepsymbol{#1}%
  \tabskip\calc@indent plus 1fil
  \let\\=\calc@cr
  \def\step{\calc@step}% Make \step available inside calculations
  \def\subcalculation{\calc@subcalculation}% similarly subcalculation
  \def\endsubcalculation{\endcalc@subcalculation}%
  $$
    \halign to \displaywidth
    \bgroup
        \tabskip\z@ \hfil\m@th$ ## $\hskip\calculcolsep
     &  \m@th$ ## $\hfil\tabskip 0pt plus 1\calc@fill
     &  \hfil\llap{##}\tabskip\z@
    \cr
    \calc@beginEXPR
 }
 {\calc@endEXPR
  \egroup $$ \global\@ignoretrue\ignorespaces}


\newcommand\calc@cr{
     \calc@endEXPR
     \noalign{\nopagebreak\vskip\jot}
     \calc@beginEXPR}

\iffalse ================= step command ===================\fi

\newcommand{\calc@step}
  {\@ifstar{\global\calc@numberedsteptrue\calc@@step}
           {\global\calc@numberedstepfalse\calc@@step}}

\newcommand{\calc@@step}[1][\calc@stepsymbol]{\calc@@@step{#1}}


\newcommand{\calc@@@step}[2]
 { \@ifmtarg{#2}{\global\calc@emptyHtrue}{\global\calc@emptyHfalse}
   \calc@endEXPR
   \ifcalc@emptyH \noalign{\vskip \calc@emptyHskip}
     \else \noalign{\nopagebreak\vskip\jot}
   \fi
   \ifcalc@numberedstep
     \refstepcounter{equation}
     \gdef\@currentlabel{\p@equation\theequation}
     \gdef\calc@stepno{\theequation}
   \fi
   #1 &
   \ifcalc@emptyH
   \else
      \hskip\Hindent
      \begin{tabular}[\Hposv]
      \HblockOPEN{>{\calc@origmath\Hsepskip}l<{\Hsepskip}}\HblockCLOSE
      \HlineOPEN #2\HlineCLOSE
      \end{tabular}
   \fi
   &
   \ifcalc@numberedstep \calc@print@theequation \fi
   \cr
   \ifcalc@emptyH \noalign{\vskip \calc@emptyHskip} \fi
   \noalign{\vskip\jot}
   \calc@beginEXPR
 }

\iffalse ====================== Auxiliary commands ===================\fi

\newcommand{\calc@beginEXPR}
   {&
    \def\doNumber{\calc@doNumber}
    \def\comment{\gdef\calc@comment}
    \gdef\calc@comment{}
    }


\newcommand{\calc@endEXPR}
   { & \calc@@eqno\calc@@comment \cr }


\newcommand{\calc@@comment}
   {\llap{\hbox to\linewidth
       {\calc@eqnoRfil \normalfont\normalcolor\calc@comment
        \calc@eqnoLfil}}}
\newcommand{\calc@@eqno}
   {\ifcalc@eqnoswitch
     \calc@print@theequation
     \global\calc@eqnoswitchfalse
    \fi}
\newcommand{\calc@print@theequation}
   {\llap{\hbox to\displaywidth
      {\calc@eqnoLfil \m@th\normalfont\normalcolor (\theequation)%
       \calc@eqnoRfil}}}

\iffalse ===================== subcalculation ===================== \fi

\newif\ifsubcalc@emptyH
\newcommand{\subcalc@stepsymbol}{\relax}


\newenvironment{calc@subcalculation}[1][\stepsymbol]{%
  \begin{minipage}[c]{0pt}
  \abovedisplayskip 0pt
  \belowdisplayskip \abovedisplayskip
  \belowdisplayshortskip\belowdisplayskip
  \abovedisplayshortskip\abovedisplayskip
  \def\subcalc@stepsymbol{#1}%
  \@ifundefined{mathindent}{\calc@indent\leftmargini}{}%
  \divide\calc@indent by 2
  \tabskip\calc@indent plus 1fil
  \let\\=\subcalc@cr
  \def\step{\subcalc@step}% Make \step available
  \def\comment
     {\@latex@error{No \string\comment\space in subcalculations}}%
  $$
  \m@th
    \halign
    \bgroup
      \tabskip\z@ \hfil$ ## $\hskip\calculcolsep
     &
      $ ## $\hfil\tabskip 0pt plus 1\calc@fill
    \cr
    &
    \global\@ignoretrue
  }
  { \raisebox{0pt}[0pt][1.5ex]{}
    \cr \egroup $$\par\end{minipage}
    \ignorespaces}

\newcommand\subcalc@cr
    {\cr
     \noalign{\nopagebreak\vskip\jot}
     &
     \global\@ignoretrue\ignorespaces
    }


\newcommand{\subcalc@step}
    {\@ifstar{\subcalc@@stepSTAR}{\subcalc@@step}}

\newcommand{\subcalc@@stepSTAR}[1]
    {\@latex@error
     {No \string\step* in subcalculations; use \string\step.}}

\newcommand{\subcalc@@step}[1][\subcalc@stepsymbol]
     {\subcalc@@@step{#1}}

\newcommand{\subcalc@@@step}[2]
 {\@ifmtarg{#2}
      {\global\subcalc@emptyHtrue}{\global\subcalc@emptyHfalse}
  \cr
  \ifsubcalc@emptyH
    \noalign{\vskip \calc@emptyHskip}
  \else
    \noalign{\nopagebreak\vskip\jot}
  \fi
  #1 &
  \ifsubcalc@emptyH \else
    \hskip\Hindent
    \hbox to 0pt {%
    \begin{tabular}[\Hposv]
    \HblockOPEN{>{\calc@origmath\Hsepskip}l<{\Hsepskip}}\HblockCLOSE
        \HlineOPEN #2\HlineCLOSE
    \end{tabular}}
  \fi
  \cr
    \ifsubcalc@emptyH \noalign{\vskip \calc@emptyHskip} \fi
  & \global\@ignoretrue
  }
\endinput
%%
%% End of file `calculation.sty'.
