-
Notifications
You must be signed in to change notification settings - Fork 1
/
algo.sty
129 lines (129 loc) · 3.33 KB
/
algo.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
%
% Algorithm environment
%
% $Modified: Mon Feb 12 21:38:28 1996 by otfried $
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{algo}
[1996/02/12 v1.2 LaTeX2e package for pseudo code]
\typeout{Package: `algo' v1.2 for pseudo code}
%
\newif\ifa@numbered
%
\DeclareOption{numbered}{\a@numberedtrue}
\DeclareOption{named}{\a@numberedfalse}
%
\ExecuteOptions{named}
\ProcessOptions
%
\RequirePackage{xspace}
%
\newlength{\a@movein}
\newlength{\a@curwidth}
%
\def\a@width#1{\settowidth{\a@curwidth}{\textbf{#1~}}}
\def\a@indent#1{\a@width{#1}\addtolength{\a@movein}{\a@curwidth}}
\def\a@outdent#1{\a@width{#1}\addtolength{\a@movein}{-\a@curwidth}}
%
\def\qinput#1{\item[\textbf{Input:~}]#1\ignorespaces}
\def\qoutput#1{\item[\textbf{Output:~}]#1\ignorespaces}
\def\qcomment#1{\item[\textrm{\qcom{#1}}]\ignorespaces}
\ifa@numbered
\def\qprocedure{\@ifnextchar[{\@qprocedure}{\@@qprocedure}}
\def\@qprocedure[#1]#2{\item[\textbf{Procedure~}]\qproc[#1]{#2}\ignorespaces}
\def\@@qprocedure#1{\item[\textbf{Procedure~}]\qproc{#1}\ignorespaces}
\else
\newcommand\qprocedure[2][]{}
\fi
%
\def\qif{{\bf if~}\a@indent{if~then}}
\def\qthen{\leavevmode\llap{{\bf then~}}}
\def\qelse{\leavevmode\a@width{then}\hbox to 0pt{\kern - \a@curwidth
\bf else\hss}}
\def\qfi{\a@outdent{if~then}}
\let\qendif\qfi
%
\def\qfor{{\bf for~}\a@indent{for~do}}
\def\qdo{\leavevmode\llap{\bf do~}}
\def\qrof{\a@outdent{for~do}}
\let\qendfor\qrof
\def\qwhile{\textbf{while~}\a@indent{while}}
\def\qend{\a@outdent{while}}
\let\qelihw\qend
\def\qrepeat{\textbf{repeat}\a@indent{rep}}
\def\quntil{\a@outdent{rep}\\\textbf{until~}}
%
\def\qcom#1{($\ast$~#1~$\ast$)}
%
\def\a@proc[#1]#2{{\it #2\/}{\rm ($#1$)}}
\def\a@noargproc#1{{\it #1\/}}
\def\qproc{\@ifnextchar[{\a@proc}{\a@noargproc}}
%
\def\qqif{\textbf{if}\xspace}
\def\qqthen{\textbf{then}\xspace}
\def\qqelse{\textbf{else}\xspace}
\def\qlet{\ensuremath{\mathrel{\leftarrow}}}
\def\qto{\textbf{to}\xspace}
\def\qdownto{\textbf{downto}\xspace}
\def\qand{\textbf{and}\xspace}
\def\qor{\textbf{or}\xspace}
\def\qnot{\textbf{not}\xspace}
\def\qstop{\textbf{stop}\xspace}
\def\qreturn{\textbf{return}\xspace}
%
\def\qnil{\textbf{nil}\xspace}
\def\qtrue{\textbf{true}\xspace}
\def\qfalse{\textbf{false}\xspace}
%
\newcounter{a@line}
\def\a@cr{\par
\advance\@totalleftmargin by \a@movein
\advance\linewidth by -\a@movein
\advance\labelwidth by \a@movein
\parshape \@ne \@totalleftmargin \linewidth
\a@movein 0pt
\item}
%
\newcounter{algorithm}
\def\a@counter{algorithm}
\edef\thealgorithm{\@thmcounter{algorithm}}
\def\algorithmcounter#1{\gdef\a@counter{#1}
\gdef\thealgorithm{\csname the#1\endcsname}}
%
% Algorithm environment:
% Use list environment to take care of vertical spacing before and
% after environment.
%
\def\algorithm#1{%
\ifa@numbered
\refstepcounter{\a@counter}%
\else
\edef\@currentlabel{\noexpand\a@noargproc{#1}}%
\fi
\@ifnextchar[{\a@paralgo{#1}}{\a@genalgo{\qproc{#1}}}% match ]
}
%
\def\a@paralgo#1[#2]{\a@genalgo{\qproc[{#2}]{#1}}}
%
\def\a@genalgo#1#2{%
\list{\arabic{a@line}.\hfill}%
{\usecounter{a@line}
\itemsep 0pt
\parsep 0pt
\leftmargin 2em
\rightmargin 0pt
\labelwidth 2em
\labelsep 0pt
\let\\=\a@cr
}
\ifa@numbered
\item[\textbf{Algorithm\ \thealgorithm}]
\ignorespaces #2
\else
\item[\textbf{Algorithm\ {#1}}]
\ignorespaces #2
\fi
\item
}
\def\endalgorithm{\endlist}
%