This repository has been archived by the owner on Jun 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnicealgo.sty
executable file
·283 lines (238 loc) · 9.3 KB
/
nicealgo.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
%%
%%
%% Algorithm Drawing Package - Felipe Bergo <[email protected]>
%% with automatic line numbering, nice display and indentation bars
%%
%% example (bad one ;-):
%%
%% \begin{nicealgo}{a.ref}
%% \naTITLE{How To Find Prime Numbers}
%% \naBEGIN{\naFOR $i=0$ \naTO $+\infty$ \naDO }
%% \naEND{ask $i$ if it's prime.}
%% \end{nicealgo}
%%
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{nicealgo}
[2005/04/08 v1.6
nice algorithm drawing%
]
%
% usage: \usepackage[options]{nicealgo}
%
% optional ( parameters in [] like \usepackage[br,nolines]{nicealgo} ) :
%
% language options:
% English (default) - any one of english, us, uk, en
% Portuguese - any one of pt, portugues, portuges
% Brazilian Portuguese - any one of br, brazil
%
% style options:
% Draw lines on indentation
% (default) - lines
% Do not draw lines on
% indentation - nolines
% Draw dash before title
% (default) - dash
% Do not draw dash before
% title - nodash
% Less indentation - narrow
%
% Provided Environments:
%
% \begin{nicealgo}{yourlabel} ... \end{nicealgo}
% - you can reference the algorithm number
% with \ref{yourlabel}
%
% Provided Commands:
%
% \naTITLE{yourtitle} - running title of the algorithm (must be
% the first thing inside the nicealgo environment)
% \naINPUT{text} - typesets input variables in the preamble
% \naOUTPUT{text} - typesets output variables in the preamble
% \naAUX{text} - typesets aux variables in the preamble
% \naSKIP - skips fixed vertical space after the preamble
%
% \naBEGIN{text} - first line of a new indentation level
% \na{text} - algorithm line, current indentation
% \naEND{text} - last line of current indentation
% \naENDN{n}{text} - last line of n indentation levels
% \nac{text} - continuation line, looks ugly.
% \nacBEGIN{text} - continuation line, looks ugly.
% \nacEND{text} - continuation line, looks ugly.
% \nacENDN{n}{text} - continuation line, looks ugly.
%
% \naword{word} - typesets word in keyword typeface
% \naname{text} - typesets algorithm name as in the title
%
% Existing keywords:
%
% \naWHILE \naIF \naTHEN \naELSE \naFOR
% \naFOREACH \naDO \naREPEAT \naUNTIL \naRETURN
% \naEXIT \naTO
%
% Additional stuff:
%
% \mget - typesets a left arrow for assignment
% (like a <- 0). Inside math mode only.
%
\newcommand{\naiFOR}{fnord }
\newcommand{\naiTO}{fnord }
\newcommand{\naiDO}{fnord }
\newcommand{\naiFOREACH}{fnord }
\newcommand{\naiWHILE}{fnord }
\newcommand{\naiIF}{fnord }
\newcommand{\naiTHEN}{fnord }
\newcommand{\naiELSE}{fnord }
\newcommand{\naiREPEAT}{fnord }
\newcommand{\naiUNTIL}{fnord }
\newcommand{\naiRETURN}{fnord }
\newcommand{\naiEXIT}{fnord }
\newcommand{\naiALGORITHM}{fnord }
\newcommand{\naiINPUT}{fnord }
\newcommand{\naiOUTPUT}{fnord }
\newcommand{\naiAUX}{fnord }
\newcommand{\naiMAXPH}{\naiAUX}
\newif\if@lines\@linestrue
\newif\if@dash\@dashtrue
\newif\if@narrow\@narrowfalse
\DeclareOption{english}{
\renewcommand{\naiFOR}{For }
\renewcommand{\naiTO}{to }
\renewcommand{\naiDO}{do }
\renewcommand{\naiFOREACH}{For each }
\renewcommand{\naiWHILE}{While }
\renewcommand{\naiIF}{If }
\renewcommand{\naiTHEN}{then }
\renewcommand{\naiELSE}{Else }
\renewcommand{\naiREPEAT}{Repeat }
\renewcommand{\naiUNTIL}{Until }
\renewcommand{\naiRETURN}{Return }
\renewcommand{\naiEXIT}{Exit }
\renewcommand{\naiALGORITHM}{Algorithm}
\renewcommand{\naiINPUT}{Input:}
\renewcommand{\naiOUTPUT}{Output:}
\renewcommand{\naiAUX}{Auxiliary:}
\renewcommand{\naiMAXPH}{\naiAUX}
}
\DeclareOption{portugues}{
\renewcommand{\naiFOR}{Para }
\renewcommand{\naiTO}{At{\'e} }
\renewcommand{\naiDO}{Fa{\c{c}}a }
\renewcommand{\naiFOREACH}{Para Cada }
\renewcommand{\naiWHILE}{Enquanto }
\renewcommand{\naiIF}{Se }
\renewcommand{\naiTHEN}{Ent{\~{a}}o }
\renewcommand{\naiELSE}{Sen{\~{a}}o }
\renewcommand{\naiREPEAT}{Repita }
\renewcommand{\naiUNTIL}{At{\'e} Que }
\renewcommand{\naiRETURN}{Retorne }
\renewcommand{\naiEXIT}{Termine }
\renewcommand{\naiALGORITHM}{Algoritmo}
\renewcommand{\naiINPUT}{Entrada:}
\renewcommand{\naiOUTPUT}{Sa{\'\i}da:}
\renewcommand{\naiAUX}{Auxiliares:}
\renewcommand{\naiMAXPH}{\naiAUX}
}
%%% proxies for the provided options
\DeclareOption{us}{
\ExecuteOptions{english}}
\DeclareOption{uk}{
\ExecuteOptions{english}}
\DeclareOption{br}{
\ExecuteOptions{portugues}}
\DeclareOption{pt}{
\ExecuteOptions{portugues}}
\DeclareOption{brazil}{
\ExecuteOptions{portugues}}
\DeclareOption{portuges}{
\typeout{Hmm, babel-educated person, it's 'portugues', not 'portuges'.}%
\ExecuteOptions{portugues}}
\DeclareOption{lines}{\@linestrue}
\DeclareOption{nolines}{\@linesfalse}
\DeclareOption{dash}{\@dashtrue}
\DeclareOption{nodash}{\@dashfalse}
\DeclareOption{narrow}{\@narrowtrue}
%% defaults
\ExecuteOptions{english}
\ExecuteOptions{lines}
\ExecuteOptions{dash}
\ProcessOptions*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{calc}
\newcounter{nalevel}
\newcounter{naline}
\newcounter{natmp}
\newcounter{nahaspreamble}
\newlength{\naxxx}
\newlength{\nayyy}
\newlength{\nazzz}
\newcommand{\natab}{\if@narrow xxx \= x \= x \= x \= x \= x \= x \= x \= x \= x \= x \= x \= x \= x\kill\else xxx \= xx \= xx \= xx \= xx \= xx \= xx \= xx \= xx \= xx \= xx \= xx \= xx \kill\fi}
\newcommand{\navert}{\if@lines\>$\,$\vrule \@width 0.5pt \>\else \>\fi}
\newcommand{\nacorner}{\if@lines\>$\,$\setlength{\nazzz}{.09\baselineskip}\addtolength{\nazzz}{0.5pt}\vrule \@width 0.5pt \@height 0.7\baselineskip \@depth -.09\baselineskip\vrule \@width 4pt \@height \nazzz \@depth -.09\baselineskip \>\else \>\fi}
% font used for the preamble headers, likely to be sticky, protect with { } if needed
\newcommand{\napreambfh}[1]{\textsc{#1}}
% font used for preamble body
\newcommand{\napreambft}[1]{\emph{#1}}
\newtheorem{algorithm}{ \naiALGORITHM}
% #1:label for \ref
\newenvironment{nicealgo}[1]{
\setcounter{nahaspreamble}{0}\begin{algorithm}\label{#1}\setcounter{nalevel}{0}\setcounter{naline}{1}}
{\end{tabbing}\end{small}\end{algorithm}}
\newcommand{\naentertab}{\begin{tabbing}\natab}
\newcommand{\naleavetab}{\end{tabbing}}
\newcommand{\naname}[1]{{\textrm{\textsc{#1}}}}
% #1: title
\newcommand{\naTITLE}[1]{\if@dash{--} \fi\naname{#1}\begin{small}\vspace{8pt}\\}
%% the twocolumn 0.48 factor guess is known to work with the IEEEtran class,
%% not tested elsewhere.
\newcommand{\naPREAMBLE}{\setcounter{nahaspreamble}{1}\settowidth{\naxxx}{\napreambfh{\naiMAXPH}}\setlength{\nayyy}{\textwidth}\if@twocolumn\setlength{\nayyy}{0.48\textwidth}\fi\addtolength{\nayyy}{-1.0\naxxx}\addtolength{\nayyy}{-2em}\begin{tabular}{lp{\nayyy}}}
\newcommand{\naBODY}{\ifnum\value{nahaspreamble}>0\end{tabular}\fi\naentertab}
\newcommand{\naINPUT}[1]{\napreambfh{\naiINPUT} & \napreambft{#1} \\}
\newcommand{\naOUTPUT}[1]{\napreambfh{\naiOUTPUT} & \napreambft{#1} \\}
\newcommand{\naAUX}[1]{\napreambfh{\naiAUX} & \napreambft{#1} \\}
% prints and increments line number
\newcommand{\naprintnum}{\emph{\arabic{naline}.}\>\addtocounter{naline}{1}}
\newcommand{\napbar}[1]{\ifnum#1>0\navert
\setcounter{natmp}{#1}\addtocounter{natmp}{-1}
\napbar{\value{natmp}}\fi}
% prints bars
\newcommand{\naprintbars}{\napbar{\value{nalevel}}}
\newcommand{\naprintcorners}[1]{\ifnum#1>0\nacorner\setcounter{natmp}{#1}
\addtocounter{natmp}{-1}\naprintcorners{\value{natmp}}\fi}
% algorithm line, starting an indentation level
\newcommand{\naBEGIN}[1]{\naprintnum\naprintbars #1 \addtocounter{nalevel}{1} \\}
% algorithm line, keeping indentation level
\newcommand{\na}[1]{\naprintnum\naprintbars #1 \\}
% continuation line
\newcommand{\nac}[1]{\>\naprintbars \> #1 \\}
% continuation line
\newcommand{\nacBEGIN}[1]{\>\naprintbars \> #1 \addtocounter{nalevel}{1} \\}
% continuation line
\newcommand{\nacEND}[1]{\nacENDN{1}{#1}}
% continuation line
\newcommand{\nacENDN}[2]{\>\ifnum#1>0\setcounter{natmp}{#1}\loop
\addtocounter{nalevel}{-1}\addtocounter{natmp}{-1}
\ifnum\value{natmp}>0\repeat\fi\naprintbars\ifnum#1>1
\naprintcorners{#1}\else\naprintcorners{1}\fi \> #2\\}
% algorithm line, last of indentation level
% optional arg: number of levels to leave
\newcommand{\naENDN}[2]{\naprintnum\ifnum#1>0\setcounter{natmp}{#1}\loop
\addtocounter{nalevel}{-1}\addtocounter{natmp}{-1}
\ifnum\value{natmp}>0\repeat\fi\naprintbars\ifnum#1>1
\naprintcorners{#1}\else\naprintcorners{1}\fi #2\\}
\newcommand{\naEND}[1]{\naENDN{1}{#1}}
\newcommand{\naword}[1]{\textsf{\textbf{#1}}}
\newcommand{\naWHILE}{\naword{\naiWHILE}}
\newcommand{\naIF}{\naword{\naiIF}}
\newcommand{\naTHEN}{\naword{\naiTHEN}}
\newcommand{\naELSE}{\naword{\naiELSE}}
\newcommand{\naFOR}{\naword{\naiFOR}}
\newcommand{\naTO}{\naword{\naiTO}}
\newcommand{\naFOREACH}{\naword{\naiFOREACH}}
\newcommand{\naDO}{\naword{\naiDO}}
\newcommand{\naREPEAT}{\naword{\naiREPEAT}}
\newcommand{\naUNTIL}{\naword{\naiUNTIL}}
\newcommand{\naRETURN}{\naword{\naiRETURN}}
\newcommand{\naEXIT}{\naword{\naiEXIT}}
\newcommand{\mget}{\leftarrow}