-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpseudocode.sty
377 lines (297 loc) · 11.5 KB
/
pseudocode.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
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% pseudocode.sty, by Dario Sneidermanis. %
% %
% Based on package `clrscode3e', written by Thomas H. Cormen: %
% %
% Package for producing pseudocode in the style of Cormen, Leiserson, %
% Rivest, and Stein, Introduction to Algorithms, Third edition. %
% %
% http://www.cs.dartmouth.edu/~thc/clrscode/ %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{pseudocode}
\RequirePackage{graphics}
\RequirePackage{xparse}
%%%%%%%%%%%%%%%%%%%%%%%
% Formatting commands %
%%%%%%%%%%%%%%%%%%%%%%%
% Typesets constants.
% Usage examples:
% \const{true}
% \const{nil}
\newcommand{\const}[1]{\textnormal{\scshape#1}}
% Typesets variables.
% Usage examples:
% \var{key}
% \var{left-sum}
% \var{i}
\newcommand{\var}[1]{\ensuremath{\hspace*{-0.5pt}\mathit{\text@hyphens#1}}}
% Typesets functions.
% Usage examples:
% \func{Insertion-Sort}{}
% \func{Dijkstra}{G, s}
% \func{Dijkstra}{G = graph, s = vertex}
\NewDocumentCommand{\func}{m g} {\@@function{\textnormal{\scshape#1}}{#2}}
% Typesets fixed functions.
% Usage examples:
% \fixfunc{sin}{x}
% \fixfunc{out-degree}{G, v}
\NewDocumentCommand{\fixfunc}{m g} {%
\@@function{\ensuremath{{\mathop{\text@hyphens\operator@font#1}\nolimits}}}{#2}%
}
% Typesets object attributes.
% Usage examples:
% \member{user}{name}
\newcommand{\member}[2]{\var{#1}\ensuremath{.\@@tsp\@@tsp}\var{#2}}
% Typesets array elements.
% Usage examples:
% \at{names}{pos}
\newcommand{\at}[2]{\ensuremath{\var{#1}\@@tsp[\@@surround{\var{#2}}]}}
% Typesets keywords.
% Usage examples:
% \keyword{for}
% \keyword{if}
\newcommand{\kw}[1]{\textbf{#1}}
% Symbol for typesetting subarray ranges.
% Usage examples:
% $[1 \twodots n]$
\newcommand{\twodots}{\mathinner{\ldotp\ldotp}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pseudocode2 environment %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\if@@activepseudocode\@@activepseudocodefalse
\newcounter{@@indentlevel}
\NewDocumentEnvironment{pseudocode2}{o}
{%
\if@@activepseudocode\PackageError{pseudocode}{Nested pseudocode environments not supported!}\fi%
\global\@@activepseudocodetrue%
\setcounter{@@indentlevel}{0}%
\newenvironment{block}{\addtocounter{@@indentlevel}{1}}{\addtocounter{@@indentlevel}{-1}}%
\addtolength{\topsep}{0.5ex}%
\def\@@indent{\textbf{else} }%
\begin{trivlist}%
\item\parindent=0pt%
\@nobreaktrue\IfValueT{#1}{#1\rule[-2ex]{0pt}{0pt}\\}%
\begin{minipage}[t]{1000pt}% TODO: fix this hardcoded size
\begin{tabbing}%
99\=\@@indent\=\@@indent\=\@@indent\=\@@indent\=\@@indent\=\@@indent\=\@@indent\=\@@indent\=\@@indent\=\+\kill%
} {%
\end{tabbing}%
\end{minipage}%
\end{trivlist}%
\addtolength{\topsep}{-0.5ex}%
\global\@@activepseudocodefalse%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pseudocode environment %
%%%%%%%%%%%%%%%%%%%%%%%%%%
% When we make a pseudocode, we save the code part into a box before printing it.
% We do not actually print the code until we know how many line numbers there
% are.
\newsavebox{\savecode}
% The \ifprocname command tells us whether this procedure has been given a name.
% yet.
\newif\ifprocname
% The \iffirstcodeline command tells us whether we are about to produce the
% first line other than the procedure declaration.
\newif\iffirstcodeline
% \digitwidth gives the width of a single digit. All digits are the same width.
% We'll need this amount to do the right thing for line numbers.
\newlength{\digitwidth}
\settowidth{\digitwidth}{0}
% Assume that the width of the pseudocode is the width of the text, minus the width
% of 2 digits. We'll correct for that later.
\newlength{\pseudocodewidth}
\setlength{\pseudocodewidth}{\linewidth} % Thanks, David Etherington!
\addtolength{\pseudocodewidth}{-2\digitwidth}
% The codelinenumber counter counts the current line number.
\newcounter{codelinenumber}
% The indent counter keeps track of the current indentation level.
\newcounter{indent}
% The "pseudocode" environment produces an unbreakable section of code.
\newenvironment{pseudocode}{%
% this proc hasn't been given a name yet
\global\procnamefalse%
% producing the first line
\setcounter{codelinenumber}{0}%
\setcounter{indent}{0}%
\firstcodelinetrue%
% distance between numbers and code
\setlength{\tabbingsep}{1em}%
% Initialize \@lilabel to allow a pageref \label cmd at the beginning of the
% pseudocode
\global\let\@lilabel\@currentlabel
\def\@currentlabel{\@lilabel}%
% save the code into a box
\begin{lrbox}{\savecode}%
% it'll be a minipage
\begin{minipage}[t]{\pseudocodewidth}%
% set up the tab stops
\def\codeindent{\textbf{else} }%
\begin{tabbing}%
99\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\codeindent\=\+\kill%
}{%
% Here's what's run at the end of a pseudocode environment. Start by making
% sure that we have ended at indent level 0. Otherwise, print a warning.
\ifnum\value{indent}=0\else\typeout{Warning: Indentation ends at level \theindent\space in pseudocode on page \thepage.}\fi%
% close all open environments
\end{tabbing}%
\end{minipage}%
\end{lrbox}%
% for the following trivlist
\addtolength{\topsep}{0.5ex}%
\begin{trivlist}\item\parindent=0pt%
% If there was a procedure name given, print it now but with a little
% space below, and disallow a page break after the procedure name.
\@nobreaktrue%
\ifprocname\saveprocname\rule[-2ex]{0pt}{0pt}\\ \fi%
% Put in the right amount of space, depending on whether we reached
% double digits in the line numbers.
\ifnum\value{codelinenumber}>9\hspace*{2\digitwidth}\else\hspace*{1\digitwidth}\fi%
% Now print the code
\usebox{\savecode}%
\end{trivlist}%
\addtolength{\topsep}{-0.5ex}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pseudocode environment commands %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function name.
\NewDocumentCommand{\function}{ m g } {%
\global\def\saveprocname{\func{#1}{#2}}%
\global\procnametrue%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Pseudocode indentation %
%%%%%%%%%%%%%%%%%%%%%%%%%%
% counter for recursive indenting code.
\newcounter{thisindent}
% \putindents is a recursive macro that indents a number of times given by the
% counter thisindent.
\newcommand{\putindents}{%
\ifnum\value{thisindent}>0\>\addtocounter{thisindent}{-1}\putindents\fi%
}
\newcommand{\Indent}{\setcounter{thisindent}{\value{indent}}\putindents}
% Indent the next line one level more.
\newcommand{\Indentmore}{\addtocounter{indent}{1}}
% should this line have a number.
\newif\ifnumberedline\numberedlinetrue
% \liprint actually prints the line number and sets up the indentation.
\newcommand{\liprint}{%
\protected@xdef\@lilabel{\thecodelinenumber}%
\ifnumberedline\thecodelinenumber\fi\'\Indent%
}
% The \li command starts a new numbered line. The optional defines sets with a
% little extra the space above.
\newcommand{\li}[1][2pt]{%
\global\numberedlinetrue%
\iffirstcodeline\global\firstcodelinefalse\else\\[#1] \fi
\stepcounter{codelinenumber}%
\liprint%
}
\newcommand{\liempty}{\li[0pt]\li[0pt]}
\providecommand{\numref}[1]{%
\@ifundefined{r@#1}{000}{%
\expandafter\expandafter\expandafter\@firstoftwo
\csname r@#1\endcsname
}%
}
% \setlinenumber sets the line number to its argument.
\newcommand{\setlinenumber}[1]{%
\setcounter{codelinenumber}{\numref{#1}}%
\addtocounter{codelinenumber}{-1}%
}
% \setlinenumberplus sets the line number to its first argument plus its second
% argument.
\newcommand{\setlinenumberplus}[2]{%
\setcounter{codelinenumber}{\numref{#1}}%
\addtocounter{codelinenumber}{-1}\addtocounter{codelinenumber}{#2}%
}
% The \zi command starts a new unnumbered line.
\newcommand{\zi}{%
\global\numberedlinefalse%
\iffirstcodeline\global\firstcodelinefalse\else\\ \fi
\liprint%
}
% Temporarily make all lines indented so that they start at the end of a given
% text.
\newcommand{\Startalign}[1]{\\ \pushtabs\FakeIndent#1\=\kill}
\newcommand{\Stopalign}{\poptabs}
\newcommand{\FakeIndent}{\setcounter{thisindent}{\value{indent}}\putfakeindents}
\newcommand{\putfakeindents}{\ifnum\value{thisindent}>0\textbf{else }\addtocounter{thisindent}{-1}\putfakeindents\fi}
%%%%%%%%%%%%
% Keywords %
%%%%%%%%%%%%
\newcommand{\For}{\textbf{for} }
\newcommand{\To}{\ifmmode\ \textrm{\textbf{to}}\ \else\textbf{to}\ \fi}
\newcommand{\By}{\ifmmode\ \textrm{\textbf{by}}\ \else\textbf{by}\ \fi}
\newcommand{\Downto}{\ifmmode\ \textrm{\textbf{downto}}\ \else\textbf{downto}\ \fi}
\newcommand{\While}{\textbf{while} }
\newcommand{\Repeat}{\textbf{repeat}\>\addtocounter{indent}{1}}
\newcommand{\Until}{\kill\addtocounter{indent}{-1}\liprint\textbf{until} }
\newcommand{\If}{\textbf{if} }
\newcommand{\Then}{\>\addtocounter{indent}{1}}
\newcommand{\Else}{\kill\addtocounter{indent}{-1}\liprint\textbf{else}\>\addtocounter{indent}{1}}
\newcommand{\End}{\addtocounter{indent}{-1}}
\newcommand{\ElseIf}{\kill\addtocounter{indent}{-1}\liprint\textbf{elseif} }
\newcommand{\ElseNoIf}{\kill\addtocounter{indent}{-1}\liprint\textbf{else} \addtocounter{indent}{1}}
\newcommand{\Do}{\>\addtocounter{indent}{1}}
\newcommand{\Return}{\textbf{return} }
\newcommand{\CommentSymbol}{\texttt{\textbf{/\hspace*{-0.3em}/}}}
\newcommand{\Comment}{\CommentSymbol\ }
\newcommand{\RComment}{\`\CommentSymbol\ }
\newcommand{\Goto}{\textbf{goto} }
\newcommand{\Error}{\textbf{error} } % optionally followed by string argument.
\newcommand{\EndTest}{\textbf{:}}
\newcommand{\Spawn}{\ifmmode\textbf{spawn}\ \else\textbf{spawn} \fi}
\newcommand{\Sync}{\textbf{sync}}
\newcommand{\Parfor}{\textbf{parallel for} }
%%%%%%%%%%%%%%%%
% Misc helpers %
%%%%%%%%%%%%%%%%
% Tiny space.
\newcommand{\@@tsp}{\hspace*{0.5pt}}
% Forces hyphens.
\newcommand{\text@hyphens}{\mathcode`\-=`\-\relax}
%%%%%%%%%%%%%%%%%%%%%
% @@function helper %
%%%%%%%%%%%%%%%%%%%%%
% Tells if we are parsing the first function argument.
\newif\if@@firstarg
% Typesets function calls/declarations.
\NewDocumentCommand{\@@function}{m >{\SplitList{,}} g} {%
#1%
\IfValueT{#2} {%
\textrm{\@@tsp(%
\@@surround{%
\global\@@firstargtrue%
\ProcessList{#2}{\@@parsearg}%
\global\@@firstargfalse%
}%
)}%
}%
}
% Splits a function argument.
\NewDocumentCommand{\@@parsearg}{>{\SplitArgument{1}{:}} m} {\@@printarg#1}
% Parses a function argument.
\NewDocumentCommand{\@@printarg}{m m} {%
\if@@firstarg\global\@@firstargfalse\else{\textit{,\ }}\fi%
\var{#1}%
\IfValueT{#2} {\textrm{\@@tsp :\ #2\@@tsp}}%
}
%%%%%%%%%%%%%%%%%%%%%
% @@surround helper %
%%%%%%%%%%%%%%%%%%%%%
% Holds the width of the text being surrounded.
\newlength{\@@paramlen}
% Holds the maximum accepted length without spaces.
\newlength{\@@maxlen}\settowidth{\@@maxlen}{\var{W}}
% Surrounds text with spaces depending on its length.
\newcommand{\@@surround}[1]{%
\settowidth{\@@paramlen}{\var{#1}}%
\ifdim\@@paramlen>\@@maxlen\@@tsp\@@tsp\@@tsp\fi%
#1%
\ifdim\@@paramlen>\@@maxlen\@@tsp\@@tsp\fi%
}
\endinput