-
Notifications
You must be signed in to change notification settings - Fork 0
/
newalg.sty
153 lines (147 loc) · 4.31 KB
/
newalg.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%%
%% This is file `newalg.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% newalg.dtx (with options: `package')
%%
%% IMPORTANT NOTICE:
%%
%% For the copyright see the source file.
%%
%% Any modified versions of this file must be renamed
%% with new filenames distinct from newalg.sty.
%%
%% For distribution of the original source see the terms
%% for copying and modification in the file newalg.dtx.
%%
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{newalg}[1994/12/15 Format code algorithms nicely]
\newbox\algstack
\newbox\algtab
\newcount\algline
\newtoks\alga
\newtoks\algb
\newif\ifalgisenv
\def\alglpsh#1\to#2{\alga={#1\\}\algb=\expandafter{#2}
\global\edef#2{\the\alga\the\algb}}
\def\alglpop#1\to#2{\ifx\empty#1\def#2{}
\else\global\expandafter\algllop#1\algllop#1#2\fi}
\def\algllop#1\\#2\algllop#3#4{\def#3{#2}\def#4{#1}}
\def\algltop#1\to#2{\ifx\empty#1\def\#2{}\else
\expandafter\alglttop#1\alglttop#2\fi}
\def\alglttop#1\\#2\alglttop#3{\def#3{#1}}
\def\algckenv#1{\algltop\alglenv\to\algenv
\def\algarg{#1}
\ifx \algarg\algenv \algisenvtrue \else \algisenvfalse \fi}
\def\algsol{\global\advance\algline by 1
\the\algline&\hbox\bgroup\copy\algtab$\ignorespaces}
\def\algeol{$\egroup\cr\algsol}
\def\algpush{\global\setbox\algstack\hbox{\unhbox\algstack\box\algtab}}
\def\algpop{\global\setbox\algstack\hbox{\unhbox\algstack
\global\setbox\algtab\lastbox}}
\def\algset{$\egroup\setbox0=\lastbox\algpush
\global\setbox\algtab\hbox to \wd0{}\hbox\bgroup\unhbox0$}
\def\algorithm#1#2{\bgroup
\let\\=\algeol
\let\==\leftarrow
\let\IF=\algIF
\let\RETURN=\algRETURN
\let\ELSE=\algELSE
\let\endIF=\endalgIF
\let\ERROR=\algERROR
\let\NIL=\algNIL
\let\WHILE=\algWHILE
\let\endWHILE=\endalgWHILE
\let\FOR=\algFOR
\let\endFOR=\endalgFOR
\let\CALL=\algCALL
\let\text=\algtext
\let\TO=\algTO
\let\EACH=\algEACH
\let\SWITCH=\algSWITCH
\let\item=\algitem
\let\endSWITCH=\endalgSWITCH
\let\DEFAULT=\algDEFAULT
\let\REPEAT=\algREPEAT
\let\UNTIL=\endalgREPEAT
\let\endREPEAT=\UNTIL
\let\IN=\algIN
\let\begin=\algbegin
\let\end=\algend
\let\endalgorithm=\algalmostend
\global\setbox\algtab\null
\global\setbox\algstack\null
\global\algline=0
\def\alglenv{algorithm\\}
\halign\bgroup\space\hfill##&\quad##\hss \cr
\omit\CALL{#1}$(#2)$\span\omit\hfill \cr
\algsol}
\def\algalmostend{$\egroup\cr\egroup\egroup\strut\end{algorithm}}
\def\algkey#1{\mbox{\bf #1\ }}
\def\algIF#1{\algkey{if}\algset#1 \\
\algkey{then}\algset}
\def\algELSE{\algckenv{IF}
\ifalgisenv
\algpop \\
{\setbox0\hbox{\algkey{then}}
\hbox to \wd0{\algkey{else}\hfill}}
\algset
\else
\PackageError{newalg}
{\string\ELSE\space is only allowed in the IF environment}
{You have an \protect\ELSE\space command without a \string\begin{IF}}
\fi}
\def\endalgIF{\algpop\algpop}
\def\algERROR#1{\algkey{error}\mbox{``#1''}}
\def\algRETURN{\algkey{return}}
\def\algconst#1{\mbox{\sc #1}}
\def\algNIL{\algconst{nil}}
\def\algWHILE#1{\algkey{while}#1 \\
\indent\algkey{do}\algset}
\def\endalgWHILE{\algpop}
\def\algCALL#1{\mbox{\sc #1}}
\def\algFOR#1{\algkey{for}#1 \\
\indent\algkey{do}\algset}
\def\endalgFOR{\algpop}
\def\algtext#1{\mbox{ #1 }}
\def\algTO{\algkey{ to}}
\def\algEACH{\algkey{ each}}
\def\algSWITCH{\algkey{switch}\algpush}
\def\algitem#1{\algckenv{SWITCH}
\ifalgisenv
\algpop \\
\quad\algkey{case}\algset #1:
\else
\PackageError{newalg}
{\string\item\space can only be used in a SWITCH environment}
{You have an item that is not inside of the correct environment}
\fi}
\def\endalgSWITCH{\algpop}
\def\algDEFAULT{\algkey{default}}
\def\algREPEAT#1{
\algkey{repeat}\algset\\}
\def\endalgREPEAT{\algpop \\
\quad\algkey{until}}
\def\algIN{\algkey{ in}}
\def\algbegin#1{\alglpsh#1\to\alglenv
\csname #1\endcsname}
\def\algend#1{\alglpop\alglenv\to\algenv
\def\algarg{#1}
\ifx \algarg\algenv
\relax
\else
\PackageError{newalg}
{\string\begin{\algenv}\space ended by \string\end{\algarg}}
{We are confused. Try to return now}
\fi
\csname end#1\endcsname
}
\endinput
%%
%% End of file `newalg.sty'.