Debian Codegraph @

texmf-dist/doc/latex/algxpar/algxpar-doc.tex

raw
1 %! Author = Jander Moreira
2 %! Email = [email protected]
3
4 \documentclass[a4paper, 11pt]{article}
5 \usepackage[T1]{fontenc}
6
7 \usepackage[presets]{packdoc}
8
9
10 \usepackage[
11 brazilian,
12 language = english,
13 ]{algxpar}
14
15 \usepackage{needspace}
16
17
18 \begin{filecontents}[overwrite, nosearch, noheader]{algxpar-lzw.tex}
19 \begin{algorithmic}[1]
20 \Description LZW Compression using a table with all known sequences of bytes.
21 \Input A flow of bytes
22 \Output A flow of bits with the compressed representation of the input bytes
23 \Statex
24 \Statep{Initialize a table with all byte values}[each table position holds a single byte]
25 \Statep{Initialize \Id{sequence} with the first byte from the input stream}
26 \While{there are bytes in the input}[wait until all bytes have been processed]
27 \Statep{Retrieve a single byte from the input and store it in \Id{byte}}
28 \If{the concatenation of \Id{sequence} and \Id{byte} exists in the table}
29 \Statep{Set \Id{sequence} to $\Id{sequence} + \Id{byte}$}[concatenate without generating any output]
30 \Else
31 \Statep{Output the code for \Id{sequence}}[i.e., the binary representation of its index in the table]
32 \Statep{Add the concatenation of \Id{sequence} and \Id{byte} to the table}[the table learns a new, longer sequence]
33 \Statep{Set \Id{sequence} to \Id{byte}}[begin a new sequence with the remaining byte]
34 \EndIf
35 \EndWhile
36 \Statep{Output the code for \Id{sequence}}[the remaining bit sequence]
37 \end{algorithmic}
38 \end{filecontents}
39
40
41 %% Repetitive text
42 \NewDocumentCommand{\MacroOptionsText}{}{%
43 Any \Argument{options} specified apply only to this macro.%
44 }
45 \NewDocumentCommand{\BlockOptionsText}{}{%
46 Any \Argument{options} specified in this macro will impact this command and all items within the inner block, propagating up to and including the closing macro.%
47 }
48
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
50
51 \title{%
52 The \PackageName{algxpar} package\\\normalsize\AlgVersion
53 }
54 \author{Jander Moreira -- \texttt{[email protected]}}
55 \date{\AlgDate}
56
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58
59
60 \begin{document}
61 \maketitle
62
63 \begin{abstract}
64 The \PackageName{algxpar} package is an extension of the \PackageName{algorithmicx}\footnote{\url{https://ctan.org/pkg/algorithmicx}.}/\PackageName{algpseudocode} package to handle multi-line text with proper indentation and provide a number of other improvements.
65 \end{abstract}
66 \tableofcontents
67
68 \PDNewVersion{0.9}{2019-11-12}
69 \PDAddChange{0.9}{
70 description = Initial version.,
71 no box,
72 no page,
73 }%
74 \PDNewVersion{0.91}{2020-08-01}
75 \PDNewVersion{0.99}{2023-06-28}
76 \PDAddChange{0.99}{
77 description = The code was reviewd and almost entirely rewritten.,
78 no box,
79 no page,
80 }.
81 \PDNewVersion{0.99.1}{2024/05/05}
82 \PDNewVersion{0.99.2}{2024/10/23}
83 \PDAddChange{0.99.2}{
84 description = Part of the code was rewritten using \PackageName{etoolbox}.,
85 no box,
86 no page,
87 }
88 \PDNewVersion{0.99.2a}{2025/07/31}
89 \PDAddChange{0.99.2a}{
90 description = {Fixed length computation in non-commented lines to consider the token length.},
91 no box,
92 no page,
93 }
94
95
96 \begin{figure}
97 \tcbinputlisting{example, listing file = algxpar-lzw.tex, listing only}
98 \vspace{3em}
99 \tcbinputlisting{example, listing file = algxpar-lzw.tex, text only}
100 \end{figure}
101
102 \PDPrintChanges
103
104
105 \section{Introduction}
106 I teach algorithms and programming, and for writing my algorithms, I've adopted the \PackageName{algorithmicx} package (\PackageName{algpseudocode}). It allows me to create pseudocode that's both clear and easy to read, requiring minimal effort to ensure it looks visually appealing.
107
108 Teaching algorithms involves a different approach to pseudocode compared to its typical use in scientific papers, where solutions are often presented in a more formal, concise manner. Students typically work with abstract algorithms before learning a specific programming language, so the focus is more on the logic of the solution than on the variables themselves. Additionally, teaching strategies that emphasize refinement and iteration call for a less programmatic and more descriptive style of code. For instance, instead of writing something like ``${s \gets s + c}$,'' we might say, ``\emph{accumulate current expenses in the total sum of costs},'' as the latter provides a clearer explanation of the logic without requiring students to understand the specifics of variable manipulation.
109
110 However, this more verbose approach often results in longer sentences that can span multiple lines. Given that pseudocode places a premium on visual clarity—particularly in control structures and indentation—it became necessary to create a package that accommodates multi-line statements while maintaining readability.
111
112 The \PackageName{algorithmicx} and \PackageName{algpseudocode} packages do not natively support multi-line statements. Therefore, this package extends several macros to manage multi-line code properly, adding new commands and features to improve its functionality.
113
114
115 \needspace{3cm}
116
117
118 \section{Package usage and options}\label{sec:package-usage-and-options}
119 This package depends on the following packages:
120
121 \begin{center}
122 \begin{tabular}{ll}
123 \PackageName{algorithmicx} & (\url{https://ctan.org/pkg/algorithmicx}) \\
124 \PackageName{algpseudocode} & (\url{https://ctan.org/pkg/algorithmicx}) \\
125 \PackageName{amssymb} & (\url{https://ctan.org/pkg/amsfonts}) \\
126 \PackageName{etoolbox} & (\url{https://ctan.org/pkg/etoolbox}) \\
127 % \PackageName{fancyvrb} & (\url{https://ctan.org/pkg/fancyvrb}) \\
128 \PackageName{pgfmath} & (\url{https://ctan.org/pkg/pgf}) \\
129 \PackageName{pgfopts} & (\url{https://ctan.org/pkg/pgf}) \\
130 \PackageName{ragged2e} & (\url{https://ctan.org/pkg/ragged2e}) \\
131 % \PackageName{tcolorbox} & (\url{https://www.ctan.org/pkg/tcolorbox}) \\
132 \PackageName{varwidth} & (\url{https://www.ctan.org/pkg/varwidth}) \\
133 \PackageName{xcolor} & (\url{https://www.ctan.org/pkg/xcolor}) \\
134 \end{tabular}
135 \end{center}
136
137 \medskip
138 To use the package, simply request its use in the preamble of the document.
139
140 \begin{Macro*}{usepackage}{\OArg{package options list}\PArg{algxpar}}{}
141 \end{Macro*}
142
143 Currently, the list of package options includes the following.
144
145 \begin{Option*}{\Argument{language name}}{}{}
146 Algorithm keywords are typically created in English by default, with the English keyword set always being loaded. When available, one can use keyword sets in other languages by specifying the desired language.
147
148 Currently supported languages:
149 \begin{itemize}
150 \item \OptionInd{english} (default language, always loaded)
151 \item \OptionInd{brazilian} Brazilian Portuguese
152 \end{itemize}
153 \end{Option*}
154
155 \begin{PDListing}
156 % Loads Brazilian keyword set and sets it as default
157 \usepackage[brazilian]{algxpar}
158 \end{PDListing}
159
160 \begin{Optiondef}{language}{\Argument{language name}}{}
161 This option selects the keyword set corresponding to \Argument{language name} as the document's default. It is available as a general option (see \OptionRef{language}).
162
163 This option is useful when other languages are loaded.
164 \end{Optiondef}
165
166 \begin{PDListing}
167 % Loads Brazilian keyword set but keeps English as default
168 \usepackage[brazilian, language = english]{algxpar}
169 \end{PDListing}
170
171 \begin{Optiondef}{noend}{\PDInline{true} | \PDInline{false}}{Default: \PDInline{true}; initially: \PDInline{true}}
172 The \Option{noend} option suppresses the line marking the end of a block, while preserving the indentation.
173
174 See more information in \OptionRef{end} and \OptionRef{noend} options.
175 \end{Optiondef}
176
177 \begin{PDListing}
178 % Suppresses all end-lines that close a block
179 \usepackage[noend]{algxpar}
180 \end{PDListing}
181
182
183 \section{Writting pseudocode}
184 Algorithms, following the structure of the \PackageName{algorithmicx} package, are written within the \PDInline{algorithmic} environment. The option to use a number to determine line numbering is preserved from the original version.
185
186 An algorithm consists of instructions, control structures like conditionals and loops, as well as documentation and comments.
187
188 \begin{PDExample}
189 \begin{algorithmic}
190 \Description Calculation of the factorial of a natural number
191 \Input $n \in \mathbb{N}$
192 \Output $n!$
193 \Statex
194 \Statep{\Read $n$}
195 \Statep{$\Id{factorial} \gets 1$}[$0! = 1! = 1$]
196 \For{$k \gets 2$ \To $n$}[from 2 up]
197 \Statep{$\Id{factorial} \gets \Id{factorial} \times k$}[$(k-1)! \times k$]
198 \EndFor
199 \Statep{\Write \Id{factorial}}
200 \end{algorithmic}
201 \end{PDExample}
202
203 \subsection{A preamble on comments}\label{sec:a-preamble-on-comments}
204 This is Euclid's algorithm as shown in the \PackageName{algorithmicx} package documentation\footnote{A label has been suppressed here.}.
205
206 \begingroup
207 %! formatter = off
208 \AlgSet{comment font = {}}
209 \begin{PDExample}
210 \begin{algorithmic}[1]
211 \Procedure{Euclid}{$a,b$}
212 \Comment{The g.c.d. of a and b}
213 \State $r\gets a\bmod b$
214 \While{$r\not=0$}
215 \Comment{We have the answer if r is 0}
216 \State $a\gets b$
217 \State $b\gets r$
218 \State $r\gets a\bmod b$
219 \EndWhile
220 \State \textbf{return} $b$\Comment{The gcd is b}
221 \EndProcedure
222 \end{algorithmic}
223 \end{PDExample}
224 %! formatter = on
225 \endgroup
226
227 Comments are added \textit{in loco} with the \MacroRef{Comment} macro, which makes them appear along the right margin. The \PackageName{algxpar} package embeded comments as part of the commands themselves in order to add multi-line support.
228
229 Until \PackageName{algxpar} v0.95, they could be added as an optional parameter before the text, in the style of most \LaTeX\ macros.
230
231 %! formatter = off
232 \begingroup
233 \AlgSet{comment font = {}}
234 \begin{PDExample}
235 \begin{algorithmic}[1]
236 \Procedure[The g.c.d. of a and b]{Euclid}{$a,b$} % <-- Comment
237 \State $r\gets a\bmod b$
238 \While[We have the answer if r is 0]{$r\not=0$} % <-- Comment
239 \State $a\gets b$
240 \State $b\gets r$
241 \State $r\gets a\bmod b$
242 \EndWhile
243 \Statep[The gcd is b]{\Keyword{return} $b$} % <-- Comment
244 \EndProcedure
245 \end{algorithmic}
246 \end{PDExample}
247 \endgroup
248 %! formatter = on
249
250 Using the comment before the text always bothered me somewhat, as it seemed more natural to put it after.
251 \PDAddChange{
252 type = new,
253 version = 0.99,
254 description = Optional comments may be specified after the command
255 }%
256 Thus, as of v0.99, the comment can be placed after the text (as the second parameter of the macro), certainly making writing algorithms more user-friendly. To maintain backward compatibility, the use of comments before text is still supported, although it is discouraged.
257
258 \PDAddChange{
259 type = new,
260 version = 0.99,
261 description = Lines that closes blocks can have comments
262 }%
263 In addition to this change, the use of comments in the new format has been extended to most pseudocode macros, such as \MacroRef{EndWhile} for example.
264
265 %! formatter = off
266 \begingroup
267 \AlgSet{comment font = {}}
268 \begin{PDExample}
269 \begin{algorithmic}[1]
270 \Procedure{Euclid}{$a,b$}[The g.c.d. of a and b] % <-- Comment
271 \State $r\gets a\bmod b$
272 \While{$r\not=0$}[We have the answer if r is 0] % <-- Comment
273 \State $a\gets b$
274 \State $b\gets r$
275 \State $r\gets a\bmod b$
276 \EndWhile[end of the loop] % <-- Comment
277 \Statep{\Keyword{return} $b$}[The gcd is b] % <-- Comment
278 \EndProcedure
279 \end{algorithmic}
280 \end{PDExample}
281 \endgroup
282 %! formatter = on
283
284 Using \MacroRef{Comment} still produces the expected result, although it may break automatic tracking of longer lines.
285
286 Throughout this documentation, former style comments are denoted as \Argument{comment*}, while the new format uses \Argument{comment}.
287
288 See more about comments in \cref{sec:comments}.
289
290 \subsection{A preamble on options}\label{sec:a-preamble-on-options}
291 \PDAddChange{0.99}{
292 description = {The ``look and feel'' of the pseudocode can be set in the \Macro{begin}\PDInline{{algorithmic}}},
293 no box,
294 }%
295 As of version 0.99, a list of options can be added to each command, changing some algorithm presentation settings. These settings are optional and must be entered using angle brackets at the end of the command.
296
297 %! formatter = off
298 \begin{PDExample}
299 \begin{algorithmic}
300 <keyword font = \scshape\bfseries, comment width = nice>
301 \If{$a > b$}[check conditions]
302 \While{$a > 0$}
303 <keyword color = blue!70>
304 \Statep{\Call{Process}{$a$}}[process current data]
305 \EndWhile
306 \EndIf
307 \end{algorithmic}
308 \end{PDExample}
309 %! formatter = on
310
311 There is a lot of additional information about options and how they can be used. See discussion and full list in \cref{sec:customization-and-fine-tunning}.
312
313 \subsection{Statements}\label{sec:statements}
314
315 The macros \Macro{State} and \Macro{Statex} defined in \PackageName{algorithmicx} can still be used for single statements and have the same general behaviour.
316
317 For automatic handling of comments and multi-line text, the \Macro{Statep} macro is available, which should be used instead of \Macro{State}.
318
319 \begin{Macrodef}{Statep}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
320 The \Macro{Statep} macro corresponds to an statement that can extrapolate a single line. The continuation of each line is indented from the baseline and this indentation is based on the value indicated in the \OptionRef{statement indent} option.
321
322 \MacroOptionsText
323 \end{Macrodef}
324
325 % As an example, observe lines~\ref{alg:lzw:output} and \ref{alg:lzw:add-to-table} of the LZW compression algorithm on \cpageref{alg:lzw:add-to-table}.
326
327 \subsection{Flow Control Blocks}\label{sec:flow-control-blocks}
328 Flow control is essentially based on conditionals and loop.
329
330 \subsubsection{The \Keyword{if} block}
331 This block is the standard \textit{if} block.
332
333 \begin{PDExample}
334 \begin{algorithmic}
335 \State \Read $v$
336 \If{$v < 0$}[is it negative?]
337 \Statep{$v \gets -v$}[make it positive]
338 \EndIf
339 \end{algorithmic}
340 \end{PDExample}
341
342 \begin{Macrodef}{If}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
343 \Macro{If} shows \Argument{text} (the condition) and must be closed with an \MacroRef{EndIf}, creating a block of nested commands.
344
345 \BlockOptionsText
346 \end{Macrodef}
347
348 \begin{Macrodef}{EndIf}{\OArg{comment}\AArg{options}}{}
349 \Macro{EndIf} closes its respective \MacroRef{If}.
350
351 \MacroOptionsText
352 \end{Macrodef}
353
354 \begin{Macrodef}{Else}{\OArg{comment}\AArg{options}}{}
355 This macro defines the \Keyword{else} part of the \MacroRef{If} statement.
356
357 \BlockOptionsText
358 \end{Macrodef}
359
360 \begin{Macrodef}{Elsif}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
361 \Macro{ElsIf} defines the \MacroRef{If} chaining. The argument \Argument{text} is the new condition.
362
363 \BlockOptionsText
364 \end{Macrodef}
365
366 \subsubsection{The \Keyword{switch} block}
367
368 %! formatter = off
369 \begin{PDExample}
370 \begin{algorithmic}
371 \Statep{Get \Id{Optiondef}}
372 \Switch{\Id{Optiondef}}
373 \Case{1}[inserts new record]
374 \Statep{\Call{Insert}{\Id{record}}}
375 \EndCase
376 \Case{2}[deletes a record]
377 \Statep{\Call{Delete}{\Id{key}}}
378 \EndCase
379 \Otherwise
380 \Statep{Print ``invalid option''}
381 \EndOtherwise
382 \EndSwitch
383 \end{algorithmic}
384 \end{PDExample}
385 %! formatter = on
386
387
388 \begin{Macrodef}{Switch}{\OArg{comment*}\MArg{expression}\OArg{comment}\AArg{options}}{}
389 The \Macro{Switch} is closed by a matching \MacroRef{EndSwitch}.
390
391 \BlockOptionsText
392 \end{Macrodef}
393
394 \begin{Macrodef}{EndSwitch}{\OArg{comment}\AArg{options}}{}
395 This macro closes a \MacroRef{Switch} block.
396
397 \MacroOptionsText
398 \end{Macrodef}
399
400 \begin{Macrodef}{Case}{\OArg{comment*}\MArg{constant-list}\OArg{comment}\AArg{options}}{}
401 When the result of the \Keyword{switch} expression matches one of the constants in \Argument{constants-list}, then the \Keyword{case} is executed. Usually the \Argument{constant-list} is a single constant, a comma-separated list of constants or some kind of range specification.
402
403 \BlockOptionsText
404 \end{Macrodef}
405
406 \begin{Macrodef}{EndCase}{\OArg{comment}\AArg{options}}{}
407 This macro closes a corresponding \MacroRef{Case} statement.
408
409 \MacroOptionsText
410 \end{Macrodef}
411
412 \begin{Macrodef}{Otherwise}{\OArg{comment}\AArg{options}}{}
413 A \Keyword{switch} structure can optionally use an \Keyword{otherwise} clause, which is executed when no previous \Keyword{case}s had a hit.
414
415 \BlockOptionsText
416 \end{Macrodef}
417
418 \begin{Macrodef}{EndOtherwise}{\OArg{comment}\AArg{options}}{}
419 This macro closes a corresponding \MacroRef{Otherwise} statement.
420
421 \MacroOptionsText
422 \end{Macrodef}
423
424 \subsubsection{The \Keyword{for} block}
425 The \Keyword{for} loop uses \Macro{For} and is also flavored with two variants: \Keyword{foreach} (\Macro{ForEach}) and \Keyword{forall} (\Macro{ForAll}).
426
427 %! parser = off
428 \begin{PDExample}
429 \begin{algorithmic}
430 \For{$i \gets 0$ \To $n$}
431 \Statep{Do something with $i$}
432 \EndFor
433 \ForAll{$\Id{item} \in C$}
434 \Statep{Do something with \Id{item}}
435 \EndFor
436 \ForEach{\Id{item} in queue $Q$}
437 \Statep{Do something with \Id{item}}
438 \EndFor
439 \end{algorithmic}
440 \end{PDExample}
441 %! parser = on
442
443
444 \begin{Macrodef}{For}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
445 The \Argument{text} is used to establish the loop scope.
446
447 \BlockOptionsText
448 \end{Macrodef}
449
450 \begin{Macrodef}{EndFor}{\OArg{comment}\AArg{Optiondef}}{}
451 This macro closes a corresponding \MacroRef{For}, \MacroRef{ForEach} or \MacroRef{ForAll}.
452
453 \MacroOptionsText
454 \end{Macrodef}
455
456 \begin{Macrodef}{ForEach}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
457 Same as \MacroRef{For}.
458 \end{Macrodef}
459
460 \begin{Macrodef}{ForAll}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
461 Same as \MacroRef{For}.
462 \end{Macrodef}
463
464 \subsubsection{The \Keyword{while} block}
465 \Macro{While} is the loop with testing condition at the top.
466
467 \begin{PDExample}
468 \begin{algorithmic}
469 \While{$n > 0$}
470 \Statep{Do something}
471 \Statep{$n \gets n - 1$}
472 \EndWhile
473 \end{algorithmic}
474 \end{PDExample}
475
476 \begin{Macrodef}{While}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
477 In \Argument{text} is the boolean expression that, when \False, will end the loop.
478
479 \BlockOptionsText
480 \end{Macrodef}
481
482 \begin{Macrodef}{EndWhile}{\OArg{comment}\AArg{options}}{}
483 This macro closes a matching \MacroRef{While} block.
484
485 \MacroOptionsText
486 \end{Macrodef}
487
488 \subsubsection{The \Keyword{repeat}-\Keyword{until} block}
489 The loop with testing condition at the bottom is the \Macro{Repeat}/\Macro{Until} block.
490
491 \begin{PDExample}
492 \begin{algorithmic}
493 \Repeat
494 \Statep{Do something}
495 \Statep{$n \gets n - 1$}
496 \Until{$n \leq 0$}
497 \end{algorithmic}
498 \end{PDExample}
499
500 \begin{Macrodef}{Repeat}{\OArg{comment}\AArg{options}}{}
501 This macro starts the \Keyword{repeat} loop, which is closed with \MacroRef{Until}.
502
503 \BlockOptionsText
504 \end{Macrodef}
505
506 \begin{Macrodef}{Until}{\OArg{comment*}\MArg{text}\OArg{comment}\AArg{options}}{}
507 In \Argument{text} is the boolean expression that, when \MacroRef{True}, will end the loop.
508
509 \MacroOptionsText
510 \end{Macrodef}
511
512 \subsubsection{The \Keyword{loop} block}
513 A generic loop is build with \Macro{Loop}.
514
515 \begin{PDExample}
516 \begin{algorithmic}
517 \Loop
518 \Statep{Do something}
519 \Statep{$n \gets n + 1$}
520 \If{$n$ is multiple of 5}
521 \Statep{\Continue}[restarts loop]
522 \EndIf
523 \Statep{Do something else}
524 \If{$n \leq 0$}
525 \Statep{\Break}[ends loop]
526 \EndIf
527 \Statep{Keep working}
528 \EndLoop
529 \end{algorithmic}
530 \end{PDExample}
531
532 \begin{Macrodef}{Loop}{\OArg{comment}\AArg{options}}{}
533 The generic loop starts with \Macro{Loop} and ends with \MacroRef{EndLoop}. Usually the infinite loop is interrupted by and internal \MacroDef{Break} or restarted with \MacroDef{Continue}.
534
535 \BlockOptionsText
536 \end{Macrodef}
537
538 \begin{Macrodef}{EndLoop}{\OArg{comment}\AArg{options}}{}
539 \Macro{EndLoop} closes a matching \MacroRef{Loop} block.
540
541 \MacroOptionsText
542 \end{Macrodef}
543
544 \subsection{Constants and Identifiers}\label{sec:constants-and-identifiers}
545 A few macros for well known constants were defined: \MacroDef{True} (\True), \MacroDef{False} (\False), and \MacroDef{Nil} (\Nil).
546
547 The macro \Macro{Id} was created to handle ``program-like'' named identifiers, such as \Id{sum}, \Id{word_counter} and so on.
548
549 \begin{Macrodef}{Id}{\MArg{identifier}}{}
550 \PDAddChange{0.91}{
551 update,
552 description = {\Macro{Id} has been recoded to work in both text and math modes, preventing hyphenation},
553 no box,
554 }%
555 \PDAddChange{0.99.2a}{
556 update,
557 description = {\MacroRef{Id} updated to natively handle the underscore character without escaping.},
558 }%
559 Identifiers are emphasised: \PDInline!\Id{my_value}! is \Id{my_value}. Its designed to work in both text and math modes: \PDInline!$\Id{offer}_k$! is $\Id{offer}_k$.
560 \end{Macrodef}
561
562 \subsection{Assignments and I/O}\label{sec:assignments-and-i/o}
563
564 To support teaching-like, basic pseudocode writing, the macros \MacroDef{Read} and \MacroDef{Write} are provided.
565
566 \begin{PDExample}
567 \begin{algorithmic}
568 \Statep{\Read $v_1, v_2$}
569 \Statep{$\Id{mean} \gets \dfrac{v_1 + v_2}{2}$}[calculate]
570 \Statep{\Write \Id{mean}}
571 \end{algorithmic}
572 \end{PDExample}
573
574 The \Macro{Set} macro, although obsolete, can be used for assignments.
575
576 \begin{Macrodef}{Set}{\MArg{lvalue}\MArg{expression}}{}
577 \PDAddChange{0.91}{
578 description = \Macro{Set} can be used for assignments.,
579 no box,
580 }%
581 \PDAddChange{0.99}{
582 deprecation,
583 description = {\Macro{Set} is deprecated and will no longer be supported},
584 }%
585 This macro expands to \MacroRef{Id}\PDInline!{#1} \gets #2!.
586
587 It will no longer be supported but will be retained as-is for backward compatibility. Since proper handling of text and math modes is needed and its usage offers no clear benefit, there is no justification to keep it.
588 \end{Macrodef}
589
590 \PDAddChange{0.99}{
591 removal,
592 title = \Macro{Setl},
593 description = {\Macro{Setl} has been removed from the package},
594 }%
595 The macro \MacroRefInd{Setl} has been removed.
596
597 \subsection{Procedures and Functions}\label{sec:procedures-and-functions}
598 Modularization uses \Macro{Procedure} or \Macro{Function}.
599
600 %! formatter = off
601 \begin{PDExample}
602 \begin{algorithmic}
603 \Procedure{SaveNode}{\Id{node}}[saves a B\textsuperscript{+}-tree node to disk]
604 \If{\Id{node}.\Id{is_modified}}
605 \If{$\Id{node}.\Id{address} = -1$}
606 \Statep{Set file writting position after file's last byte}[creates a new node on disk]
607 \Else
608 \Statep{Set file writting position to \Id{node}.\Id{address}}[updates the node]
609 \EndIf
610 \Statep{Write \Id{node} to disk}
611 \Statep{$\Id{node}.\Id{is_modified} \gets \False$}
612 \EndIf
613 \EndProcedure
614 \end{algorithmic}
615 \end{PDExample}
616
617 \begin{PDExample}
618 \begin{algorithmic}
619 \Function{Factorial}{$n$}[$n \geq 0$]
620 \If{$n \in \{0, 1\}$}
621 \Statep{\Return $1$}[base case]
622 \Else
623 \Statep{\Return $n \times \Call{Factorial}{n-1}$}[recursive case]
624 \EndIf
625 \EndFunction
626 \end{algorithmic}
627 \end{PDExample}
628 %! formatter = on
629
630 \begin{Macrodef}{Procedure}{\MArg{name}\MArg{argument list}\OArg{comment}\AArg{options}}{}
631 This macro creates a \Keyword{procedure} block that must be ended with \MacroRef{EndProcedure}.
632
633 \BlockOptionsText
634 \end{Macrodef}
635
636 \begin{Macrodef}{EndProcedure}{\OArg{comment}\AArg{optons}}{}
637 This macro closes the \MacroRef{Procedure} block.
638
639 \MacroOptionsText
640 \end{Macrodef}
641
642 \begin{Macrodef}{Function}{\MArg{name}\MArg{argument list}\OArg{comment}\AArg{options}}{}
643 This macro creates a \Keyword{function} block that must be ended with \MacroRef{EndFunction}. A \MacroDef{Return} is defined.
644
645 \BlockOptionsText
646 \end{Macrodef}
647
648 \begin{Macrodef}{EndFunction}{\OArg{comment}\AArg{optons}}{}
649 This macro closes the \MacroRef{Function} block.
650
651 \MacroOptionsText
652 \end{Macrodef}
653
654 For calling a procedure or function, \Macro{Call} should be used.
655
656 \begin{Macrodef}{Call}{\MArg{name}\MArg{arguments}\AArg{options}}{}
657 \label{call}
658 \Macro{Call} is used to state a function or procedure call. The module's \Argument{name} and \Argument{arguments} are mandatory.
659
660 \MacroOptionsText
661 \end{Macrodef}
662
663 \subsection{Comments}\label{sec:comments}
664
665 The \Macro{Comment} macro, as defined by the \PackageName{algorithmicx} package, retains its original behavior and has been redefined to support styling options.
666
667 \begin{Macrodef}{Comment}{\MArg{text}\AArg{options}}{}
668 The updated \Macro{Comment} can be used with \Macro{State}, \Macro{Statex}, and \MacroRef{Statep}. When used with \MacroRef{Statep}, it should be enclosed within the text braces, though multi-line statements may function differently than expected.
669
670 \MacroOptionsText
671 \end{Macrodef}
672
673 %! formatter = off
674 \begin{PDExample}
675 \begin{minipage}{7.5cm}
676 \begin{algorithmic}
677 <comment color = blue>% for viewing purposes only
678 \State Assign the value zero to the variable $x$\Comment{first assignment}
679 \Statep{Assign the value zero to the variable $x$\Comment{first assignment}}
680 \Statep{Assign the value zero to the variable $x$}[first assignment]% best choice
681 \end{algorithmic}
682 \end{minipage}
683 \end{PDExample}
684 %! formatter = on
685
686 \begin{Macrodef}{Commentl}{\MArg{text}\AArg{options}}{}
687 While \MacroRef{Comment} pushes text to the end of the line, the \Macro{Commentl} macro is ``local'' -- it simply places a comment without altering the line structure.
688
689 Local comments follows regular text and no line changes are checked.
690
691 \MacroOptionsText
692 \end{Macrodef}
693
694 \begin{PDExample}
695 \begin{algorithmic}
696 \If{$a > 0$~~\Commentl{special case}\\
697 or\\
698 $a < b$~~\Commentl{general case}\\}
699 \Statep{Process data~~\Commentl{may take a while}}
700 \EndIf
701 \end{algorithmic}
702 \end{PDExample}
703
704 \begin{Macrodef}{CommentIn}{\MArg{text}\AArg{options}}{}
705 \Macro{CommentIn} is an alternative to \textit{line comments}, which typically extend to the end of the line. This macro defines a comment with both a beginning and an end. A comment starts with \CommentSymbol\ and ends with \CommentSymbolRight.
706
707 \MacroOptionsText
708 \end{Macrodef}
709
710
711 \begin{PDExample}
712 \begin{algorithmic}
713 \If{$a > 0$ \CommentIn{special case} or $a < b$ \CommentIn{general case}}
714 \Statep{Process data~~\Commentl{may take a while}}
715 \EndIf
716 \end{algorithmic}
717 \end{PDExample}
718
719 \subsection{Documentation}\label{sec:documentation}
720 A series of macros are defined to provide the header documentation for a pseudocode.
721
722 \begin{PDExample}
723 \begin{algorithmic}
724 \Description Calculation of the factorial of a natural number through successive multiplications
725 \Require $n \in \mathbb{N}$
726 \Ensure $f = n!$
727 \end{algorithmic}
728 \end{PDExample}
729
730 \begin{Macrodef}{Description}{~\Argument{description text}}{}
731 The \Macro{Description} is intended to hold the general description of the pseudocode.
732 \end{Macrodef}
733
734 \begin{Macrodef}{Require}{~\Argument{pre-conditions}}{}
735 The required initial state that the code relies on. These are \textit{pre-conditions}.
736 \end{Macrodef}
737
738 \begin{Macrodef}{Ensure}{~\Argument{post-conditions}}{}
739 The final state produced by the code. These are \textit{post-conditions}.
740 \end{Macrodef}
741
742 \begin{PDExample}
743 \begin{algorithmic}
744 \Description Calculation of the factorial of a natural number through successive multiplications
745 \Input $n$ (integer)
746 \Output $n!$ (integer)
747 \end{algorithmic}
748 \end{PDExample}
749
750 \begin{Macrodef}{Input}{~\Argument{inputs}}{}
751 This works as an alternative to \MacroRef{Require}, presenting \Keyword{input}.
752 \end{Macrodef}
753 \begin{Macrodef}{Output}{~\Argument{outputs}}{}
754 This works as an alternative to \MacroRef{Ensure}, presenting \Keyword{output}.
755 \end{Macrodef}
756
757
758 \section{Customization and Fine Tunning}\label{sec:customization-and-fine-tunning}
759 Starting from version 0.99 of \PackageName{algxpar}, a set of options has been introduced to customize the presentation of algorithms. For instance, colors and fonts specific to keywords can now be specified, offering a more convenient way to tailor each algorithm.
760
761 The \MacroRef{AlgSet} macro fulfills this purpose.
762
763 \begin{Macrodef}{AlgSet}{\MArg{options list}}{}
764 This macro sets algorithmic settings as specified in the \Argument{options list}, which is key/value comma-separated list.
765
766 All settings will be applied to the entire document from the point where the macro is called. To limit the scope of a definition made with \MacroRef{AlgSet} to a specific part of the document, simply enclose it in a \TeX\ group.
767 \end{Macrodef}
768
769 \begingroup
770 \begin{PDExample}
771 \AlgSet{algorithmic indent = 1.5cm}
772 \begin{algorithmic}
773 \Statep{\Read $k$}
774 \If{$k < 0$}
775 \Statep{$k \gets -k$}
776 \EndIf
777 \Statep{\Write $k$}
778 \end{algorithmic}
779 \end{PDExample}
780 \endgroup% ends the scope
781
782 If the settings are only applied to a single algorithm and not a group of algorithms in a text section, the easiest way is to include the options in the \texttt{algorithmicx} environment.
783
784 %! formatter = off
785 \begin{PDExample}
786 \begin{algorithmic}
787 <keyword font = \sffamily\bfseries\itshape>
788 \Statep{\Read $k$}
789 \If{$k < 0$}
790 \Statep{$k \gets -k$}
791 \EndIf
792 \Statep{\Write $k$}
793 \end{algorithmic}
794 \end{PDExample}
795 %! formatter = on
796
797 Named styles can also be defined using the \PackageName{pgfkeys} syntax.
798
799 %! formatter = off
800 \begin{PDExample}
801 \AlgSet{
802 fancy/.style = {
803 text color = green!40!black,
804 keyword color = blue!75!black,
805 comment color = brown!80!black,
806 comment symbol = \texttt{//},
807 }
808 }
809 \begin{algorithmic}
810 <fancy>
811 \Statep{\Commentl{Process $k$}}
812 \Statep{\Read $k$}
813 \If{$k < 0$}
814 \Statep{$k \gets -k$}[back to positive]
815 \EndIf
816 \Statep{\Write $k$}
817 \end{algorithmic}
818 \end{PDExample}
819 %! formatter = on
820
821 Sometimes some settings need to be applied exclusively to one command, for example to highlight a segment of the algorithm.
822
823 %! formatter = off
824 \begin{PDExample}
825 \AlgSet{
826 highlight/.style = {
827 text color = red!60!black,
828 keyword color = red!60!black,
829 }
830 }
831 \begin{algorithmic}
832 \Statep{\Commentl{Process $k$}}
833 \Statep{\Read $k$}
834 \If{$k < 0$}
835 <highlight>
836 \Statep{$k \gets -k$}[back to positive]
837 \EndIf
838 \Statep{\Write $k$}
839 \end{algorithmic}
840 \end{PDExample}
841 %! formatter = on
842
843 \subsection{Options}\label{sec:options}
844 \PDAddChange{0.99}{
845 description = {Added support to style the main components of the pseudocode, such as keywords, comments and text and widths.},
846 no box,
847 }%
848 This section presents the options that can be specified for the algorithms, either using \MacroRef{AlgSet} or the \Argument{options} parameter of the various macros.
849
850 \begin{Optiondef}{indent lines}{\PDInline{true} | \PDInline{false}}{Default: \PDInline{true}; initially: \PDInline{false}}
851 \PDAddChange{0.99.1}{
852 description = Added experimental support to show vertical indentantion lines,
853 }%
854 The \Option{indent lines} option displays vertical indentation lines in the pseudocode. This feature works properly only if the start and end of the block are on the same page. Additionally, at least two compilation steps are needed for the lines to be correctly positioned.
855
856 To change the style for the lines, use \OptionRef{line style}
857
858 \textit{This feature is still experimental and incomplete.}
859 \end{Optiondef}
860
861 %! formatter = off
862 \begin{PDExample}
863 \begin{algorithmic}
864 <indent lines>
865 \For{$i \gets 0$ \To $N - 1$}
866 \For{$j \gets$ \To $N - 1$}
867 \If{$m_{ij} < 0$}
868 \Statep{$m_{ij} \gets 0$}
869 \EndIf
870 \EndFor
871 \EndFor
872 \end{algorithmic}
873
874 \begin{algorithmic}
875 <indent lines, noend>
876 \For{$i \gets 0$ \To $N - 1$}
877 \For{$j \gets$ \To $N - 1$}
878 \If{$m_{ij} < 0$}
879 \Statep{$m_{ij} \gets 0$}
880 \EndIf
881 \EndFor
882 \EndFor
883 \end{algorithmic}
884 \end{PDExample}
885 %! formatter = on
886
887 \begin{Optiondef}{line style}{\Argument{style}}{}
888 \PDAddChange{0.99.1}{
889 description = {\Option{line style} option added to style indent lines.},
890 }%
891 Almost anything Ti\emph{k}Z can apply to a line can be used to set the indentation lines.
892
893 To show indentation lines in algorithms, \OptionRef{indent lines} must be set to true.
894 \end{Optiondef}
895
896 %! formatter = off
897 \begin{PDExample}
898 \begin{algorithmic}
899 <indent lines, line style = {orange, dotted, -latex, ultra thick}>
900 \For{$i \gets 0$ \To $N - 1$}
901 \For{$j \gets$ \To $N - 1$}
902 \If{$m_{ij} < 0$}
903 \Statep{$m_{ij} \gets 0$}
904 \EndIf
905 \EndFor
906 \EndFor
907 \end{algorithmic}
908 \end{PDExample}
909 %! formatter = on
910
911
912 \begin{Optiondef}{language}{\Argument{language}}{}
913 This key is used to select the keyword language set for the current scope. The language keyword set should have already been loaded via the package options (see \cref{sec:package-usage-and-options}).
914 \end{Optiondef}
915
916 \begin{Optiondef}{noend}{}
917 Structured algorithms use blocks to define their structures, marking both the beginning and the end. In pseudocode, it is common to use a line to indicate the end of a block. The \Option{end} option suppresses this line.
918
919 The result resembles a program written in Python.
920 \end{Optiondef}
921
922
923 \begin{Optiondef}{end}{}
924 This option reverses the behaviour of \OptionRef{noend}, and the closing line of a block presented.
925 \end{Optiondef}
926
927 %! formatter = off
928 \begin{PDExample}
929 \begin{algorithmic}
930 <noend>
931 \For{$i \gets 0$ \To $N - 1$}
932 \For{$j \gets$ \To $N - 1$}
933 \If{$m_{ij} < 0$}
934 <end>
935 \Statep{$m_{ij} \gets 0$}
936 \EndIf
937 \EndFor
938 \EndFor
939 \end{algorithmic}
940 \end{PDExample}
941 %! formatter = on
942
943 \begin{Optiondef}{keywords}{\Argument{list of keywords assignments}}{}
944 This option allows you to modify an existing keyword or define a new one. For more information on keywords and translations, see \cref{sec:languages-and-translations}.
945 \end{Optiondef}
946
947 %! formatter = off
948 \begin{PDExample}
949 \begin{algorithmic}
950 <
951 keywords = {
952 terminate = Terminate, % new keyword
953 then = \{, % redefined
954 endif = \}, % redefined
955 while = whilst, % redefined
956 }
957 >
958 \While{\True}
959 \If{$t < 0$}
960 \Statep{Run the \Keyword{terminate} module}
961 \EndIf
962 \EndWhile
963 \end{algorithmic}
964 \end{PDExample}
965 %! formatter = on
966
967 \begin{Optiondef}{algorithmic indent}{\Argument{width}}{Initially: \PDInline{1.5em}}
968 The algorithmic indent is the amount of horizontal space used for indentation inner commands.
969
970 This option actually sets the \PackageName{algorithmicx}'s \Macro{algorithmicindent}.
971 \end{Optiondef}
972
973 \begin{Optiondef}{comment symbol}{\Argument{symbol}}{Initially: \PDInline{$\triangleright$}}
974 The default symbol that preceeds the text in comments is \CommentSymbol, as used by \PackageName{algorithmicx}, and can be changed with this key.
975
976 The current comment symbol is available with \MacroDef{CommentSymbol}. Do not change this symbol by redefining \Macro{CommentSymbol}, as font, shape and color settings will no longer be respected. Always use \Option{comment symbol}.
977 \end{Optiondef}
978
979 \begin{Optiondef}{comment symbol right}{\Argument{symbol}}{Initially: \PDInline{$\triangleleft$}}
980 This symbol closes a \MacroRef{CommentIn}. It is set to \CommentSymbolRight\ and can be accessed using the \MacroDef{CommentSymbolRight} macro. Avoid changing the symbol by redefining \Macro{CommentSymbolRight}, as this would cause font, shape, and color settings to be ignored. Always use the \Option{comment symbol right} option.
981 \end{Optiondef}
982
983 \subsubsection{Fonts, shapes and sizes}
984 The options ins this section allows setting font family, shape, weight and size for several parts of an algorithm.
985
986 Notice that color are handled separately (see \cref{sec:colors}) and using \Macro{color} with font options will tend to break the document.
987
988 \begin{Optiondef}{text font}{\Argument{font, shape and size}}{Initially: empty}
989 This setting corresponds to the font family, its shape and size and applies to the \Argument{text} field in each of the commands.
990 \end{Optiondef}
991 %
992 \begin{Optiondef}{comment font}{\Argument{font, shape and size}}{Initially: \PDInline{\slshape}}
993 This setting corresponds to the font family, its shape and size and applies to all comments.
994 \end{Optiondef}
995
996 \begin{Optiondef}{keyword font}{\Argument{font, shape and size}}{Initially: \PDInline{\bfseries}}
997 This setting sets the font family, shape, and size, and applies to all keywords, such as \Keyword{function} or \Keyword{end}.
998 \end{Optiondef}
999
1000 \begin{Optiondef}{constant font}{\Argument{font, shape and size}}{Initially: \PDInline{\scshape}}
1001 This setting sets the font family, shape, and size, and applies to all constants, such as \Nil, \True\ and \False.
1002
1003 This setting also applies when \MacroRef{Constant} is used.
1004 \end{Optiondef}
1005
1006 \begin{Optiondef}{module font}{\Argument{font, shape and size}}{Initially: \PDInline{\scshape}}
1007 This setting sets the font family, shape, and size, and applies to both procedure and function identifiers, as well as their callings with \MacroRef{Call}.
1008 \end{Optiondef}
1009
1010 \subsubsection{Colors}\label{sec:colors}
1011 Colors are defined using the \PackageName{xcolors} package.
1012
1013 \begin{Optiondef}{text color}{\Argument{color}}{Initially: \PDInline{.}}
1014 This setting corresponds to the color that applies to the \Argument{text} field in each of the commands.
1015 \end{Optiondef}
1016
1017 \begin{Optiondef}{comment color}{\Argument{color}}{Initially: \PDInline{.!70}}
1018 This setting corresponds to the color that applies to all comments.
1019 \end{Optiondef}
1020
1021 \begin{Optiondef}{keyword color}{\Argument{color}}{Initially: \PDInline{.}}
1022 This key is used to set the color for all keywords.
1023 \end{Optiondef}
1024
1025 \begin{Optiondef}{constant color}{\Argument{color}}{Initially: \PDInline{.}}
1026 This setting corresponds to the color that applies to the defined constant (see \cref{sec:constants-and-identifiers}) and also when macro \MacroRef{Constant} is used.
1027 \end{Optiondef}
1028
1029 \begin{Optiondef}{module color}{\Argument{color}}{Initially: \PDInline{.}}
1030 This color is applied to the identifier used in both \MacroRef{Procedure} and \MacroRef{Function} definitions, as well as module calls with \MacroRef{Call}. Notice that the arguments use \Option{text color}.
1031 \end{Optiondef}
1032
1033 \subsubsection{Paragraphs}
1034 Multi-line support are internally handled by \Macro{parbox}es.
1035 \PDAddChange{0.99.2}{
1036 update,
1037 description = {Wrong spacing between parboxes were fixed.},
1038 no box,
1039 }
1040
1041 \medskip
1042 %! formatter = off
1043 \begingroup
1044 \begin{algorithmic}
1045 <show boxes>
1046 \Procedure{Euclid}{$a,b$}[The g.c.d. of a and b]
1047 \Statep{$r\gets a\bmod b$}
1048 \While{$r\not=0$}[We have the answer if r is 0]
1049 \Statep{$a\gets b$}
1050 \Statep{$b\gets r$}
1051 \Statep{$r\gets a\bmod b$}
1052 \EndWhile
1053 \Statep{\Keyword{return} $b$}[The g.c.d. is b]
1054 \EndProcedure
1055 \end{algorithmic}
1056 \endgroup
1057 %! formatter = on
1058
1059 \medskip
1060 The options in this section should be used to set how these paragraphs will be presented.
1061
1062 \begin{Optiondef}{text style}{\Argument{style}}{Initially: \PDInline{\RaggedRight}}
1063 This \Argument{style} is applied to the paragraph box that holds the \Argument{text} field in all commands.
1064 \end{Optiondef}
1065
1066 \begin{Optiondef}{comment style}{\Argument{style}}{Initially: \PDInline{\RaggedRight}}
1067 This \Argument{style} is applied to the paragraph box that holds the \Argument{comment} field in all algorithmic commands. This setting will not be used with \MacroRef{Comment}, \MacroRef{Commentl} or \MacroRef{CommentIn}.
1068 \end{Optiondef}
1069
1070 \begin{Optiondef}{comment separator width}{\Argument{width}}{Initially: \PDInline{1em}}
1071 The minimum space between the text box and the \Macro{CommentSymbol}. This affects the available space in a line for keywords, text and comment.
1072 \end{Optiondef}
1073
1074 \begin{Optiondef}{statement indent}{\Argument{width}}{Initially: \PDInline{1em}}
1075 This is the \Macro{hangindent} set inside \MacroRef{Statep} statements.
1076 \end{Optiondef}
1077
1078 \begin{Optiondef}{comment width}{\PDInline{auto}|\PDInline{nice}|\Argument{width}}{Initially: \PDInline{auto}}
1079 There are two ways to balance the lengths of \Argument{text} and \Argument{comments} on a line, each providing different visual experiences.
1080
1081 In automatic mode (\PDInline{auto}), the balance is chosen considering the widths that the actual text and comment have, trying to reduce the total number of lines, given there is not enough space in a single line for the keywords, text , comment and comment symbol. The consequence is that each line with a comment will have its own balance.
1082
1083 The second mode, \PDInline{nice}, sets a fixed width for the entire algorithm, maintaining consistency across all comments. In that case, longer comments will tend to span a larger number of lines. The ``nice value'' is hardcoded and sets the comment width to \PDInline{0.4\linewidth}.
1084
1085 Also, a fixed comment \Argument{width} can be specified.
1086 \end{Optiondef}
1087
1088 % todo: insert the parameter indent when it starts working
1089 \begin{comment}
1090 parameter indent = 0pt,
1091 \end{comment}
1092
1093 \subsection{Languages and translations}\label{sec:languages-and-translations}
1094 \PDAddChange{0.99}{
1095 description = {New mechanism to translation with separate files for each language},
1096 no box,
1097 }%
1098 A simple mechanism is employed to allow keywords to be translated into other languages.
1099
1100 %! formatter = off
1101 \begingroup
1102 \begin{PDExample}
1103 \begin{algorithmic}
1104 <language = brazilian>
1105 \Procedure{Euclid}{$a,b$}
1106 \State $r\gets a\bmod b$
1107 \While{$r\not=0$}
1108 \State $a\gets b$
1109 \State $b\gets r$
1110 \State $r\gets a\bmod b$
1111 \EndWhile
1112 \Statep{\Keyword{return} $b$}
1113 \EndProcedure
1114 \end{algorithmic}
1115 \end{PDExample}
1116 \endgroup
1117 %! formatter = on
1118
1119
1120 Creating a new keyword set uses the \Macro{AlgLanguageSet} macro.
1121
1122 \begin{Macrodef}{AlgLanguageSet}{\MArg{language name}\MArg{keyword assignments}}{}
1123 This macro assigns new values to existing keywords as well as creates new ones. Once defined, keywords cannot be removed.
1124
1125
1126 If a default keyword is not redefined, the English version will be used.
1127
1128
1129 To create a new set, copy the file \FileName{algxpar-english.kw.tex} and modify it as needed.
1130
1131
1132 Note that there is a set of keywords for the lines that close each block. These keys are included to offer more flexibility in customizing how these lines are displayed. It is strongly recommended to use the {Keyword} macro for references to other keywords, so that font, color, and language changes can be made without issues.
1133
1134
1135 In translations, these \textit{compound keywords} are not necessarily required (see the file \FileName{brazilian.kw.tex}, which follows the settings in \FileName{algxpar-english.kw.tex}). However, if they are defined, there will be different versions for each language.
1136 \end{Macrodef}
1137
1138 % \begin{figure}
1139 \tcbinputlisting{example, listing file = algxpar-english.kw.tex, listing only}
1140 % \end{figure}
1141
1142 The mechanism behind \MacroRef{AlgLanguageSet} utilizes the \Macro{SetKeyword} macro, which is called to modify the value of a single keyword\footnote{Macros such as \Macro{algorithmicwhile} from the \PackageName{algorithmicx} package are no longer in use.}. To retrieve the value of a specific keyword, the \Macro{Keyword} macro should be used. It produces the formatted value based on the options currently applied to keywords.
1143
1144 \begin{Macrodef}{SetKeyword}{\OArg{language}\MArg{keyword}\MArg{value}}{}
1145 The macro \MacroRef{SetKeyword} changes a given \Argument{keyword} to \Argument{value} if it exists; otherwise a new keyword is created.
1146
1147 If \Argument{language} is omitted, the language currently in use is changed.
1148
1149 See also the \OptionRef{keywords} option.
1150 \end{Macrodef}
1151
1152 \begin{Macrodef}{Keyword}{\OArg{language}\MArg{keyword}}{}
1153 This macro expands to the value of a keyword in a \Argument{language} using the font, shape, size, and color determined for the keyword set.
1154
1155 If \Argument{language} is not specified, the current language is used. \Argument{keyword} is any keyword defined for a language, including custom ones.
1156 \end{Macrodef}
1157
1158 \begin{PDExample}
1159 \SetKeyword[german]{if}{wenn} % new
1160 Depending on the language, a keyword can take different forms: \Keyword{if} (English), \Keyword[german]{if} (German) or \Keyword[brazilian]{if} (Brazilian Portuguese).
1161 \end{PDExample}
1162
1163 \begin{PDExample}
1164 \AlgLanguageSet{spanish}{
1165 while = mientras,
1166 do = hacer,
1167 endwhile = fin,
1168 if = si,
1169 then = entonces,
1170 else = en otro caso,
1171 endif = fin,
1172 }
1173 \begin{algorithmic}
1174 <language = spanish, indent lines>
1175 \If{$a > b$}[check conditions]
1176 \While{$a > 0$}
1177 \Statep{\Call{Process}{$a$}}[process current data]
1178 \EndWhile
1179 \Else
1180 \Statep{\Call{Signal}{\Id{pid}}}
1181 \EndIf
1182 \end{algorithmic}
1183 \end{PDExample}
1184
1185 \subsection{Other features}
1186
1187 \PDAddChange{0.99}{
1188 description = Added support to style named constants and function/procedures identifiers,
1189 no box,
1190 }%
1191 These macros were added to support the styles defined for named constants and function and procedure identifiers.
1192
1193 \begin{Macrodef}{Constant}{\MArg{name}}{}
1194 This macro presents \Argument{name} using font, shape, size and color defined for constants.
1195 \end{Macrodef}
1196
1197 \begin{Macrodef}{Module}{\MArg{name}}{}
1198 This macro presents \Argument{name} using font, shape, size and color defined for procedures and functions.
1199 \end{Macrodef}
1200
1201
1202 \section{``Forgotten'' features}
1203
1204 \PDAddChange{0.99}{
1205 removal,
1206 description = {Macros \Macro{TextString} and \Macro{VisibleSpace} accidentally removed.},
1207 no box,
1208 }%
1209 Some features were forgotten from v0.91 to v.99, although it was not intentional. The macros \Macro{TextString} and \Macro{VisibleSpace} simply vanished into thin air.
1210
1211
1212 \section{To do}
1213 This is a \textit{todo} list:
1214 \begin{itemize}
1215 \item Add font, shape, size and color settings to a whole algorithm;
1216 \item Add font, shape, size and color settings to line numbers;
1217 \item Add font, shape, size and color settings to identifiers.
1218 \end{itemize}
1219
1220
1221 \section{Examples}
1222
1223 \subsection{LZW revisited}
1224 \begingroup
1225 \begin{PDListing}
1226 \AlgSet{
1227 comment color = purple,
1228 comment width = nice,
1229 comment style = \raggedleft,
1230 }
1231 \end{PDListing}
1232
1233 \AlgSet{
1234 comment color = purple,
1235 comment width = nice,
1236 comment style = \raggedleft,
1237 }
1238 \input{algxpar-lzw.tex}
1239 \endgroup
1240
1241 \subsection{LZW revisited again}
1242 \begingroup
1243 \begin{PDListing}
1244 \AlgSet{
1245 keyword font = \ttfamily,
1246 keyword color = green!40!black,
1247 text font = \itshape,
1248 comment font = \footnotesize,
1249 algorithmic indent = 1.5em,
1250 indent lines,
1251 noend,
1252 }
1253 \end{PDListing}
1254
1255 \AlgSet{
1256 keyword font = \ttfamily,
1257 keyword color = green!40!black,
1258 text font = \itshape,
1259 comment font = \footnotesize,
1260 algorithmic indent = 1.5em,
1261 indent lines,
1262 noend,
1263 }
1264 \noindent%
1265 \begin{minipage}{\linewidth}
1266 \input{algxpar-lzw.tex}
1267 \end{minipage}
1268 \endgroup
1269
1270
1271 % Index
1272 \printindex
1273
1274 \end{document}
1275 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%