summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/alg/alg.dtx
blob: b0e7e57f53102243a379cd09b3c774782a03833f (plain)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
\def\filedate{13 March 2001}
% \iffalse
%% alg.dtx
%% Copyright (c) 1995, 1999, 2001 Staffan Ulfberg
%
% This program can redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or (at your option) any later version.
%
%<package>\NeedsTeXFormat{LaTeX2e}[1996/12/01]
%<package>\ProvidesPackage{alg}
%<package>         [2001/02/12]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{alg}
\DisableCrossrefs
\setlength{\parindent}{0pt}
\begin{document}
\title{The \textsf{alg} package\thanks{This file was last
       revised \filedate.}}
\author{Staffan Ulfberg\\staffan@ulfberg.se}
\date{\filedate}
\maketitle
\DocInput{alg.dtx}
\end{document}
%</driver>
% \fi
%
% \begin{abstract}
% This package defines two environments for typesetting algorithms in
% \LaTeXe\@.  Lines are automatically numbered and can be referenced,
% the means for easy indentation is provided, and algorithms can be made
% floating bodies if desired.
% \end{abstract}
%
% \section{Introduction}
% |alg.sty| defines two environments.  The \DescribeEnv{algtab} |algtab|
% environment is used to typeset an algorithm with automatically
% numbered lines; it also takes care of indentation.  The
% \DescribeEnv{algorithm} |algorithm| environment can be used to
% encapsulate the |algtab| environment in a floating body together
% with a header, a caption, etc.
%
% The package recognizes a few language options and changes its fixed
% output strings accordingly.  Please tell the author of this package
% if you need support for another language!
%
% \subsection{The \texttt{algorithm} environment}
%
% The |algorithm| environment starts a floating body; its placement
% is decided by an optional agrument that can be any combination of
% |t|, |b|, |p|, or |h|.  By default, the placement |H| (Here!) is
% used, so the float does not float after all.  See the
% description of the \textsf{float} package by Anselm Lingnau for
% more details about this.
%
% Inside the |algorithm| environment, the text will be indented from
% the left and right edges.  It is possible to put a |\caption| within
% the float, and also to generate a list of algorithms with the
% \DescribeMacro{\listofalgorithms} |\listofalgorithms| command.
%
% Notice that it is by no means necessary to use this environment to 
% use the rest of the package.
%
% \subsection{Algorithm description}
%
% Three macros are defined to describe an algorithm's name,
% parameters, and function.  They may be used in the |algorithm|
% environment, or just anywhere before the |algtab| environment.
% \begin{description}
% \item[\texttt{\bslash algname}] \DescribeMacro{\algname}
% takes two arguments of which the first is printed using small caps,
% followed by the second in parentheses.
% \item[\texttt{\bslash algdescript}] \DescribeMacro{\algdescript}
% takes one argument which is printed after the string
% ``Description:'' in boldface letters.
% \item[\texttt{\bslash alginout}] \DescribeMacro{\alginout}
% takes two arguments which are printed after the strings
% ``Input:'' and ``Output:'' respectively.
% \end{description}
% 
% \subsection{The \texttt{algtab} environment}
%
% In the |algtab| environment, an algorithm is typed line by line,
% separating lines by~|\\|.  Every time a new line is started, it will
% be numbered in the left margin starting at line~1.  A single
% ``line'' of the algorithm can span several lines of the page,
% but a line number will only be printed after each |\\| command.  The
% current |\ref| value is also set, so that a label can be entered; it's
% recommended that \DescribeMacro{\alglabel}|\alglabel| is used for this
% purpose.
% 
% The |algtab| environment has a star-form \DescribeEnv{algtab*} variant
% that supresses line number generation; if the |\alglabel| macro is
% used in this case, the supplied label argument is printed as-is in the
% left margin of the algorithm (where the line numbers usually are
% printed).  The macro \DescribeMacro{\algref} |\algref| corresponds
% to |\alglabel| in the way that it prints either the referenced
% label's value, or the label name literally if no line numbers are
% used.  However, |\algref| can't be used outside the |algtab|
% environment: use |\ref| intead.
% 
% The \DescribeMacro{\algnonumber} |\algnonumber| macro can be used at the
% start of a line to suppress the printing of a line number on that line.
%
% The |algtab| environment indents the algorithm to make room for the
% line numbers; the |algtab*| does not indent the algorithm by default.
% However, both environments take an optional argument specifying the
% amount of indentation that is desired.
%
% There are two important macros,  \DescribeMacro{\algbegin} |\algbegin|
% and \DescribeMacro{\algend} |\algend|, defined in the |algtab|
% environment.  These macros start and stop additional indentation
% respectively; they should always be used at the beginning of a line.
% These macros are already included in some of the macros that are
% special to the |algtab| environment.
%  
% \DeleteShortVerb{\|}
%
% \begin{algorithm}[h]
% \caption{Example of the \texttt{algorithm} environment.\label{alg:ex}}
% \alginout{An ordered set $U=\{u_1,u_2,\dots,u_n\}$.}
%          {The set's maximum element, and a set $A$, $|A|=\log n$,
%           containing the next largest element.  (If $n=1$, then
%           $A=\emptyset$).}
% \algname{Max2}{$U$}
% \begin{algtab}
% \algif{$|U|=1$}
%         \algreturn $(u_1, \emptyset)$ \\
% \algelsif{$|U|=2$}
%         \algifthenelse{$u_1>u_2$}{\algreturn $(u_1, \{u_2\})$}
%                                  {\algreturn $(u_2, \{u_1\})$}
% \algelse
%         $(b,B) \leftarrow$
%            \algcall{Max2}{$\{u_i\}_{i=1}^{\lfloor n/2\rfloor}$}\\
%         $(c,C) \leftarrow$
%            \algcall{Max2}{$\{u_i\}_{i=\lfloor n/2\rfloor+1}^{n}$}\\
%         \algifthenelse{$b>c$}{\algreturn $(b, \{c\}\cup B)$}
%                              {\algreturn $(c, \{b\}\cup C)$}
% \algend
% \end{algtab}
% \end{algorithm}
%
% \MakeShortVerb{\|}
%
% \section{Example}
%
% This section shows how to typeset Algorithm~\ref{alg:ex} using the
% \textsf{alg} package in \LaTeXe:
%
% \DeleteShortVerb{\|}
%
% \begin{verbatim}
% \begin{algorithm}[h]
% \caption{Example of the \texttt{algorithm} environment.\label{alg:ex}}
% \alginout{An ordered set $U=\{u_1,u_2,\dots,u_n\}$.}
%          {The set's maximum element, and a set $A$, $|A|=\log n$,
%           containing the next largest element.  (If $n=1$, then
%           $A=\emptyset$).}
% \algname{Max2}{$U$}
% \begin{algtab}
% \algif{$|U|=1$}
%         \algreturn $(u_1, \emptyset)$ \\
% \algelsif{$|U|=2$}
%         \algifthenelse{$u_1>u_2$}{\algreturn $(u_1, \{u_2\})$}
%                                  {\algreturn $(u_2, \{u_1\})$}
% \algelse
%         $(b,B) \leftarrow$
%            \algcall{Max2}{$\{u_i\}_{i=1}^{\lfloor n/2\rfloor}$}\\
%         $(c,C) \leftarrow$
%            \algcall{Max2}{$\{u_i\}_{i=\lfloor n/2\rfloor+1}^{n}$}\\
%         \algifthenelse{$b>c$}{\algreturn $(b, \{c\}\cup B)$}
%                              {\algreturn $(c, \{b\}\cup C)$}
% \algend
% \end{algtab}
% \end{algorithm}
% \end{verbatim}
%
% \MakeShortVerb{\|}
% 
% There is a list of all programming constructs available in the
% |algtab| environment at the end of this document.  They work
% very much like the macros used above.  As a general rule, if a macro
% doesn't take an argument, a space charater is included at the end of
% the macro definition.  For example, type |$a=0$ \algor \algnot $b=0$|
% to produce ``$a=0$ \algor \algnot $b=0$.''
%
% \section{The Implementation}
%
%    \begin{macrocode}
%<*package>
%    \end{macrocode}
%
% The |float| package by Anselm Lingnau is used for floating algorithms.
%    \begin{macrocode}
\RequirePackage{float, ifthen}
%    \end{macrocode}
% 
% Here are the variable declarations.  |\algleftmarginwidth|,
% |\algrightmarginwidth|, |\alglinenowidth|,
% and |\algtabwidth| define the amount of indentation of the entire
% algorithm, the space reserved for line numbers, and the indentation
% made by |\algbegin| respectively.
%    \begin{macrocode}
\newlength{\algleftmarginwidth}\setlength{\algleftmarginwidth}{.5in}
\newlength{\algrightmarginwidth}\setlength{\algrightmarginwidth}{.5in}
\newlength{\alglinenowidth}\setlength{\alglinenowidth}{1.2cm}
\newlength{\algtabwidth}\setlength{\algtabwidth}{.5cm}
\newlength{\alg@fromleft}
\newlength{\alg@tmplen}
\newsavebox{\alg@tmpbox}
\newcounter{alg@inmargin}\setcounter{alg@inmargin}{0}
\newcounter{algline}
\newboolean{alg@linenums}
\newboolean{alg@nonumber}
%    \end{macrocode}
%
% Strings used in captions and in the List of Algorithms differ in some
% languages.  If a language is specified as an option to this package,
% that language is used.  Otherwise, if the babel package is loaded, the
% selected babel lanugage is used.
%    \begin{macrocode}
\def\alg@language{english}
\@ifpackageloaded{babel}{
\iflanguage{english}{\def\alg@language{english}}{}
\iflanguage{american}{\def\alg@language{american}}{}
\iflanguage{swedish}{\def\alg@language{swedish}}{}
\iflanguage{french}{\def\alg@language{french}}{}
\iflanguage{german}{\def\alg@language{german}}{}}{}
\DeclareOption{english}{\def\alg@language{english}}
\DeclareOption{american}{\def\alg@language{american}}
\DeclareOption{swedish}{\def\alg@language{swedish}}
\DeclareOption{french}{\def\alg@language{french}}
\DeclareOption{german}{\def\alg@language{german}}
\ProcessOptions
\ifthenelse{\equal{\alg@language}{english}}{
	\def\alg@floatname{Algorithm}
	\def\alg@listname{List of Algorithms}
	\def\alg@descname{Description}
	\def\alg@inputname{Input}
	\def\alg@outputname{Output}}{}
\ifthenelse{\equal{\alg@language}{american}}{
	\def\alg@floatname{Algorithm}
	\def\alg@listname{List of Algorithms}
	\def\alg@descname{Description}
	\def\alg@inputname{Input}
	\def\alg@outputname{Output}}{}
\ifthenelse{\equal{\alg@language}{swedish}}{
	\def\alg@floatname{Algoritm}
	\def\alg@listname{Algoritmer}
	\def\alg@descname{Beskrivning}
	\def\alg@inputname{Input}
	\def\alg@outputname{Output}}{}
\ifthenelse{\equal{\alg@language}{french}}{
	\def\alg@floatname{Algorithme}
	\def\alg@listname{Liste des algorithmes}
	\def\alg@descname{Description}
	\def\alg@inputname{Entr\'{e}e}
	\def\alg@outputname{Sortie}}{}
\ifthenelse{\equal{\alg@language}{german}}{
	\def\alg@floatname{Algorithmus}
	\def\alg@listname{Algorithmenverzeichnis}
	\def\alg@descname{Beschreibung}
	\def\alg@inputname{Eingabe}
	\def\alg@outputname{Ausgabe}}{}
%    \end{macrocode}
%
% The following definitions set up the properties of the
% |algorithmfloat|.
%    \begin{macrocode}
\newcommand\floatc@alg[2]{{\bfseries\rmfamily
   \hspace{\algleftmarginwidth}#1:} #2\par}
\newcommand\fs@alg{
   \let\@fs@capt\floatc@alg
   \def\@fs@pre{}\def\@fs@post{}\def\@fs@mid{\vspace{3pt}}
   \let\@fs@iftopcapt\iftrue}
\floatstyle{alg}
\newfloat{algorithmfloat}{h}{loa}
\floatname{algorithmfloat}{\alg@floatname}
%    \end{macrocode}
%
% \begin{macro}{\listofalgorithms}
% The |\listofalgorithms| macro can be used to generate a list of all
% algorithms in a document.
%    \begin{macrocode}
\newcommand{\listofalgorithms}{\listof{algorithmfloat}{\alg@listname}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\alg@margin}
% The |alg@margin| macro makes the text a bit narrower.  It is used in
% the start of both the |algorithm| and |algtab| environments, and also
% in the |algname|, |algdescript|, and |alginout| macros ; it keeps
% track of if the text is already narrow, and in this case does nothing.
%    \begin{macrocode}
\newcommand{\alg@margin} {
   \ifthenelse{\value{alg@inmargin}=0} {
       \advance\leftskip\algleftmarginwidth
       \advance\rightskip\algrightmarginwidth
       \alg@fromleft=\leftskip
   } {}
   \stepcounter{alg@inmargin}
   \parskip=0cm\parindent=0cm
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\alg@unmargin}
% The |alg@unmargin| macro resets any indentation made by |alg@margin|.
%    \begin{macrocode}
\newcommand{\alg@unmargin} {
   \addtocounter{alg@inmargin}{-1}%
   \advance\leftskip-\algleftmarginwidth%
   \advance\rightskip-\algrightmarginwidth%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{environment}{algorithm}
% The |algorithm| environment simply begins a float as defined above.
% Actually, the default is to use the placement parameter |H|, so that
% the algorithm will not really float.  Inside the float all text is
% indented by |\algleftmarginwidth| and |\algrightmarginwidth|.
%    \begin{macrocode}
\newenvironment{algorithm}[1][H] {
   \begin{algorithmfloat}[#1]\alg@margin
} {
   \alg@unmargin\end{algorithmfloat}
}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{alg@tab}
% The |alg@tab| environment is what does most of the formatting job;
% it's called by |algtab| and |algtab*| defined below.  The argument
% defines the amount of initial indentation.  If the counter
% |alg@inmargin| is zero, |alg@tab| is not started within a float.  In
% this case some room is made above and below it.  Inside this
% environment, |\\| is let to the macro |alg@cr| that is used to 
% begin new lines.  The |catcode| for |^^M| is changed to admit blank
% input lines within an algorithm.
%    \begin{macrocode}
\newenvironment{alg@tab}[1] {
   \setboolean{alg@nonumber}{false}%
   \ifthenelse{\value{alg@inmargin}=0} {\vskip\baselineskip}{}
   \alg@margin
   \let\\=\alg@cr
   \catcode`\^^M=10
   \setcounter{algline}{0}\refstepcounter{algline}
   \advance\leftskip#1
   \alg@putlineno\ignorespaces
} {
   \setbox\alg@tmpbox=\lastbox
   \ifhbox\alg@tmpbox{\vskip-\baselineskip}\else\par\fi
   \alg@unmargin
   \ifthenelse{\value{alg@inmargin}=0}{\vskip\baselineskip}{}
}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{algtab}
% This environment sets |alg@linenums| true, which will make
% |\algputlineno| write the current line number in the left margin.
%    \begin{macrocode}
\newenvironment{algtab}[1][\alglinenowidth] {
   \setboolean{alg@linenums}{true}\begin{alg@tab}{#1}
} {\end{alg@tab}}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{algtab*}
% The |algtab*| environment works like |algtab| but sets |alg@linenums|
% false and uses zero indentation by default.
%    \begin{macrocode}
\newenvironment{algtab*}[1][0cm] {
   \setboolean{alg@linenums}{false}\begin{alg@tab}{#1}
} {\end{alg@tab}}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\alg@kill}
% The |\alg@kill| macro removes the last box from the current
% horizontal list.  It is used to remove the box containing the line
% number (label) when indentation is changed: in this case a new box
% for the label must be created.
%    \begin{macrocode}
\newcommand{\alg@kill}{\setbox\alg@tmpbox=\lastbox%
   \ifvoid\alg@tmpbox\PackageError{alg}{Attempt to remove label
      in middle of line}\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\algbegin}
% |\algbegin| adds to |\leftskip| and replaces the line number.  It
% must only be used at the beginning of a line.
%    \begin{macrocode}
\newcommand{\algbegin}[1][\algtabwidth]{\advance\leftskip#1%
   \alg@kill\alg@putlineno\ignorespaces}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\algend}
% This macro reverses the effect of a previous |\algbegin|.
%    \begin{macrocode}
\newcommand{\algend}[1][\algtabwidth]{\advance\leftskip-1#1%
   \alg@kill\alg@putlineno\ignorespaces}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\algnonumber}
%    \begin{macrocode}
\newcommand{\algnonumber}{\alg@kill\alg@putlabel{}%
   \setboolean{alg@nonumber}{true}\ignorespaces}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\alg@cr}
% New lines are started using this macro.  The line number is
% incremented and printed.
%    \begin{macrocode}
\newcommand{\alg@cr}{\par\refstepcounter{algline}%
   \setboolean{alg@nonumber}{false}\alg@putlineno\ignorespaces}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\alg@putlineno}
% Line numbers are typeset as labels.
%    \begin{macrocode}
\newcommand{\alg@putlineno} {%
   \ifthenelse{\boolean{alg@linenums}} {%
      \ifthenelse{\boolean{alg@nonumber}}{\alg@putlabel{}} {%
         \alg@putlabel{(\arabic{algline})}}}%
      {\alg@putlabel{}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\alg@putlabel}
% This is the macro that puts a label on the horizontal list.  The
% label extends to the left of a zero-width box.
%    \begin{macrocode}
\newcommand{\alg@putlabel}[1]{{%
      \alg@tmplen=\leftskip \advance\alg@tmplen-\alg@fromleft%
      \makebox[0cm][r]{\makebox[\alg@tmplen][l]{#1}}}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Macros for algorithm descriptions}
% 
%    \begin{macrocode}
\newcommand{\algdescript}[1]{\alg@margin\textbf{\alg@descname: }#1\par}
\newcommand{\alginout}[2]{\alg@margin\textbf{\alg@inputname: }#1\par
   \textbf{\alg@outputname: }#2\par}
\newcommand{\algname}[2]{\alg@margin\textsc{#1}(#2)\par}
%    \end{macrocode}
%
% \subsection{Macros for referencing}
% 
%    \begin{macrocode}
\newcommand{\alglabel}[1]{%
   \ifthenelse{\boolean{alg@linenums}}{%
      \label{#1}}{\alg@kill\alg@putlabel{#1}}\ignorespaces}
\newcommand{\algref}[1]{\ifthenelse{\boolean{alg@linenums}}%
   {\ref{#1}}{#1}}
%    \end{macrocode}
%
% \subsection{Macros for the \texttt{algtab} environment}
%
%    \begin{macrocode}
\newcommand{\algand}{\mbox{\textbf{and }}}
\newcommand{\algbreak}{\textbf{break}}
\newcommand{\algcall}[2]{\textsc{#1}(#2)}
\newcommand{\algcase}[1]{\algend\textbf{case} #1\\\algbegin}
\newcommand{\algcontinue}{\textbf{continue}}
\newcommand{\algdefault}{\algend\textbf{default}\\\algbegin}
\newcommand{\algelse}{\algend\textbf{else}\\\algbegin}
\newcommand{\algelseif}[1]{\algelsif{#1}}
\newcommand{\algelsif}[1]{\algend\textbf{else if} #1\\\algbegin}
\newcommand{\algerror}{\textbf{error }}
\newcommand{\algfalse}{\mbox{\textbf{false }}}
\newcommand{\algforto}[2]{\textbf{for} #1 \textbf{to} #2\\\algbegin}
\newcommand{\algforeach}[1]{\textbf{foreach} #1\\\algbegin}
\newcommand{\alggoto}{\textbf{goto~}}
\newcommand{\algif}[1]{\textbf{if} #1\\\algbegin}
\newcommand{\algifthen}[2]{\textbf{if} #1 \textbf{then} #2\\}
\newcommand{\algifthenelse}[3]{\setbox\alg@tmpbox=
   \hbox{\textbf{if} #1 }\copy\alg@tmpbox\textbf{then} #2\\
   \settowidth{\alg@tmplen}{\box\alg@tmpbox}%
   \algbegin[\alg@tmplen]\textbf{else} #3\\ \algend[\alg@tmplen]}
\newcommand{\algnot}{\mbox{\textbf{not }}}
\newcommand{\algor}{\mbox{\textbf{or }}}
\newcommand{\algpardo}{\mbox{\textbf{pardo}}}
\newcommand{\algprint}{\textbf{print }}
\newcommand{\algrepeat}{\textbf{repeat}\\\algbegin}
\newcommand{\algreturn}{\textbf{return~}}
\newcommand{\algswitch}[1]{\textbf{switch} #1\\\algbegin}
\newcommand{\algtrue}{\mbox{\textbf{true }}}
\newcommand{\alguntil}[1]{\algend\textbf{until} #1\ \\}
\newcommand{\algwhile}[1]{\textbf{while} #1\\\algbegin}
%    \end{macrocode}
%
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% \Finale