%\filename{algmacros.tex}
%\edited{last by stolfi on Mon Aug 17 20:47:18 1987}

% Macros for algorithms

\catcode`@=11 \endlinechar=-1

\message{algorithms ...}

% DIMENSIONAL PARAMETERS

  % Parameters that can be changed by the user

    \newdimen\algindent % indentation of outermost bracket
    \newdimen\alglevelindent % extra indentation per level
    \newdimen\algstepindent % indentation of step text (exc. step number)
                            %   relative to innermost bracket
    \newdimen\algcommindent % indent. of comment text rel. innermost bracket
    \newdimen\algcontindent % extra indentation of continuation lines
    \newglue\algstepnoglue % space between step no and step text
    \newdimen\algthickness % thickness of block brackets
    \newglue\algvsep % extra space between steps (will not stretch)

  % Standard settings
  % Assumes the parameter \textindent has been defined

    \algindent=20pt % indentation of outermost bracket
    \algthickness= 0.6pt   % thickness of block brackets
    \alglevelindent = 17pt % extra indent. per level (incl. \algthickness)
    \algstepindent = 16pt  % indent. of \step text rel. to innermost bracket
    \algcommindent = 16pt % indent. of \comm text rel. to innermost bracket
    \algcontindent = 0pt   % extra indentation of continuation lines
    \algstepnoglue = 2pt   % space between step number and step text
    \algvsep=3pt % extra space between steps (will not stretch)

% HEADER

% Usage: \algorithm{1.1}{Computing integer numbers.}

  \def\algorithm#1#2{\theolemma{\it}{Algorithm}{#1}{#2}}

% BODY

% Usage:

  %     \algbody
  %       \rem{We are lost now}
  %       \step1{If there is any hope, then}
  %         \begblock
  %           \step{1A}{\sna{Don't despair yet} Go on.}
  %         \endblock
  %       \unstep{Else,}
  %         \begblock
  %           \step2{Give up.}
  %         \endblock
  %       \step3{Terminate the algorithm.}
  %      \endbody

  \newcount\nb@rs
  \newglue\st@pwidth
  \newglue\algn@hang

  \outer\def\algbody{\smallvbreak
    \begingroup
      \parindent\z@ \hangindent\z@ \hangafter\z@
      \leftglue\z@\rightglue\z@
      \parsep\z@ \baselinesep -\p@ \linesep\z@
      \nb@rs\z@ \st@pwidth\hsize
      \advance\st@pwidth by -\algindent
      \setbox2\hbox{\bf 2.\hskip\algstepnoglue} \algn@hang=\wd2

  \def\begblock{\begingroup
          \advance\nb@rs by 1 \alge@r\z@\z@
          \advance\st@pwidth by -\alglevelindent}

  \def\endblock{\alge@r\z@\z@\endgroup}

  \def\endbody{\endblock\endgroup\par\smallvbreak}

  \let\rem=\algc@mment       % comment
      \let\step=\algst@p           % numbered step
      \let\unstep=\algunst@p     % un-numbered step
      \def\sna##1{{[{\it ##1\/}]}} % step name/function.

  \begblock}

% INTERNAL MACROS

% No user-serviceable parts below.

  % Numbered step

    \long\def\algst@p#1#2{
      \algunst@p{\hbox{\null\kern-\algn@hang\bf #1.\hskip\algstepnoglue}#2}
       } % numbered steps

  % Unnumbered step

    \long\def\algunst@p#1{\algit@m\algstepindent{
      \normalsize\rm\hmode\strut #1\strut}}

  % Comment

  \long\def\algc@mment#1{\algit@m\algcommindent{
    \smallsize\it\hmode\strut $\{$#1\/$\}$\strut}}

  % Internal algorithm step.

  \long\def\algit@m#1#2{\par\penalty 600
    \hbox
     {\algvb@rs \hskip-\alglevelindent
      \hskip #1
      \vbox{\hsize=\st@pwidth \advance\hsize by -#1
         \vskip 0.5\algvsep
         \vbox{\hangindent\algcontindent \hangafter1 #2}
        \vskip 0.5\algvsep }
      \hfil}}

  % "ear" for block brackets

  \def\alge@r#1#2{\par\penalty\@M
    \hbox
     {\algvb@rs \hskip-\alglevelindent
       \vbox{\vskip #1
         \hrule height \algthickness width 0.4em
         \vskip #2}
       \hfil}
    \penalty\@M}

  % generates pieces of left brackets

  \def\algvb@rs{\hskip \algindent
    \rep\nb@rs{\vrule width\algthickness
      \hskip-\algthickness \hskip\alglevelindent}}

\catcode`@=12 \endlinechar=`\^^M

% END
