-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithmicx.tex
1768 lines (1501 loc) · 45.9 KB
/
algorithmicx.tex
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{article}
\usepackage{algorithmicx}
\usepackage[ruled]{algorithm}
\usepackage{algpseudocode}
\usepackage{algpascal}
\usepackage{algc}
%\algdisablelines
\newcommand{\alg}{\texttt{algorithmicx}}
\newcommand{\old}{\texttt{algorithmic}}
\newcommand{\euk}{Euclid}
\newcommand\ASTART{\bigskip\noindent\begin{minipage}[b]{0.5\linewidth}}
\newcommand\ACONTINUE{\end{minipage}\begin{minipage}[b]{0.5\linewidth}}
\newcommand\AENDSKIP{\end{minipage}\bigskip}
\newcommand\AEND{\end{minipage}}
\title{The \alg\ package\footnote{This is the documentation for the version 1.2
of the package. This package is released under LPPL.}}
\author{Sz\'asz J\'anos\\[email protected]}
\begin{document}
\maketitle
\begin{abstract}
The \alg\ package provides many possibilities to customize the layout of algorithms.
You can use one of the predefined layouts (\textbf{pseudocode}, \textbf{pascal}
and \textbf{c} and others), with or without modifications, or you can define a
completely new layout for your specific needs.
\end{abstract}
\tableofcontents
\section{Introduction}
All this has begun in my last year at the university. The only thing that I knew of
\LaTeX\ was that it exists, and that it is ``good''. I started using it, but I needed to typeset some
algorithms. So I begun searching for a good algorithmic style, and I have found the \old\ package.
It was a great joy for me, and I started to use it\dots\
Well\dots\ Everything went nice, until I needed some block that wasn't defined in there. What to do?
I was no \LaTeX\ guru, in fact I only knew the few basic macros. But there was no other way, so I opened
the style file, and I copied one existing block, renamed a few things, and voil\`a! This (and some other
small changes) where enough for me\dots
One year later --- for one good soul --- I had to make some really big changes on the style. And there on
a sunny day came the idea. What if I would write some macros to let others create blocks automatically?
And so I did! Since then the style was completely rewritten\dots\ several times\dots
I had fun writing it, may you have fun using it! I am still no \LaTeX\ guru, so if you are, and you find
something really ugly in the style, please mail me! All ideas for improvements are welcome!
Thanks go to Benedek Zsuzsa, Ionescu Clara, Sz\H ocs Zolt\'an, Cseke Botond, Kanoc
%Szotyori Zolt\'an Csaba
and many-many others. Without them I would have never started or continued \textbf{algorithmicx}.
\section{General informations}
\subsection{The package}
The package \textbf{algorithmicx} itself doesn't define any algorithmic commands, but gives
a set of macros to define such a command set. You may use only \textbf{algorithmicx}, and define
the commands yourself, or you may use one of the predefined command sets.
These predefined command sets (layouts) are:
\begin{description}
\item[algpseudocode] has the same look\footnote{almost :-)} as the one defined in the
\old\ package. The main difference is that while the \old\ package doesn't
allow you to modify predefined structures, or to create new ones, the \alg\
package gives you full control over the definitions (ok, there are some
limitations --- you can not send mail with a, say, \verb:\For: command).
\item[algcompatible] is fully compatible with the \old\ package, it should be
used only in old documents.
\item[algpascal] aims to create a formatted pascal program, it performs
automatic indentation (!), so you can transform a pascal program into an
\textbf{algpascal} algorithm description with some basic substitution rules.
\item[algc] -- yeah, just like the \textbf{algpascal}\dots\ but for c\dots\
This layout is incomplete.
\end{description}
To create floating algorithms you will need \verb:algorithm.sty:. This file may or may not be
included in the \alg\ package. You can find it on CTAN, in the \old\ package.
\subsection{The algorithmic block}
Each algorithm begins with the \verb:\begin{algorithmic}[lines]: command, the
optional \verb:lines: controls the line numbering: $0$ means no line numbering,
$1$ means number every line, and $n$ means number lines $n$, $2n$, $3n$\dots\ until the
\verb:\end{algorithmic}: command, witch ends the algorithm.
\subsection{Simple lines}
A simple line of text is beginned with \verb:\State:. This macro marks the begin of every
line. You don't need to use \verb:\State: before a command defined in the package, since
these commands use automatically a new line.
To obtain a line that is not numbered, and not counted when counting the lines for line numbering
(in case you choose to number lines), use the \verb:Statex: macro. This macro jumps into a new line,
the line gets no number, and any label will point to the previous numbered line.
We will call \textit{statament\/}s the lines starting with \verb:\State:. The \verb:\Statex:
lines are not stataments.
\subsection{Placing comments in sources}\label{Putting comments in sources}
Comments may be placed everywhere in the source using the \verb:\Comment: macro
(there are no limitations like those in the \old\ package), feel the freedom!
If you would like to change the form in witch comments are displayed, just
change the \verb:\algorithmiccomment: macro:
\begin{verbatim}
\algrenewcommand{\algorithmiccomment}[1]{\hskip3em$\rightarrow$ #1}
\end{verbatim}
will result:
\medskip
\begin{algorithmic}[1]
\algrenewcommand{\algorithmiccomment}[1]{\hskip3em$\rightarrow$ #1}
\State $x\gets x+1$\Comment{Here is the new comment}
\end{algorithmic}
\subsection{Labels and references}
Use the \verb:\label: macro, as usual to label a line. When you use \verb:\ref: to reference
the line, the \verb:\ref: will be subtitued with the corresponding line number. When using the
\textbf{algorithmicx} package togedher with the \textbf{algorithm} package, then you can label
both the algorithm and the line, and use the \verb:\algref: macro to reference a given line
from a given algorithm:
\begin{verbatim}
\algref{<algorithm>}{<line>}
\end{verbatim}
\noindent\begin{minipage}[t]{0.5\linewidth}
\begin{verbatim}
The \textbf{while} in algorithm
\ref{euclid} ends in line
\ref{euclidendwhile}, so
\algref{euclid}{euclidendwhile}
is the line we seek.
\end{verbatim}
\end{minipage}\begin{minipage}[t]{0.5\linewidth}
The \textbf{while} in algorithm \ref{euclid} ends in line \ref{euclidendwhile},
so \algref{euclid}{euclidendwhile} is the line we seek.
\end{minipage}
\subsection{Breaking up long algorithms}
Sometimes you have a long algorithm that needs to be broken into parts, each on a
separate float. For this you can use the following:
\begin{description}
\item[]\verb:\algstore{<savename>}: saves the line number, indentation, open blocks of
the current algorithm and closes all blocks. If used, then this must be the last command
before closing the algorithmic block. Each saved algorithm must be continued later in the
document.
\item[]\verb:\algstore*{<savename>}: Like the above, but the algorithm must not be continued.
\item[]\verb:\algrestore{<savename>}: restores the state of the algorithm saved under
\verb:<savename>: in this algorithmic block. If used, then this must be the first command
in an algorithmic block. A save is deleted while restoring.
\item[]\verb:\algrestore*{<savename>}: Like the above, but the save will not be deleted, so it
can be restored again.
\end{description}
See example in the \textbf{Examples} section.
\subsection{Multiple layouts in the same document}
You can load multiple algorithmicx layouts in the same document. You can switch between the layouts
using the \verb:\alglanguage{<layoutname>}: command. After this command all new algorithmic
environments will use the given layout until the layout is changed again.
\section{The predefined layouts}
\subsection{The \textbf{algpseudocode} layout}\label{algpseudocode}
\alglanguage{pseudocode}
If you are familiar with the \old\ package, then you'll find it easy to
switch. You can use the old algorithms with the \textbf{algcompatible} layout, but please
use the \textbf{algpseudocode} layout for new algorithms.
To use \textbf{algpseudocode}, simply load \verb:algpseudocode.sty::
\begin{verbatim}
\usepackage{algpseudocode}
\end{verbatim}
You don't need to manually load the \textbf{algorithmicx} package, as this is done by
\textbf{algpseudocode}.
The first algorithm one should write is the first algorithm ever (ok,
an improved version), \textit{\euk's algorithm}:
\begin{algorithm}[H]
\caption{\euk's algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{\euk}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \Return $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
Created with the following source:
\begin{verbatim}
\begin{algorithm}
\caption{Euclid's algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{verbatim}
The \verb:\State: stands at the beginning of each simple statement; the respective
statement is put in a new line, with the needed indentation.
The \verb:\Procedure: \dots\verb:\EndProcedure: and
\verb:\While: \dots\verb:\EndWhile: blocks (like any block defined in the
\textbf{algpseudocode} layout) automatically indent their content.
The indentation of the source doesn't matter, so
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\Repeat
\Comment{forever}
\State this\Until{you die.}
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Repeat
\Comment{forever}
\State this\Until{you die.}
\Statex
\end{algorithmic}
\AENDSKIP
But, generally, it is a good idea to keep the source indented, since you will find
errors much easier. And your tex file looks better!
All examples and syntax descriptions will be shown as the previous
example --- the left side shows the \LaTeX\ input, and the right side
the algorithm, as it appears in your document. I'm cheating! Don't look
in the \verb:algorithmicx.tex: file! Believe what the examples state! I may use some
undocumented and dirty stuff to create all these examples. You might be more
confused after opening \verb:algorithmicx.tex: as you was before.
In the case of syntax
descriptions the text between $<$ and $>$ is symbolic, so if you type
what you see on the left side, you will not get the algorithm on the
right side. But if you replace the text between $<$ $>$ with a proper piece of
algorithm, then you will probably get what you want. The parts between
$[$ and $]$ are optional.
\subsubsection{The \textbf{for} block}
The \textbf{for} block may have one of the forms:
\ASTART
\begin{verbatim}
\For{<text>}
<body>
\EndFor
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\For{$<$text$>$}
\State $<$body$>$
\EndFor
\end{algorithmic}
\AEND
\ASTART
\begin{verbatim}
\ForAll{<text>}
<body>
\EndFor
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\ForAll{$<$text$>$}
\State $<$body$>$
\EndFor
\end{algorithmic}
\AENDSKIP
\noindent Example:
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\State $sum\gets 0$
\For{$i\gets 1, n$}
\State $sum\gets sum+i$
\EndFor
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\State $sum\gets 0$
\For{$i\gets 1, n$}
\State $sum\gets sum+i$
\EndFor
\Statex
\end{algorithmic}
\AEND
\subsubsection{The \textbf{while} block}
The \textbf{while} block has the form:
\ASTART
\begin{verbatim}
\While{<text>}
<body>
\EndWhile
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\While{$<$text$>$}
\State $<$body$>$
\EndWhile
\end{algorithmic}
\AENDSKIP
\noindent Example:
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\State $sum\gets 0$
\State $i\gets 1$
\While{$i\le n$}
\State $sum\gets sum+i$
\State $i\gets i+1$
\EndWhile
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\State $sum\gets 0$
\State $i\gets 1$
\While{$i\le n$}
\State $sum\gets sum+i$
\State $i\gets i+1$
\EndWhile
\Statex
\end{algorithmic}
\AEND
\subsubsection{The \textbf{repeat} block}
The \textbf{repeat} block has the form:
\ASTART
\begin{verbatim}
\Repeat
<body>
\Until{<text>}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Repeat
\State $<$body$>$
\Until{$<$text$>$}
\end{algorithmic}
\AENDSKIP
\noindent Example:
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\State $sum\gets 0$
\State $i\gets 1$
\Repeat
\State $sum\gets sum+i$
\State $i\gets i+1$
\Until{$i>n$}
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\State $sum\gets 0$
\State $i\gets 1$
\Repeat
\State $sum\gets sum+i$
\State $i\gets i+1$
\Until{$i>n$}
\Statex
\end{algorithmic}
\AEND
\subsubsection{The \textbf{if} block}
The \textbf{if} block has the form:
\ASTART
\begin{verbatim}
\If{<text>}
<body>
[
\ElsIf{<text>}
<body>
...
]
[
\Else
<body>
]
\EndIf
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\If{$<$text$>$}
\State $<$body$>$
\Statex [
\ElsIf{$<$text$>$}
\State $<$body$>$
\Statex \dots
\Statex ]
\Statex [
\Else
\State $<$body$>$
\Statex ]
\EndIf
\end{algorithmic}
\AENDSKIP
\noindent Example:
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\If{$quality\ge 9$}
\State $a\gets perfect$
\ElsIf{$quality\ge 7$}
\State $a\gets good$
\ElsIf{$quality\ge 5$}
\State $a\gets medium$
\ElsIf{$quality\ge 3$}
\State $a\gets bad$
\Else
\State $a\gets unusable$
\EndIf
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\If{$quality\ge 9$}
\State $a\gets perfect$
\ElsIf{$quality\ge 7$}
\State $a\gets good$
\ElsIf{$quality\ge 5$}
\State $a\gets medium$
\ElsIf{$quality\ge 3$}
\State $a\gets bad$
\Else
\State $a\gets unusable$
\EndIf
\Statex
\end{algorithmic}
\AEND
\subsubsection{The \textbf{procedure} block}
The \textbf{procedure} block has the form:
\ASTART
\begin{verbatim}
\Procedure{<name>}{<params>}
<body>
\EndProcedure
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Procedure{$<$name$>$}{$<$params$>$}
\State $<$body$>$
\EndProcedure
\end{algorithmic}
\AENDSKIP
\noindent Example: See \euk's\ algorithm on page \pageref{euclid}.
\subsubsection{The \textbf{function} block}The
\textbf{function} block has the same syntax as the \textbf{procedure} block:
\ASTART
\begin{verbatim}
\Function{<name>}{<params>}
<body>
\EndFunction
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Function{$<$name$>$}{$<$params$>$}
\State $<$body$>$
\EndFunction
\end{algorithmic}
\AEND
\subsubsection{The \textbf{loop} block}
The \textbf{loop} block has the form:
\ASTART
\begin{verbatim}
\Loop
<body>
\EndLoop
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Loop
\State $<$body$>$
\EndLoop
\end{algorithmic}
\AEND
\subsubsection{Other commands in this layout}
The starting conditions for the algorithm can be described with the \textbf{require}
instruction, and its result with the \textbf{ensure} instruction.
A procedure call can be formatted with \verb:\Call:.
\ASTART
\begin{verbatim}
\Require something
\Ensure something
\Statex
\State \Call{Create}{10}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Require something
\Ensure something
\Statex
\State \Call{Create}{10}
\end{algorithmic}
\AENDSKIP
\noindent Example:
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\Require $x\ge5$
\Ensure $x\le-5$
\Statex
\While{$x>-5$}
\State $x\gets x-1$
\EndWhile
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Require $x\ge5$
\Ensure $x\le-5$
\Statex
\While{$x>-5$}
\State $x\gets x-1$
\EndWhile
\Statex
\end{algorithmic}
\AEND
\subsubsection{Package options}\label{algpseudocode package options}
The \texttt{algpseudocode} package supports the following options:
\begin{description}
\item[compatible/noncompatible]\ \textit{Obsolote, use the algcompatible layout instead.}\\
If you would like to use old
algorithms, written with the \old\ package without (too much)
modification, then use the \textbf{compatible} option. This option
defines the uppercase version of the commands. Note that you still need
to remove the \verb:[...]: comments (these comments appeared due to some
limitations in the \old\ package, these limitations and comments are gone now).
The default \textbf{noncompatible} does not define the all uppercase
commands.
\item[noend/end]\ \\With \textbf{noend} specified all \textbf{end \dots}
lines are omitted. You get a somewhat smaller algorithm, and the ugly
feeling, that something is missing\dots{} The \textbf{end} value is the
default, it means, that all \textbf{end \dots} lines are in their right
place.
\end{description}
\subsubsection{Changing command names}
One common thing for a pseudocode is to change the command names. Many people
use many different kind of pseudocode command names. In \textbf{algpseudocode}
all keywords are declared as \verb:\algorithmic<keyword>:. You can change them
to output the text you need:
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}}
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}}
\algrenewcommand\algorithmicend{\textbf{v\'ege}}
\begin{algorithmic}[1]
\State $x \gets 1$
\While{$x < 10$}
\State $x \gets x + 1$
\EndWhile
\end{algorithmic}
\end{verbatim}
\end{minipage}
\begin{minipage}[b]{0.5\linewidth}
\begin{algorithmic}[1]
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}}
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}}
\algrenewcommand\algorithmicend{\textbf{v\'ege}}
\State $x \gets 1$
\While{$x < 10$}
\State $x \gets x + 1$
\EndWhile
\Statex
\end{algorithmic}
\end{minipage}\bigskip
In some cases you may need to change even more (in the above example
\textbf{am\'\i g} and \textbf{v\'ege} should be interchanged in the \verb:\EndWhile:
text). Maybe the number of the parameters taken by some commands must be changed too.
this can be done with the command text customizing macros (see section
\ref{custom text}). Here I'll give only some examples of the most common usage:
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}}
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}}
\algrenewcommand\algorithmicend{\textbf{v\'ege}}
\algrenewtext{EndWhile}{\algorithmicwhile\ \algorithmicend}
\begin{algorithmic}[1]
\State $x \gets 1$
\While{$x < 10$}
\State $x \gets x - 1$
\EndWhile
\end{algorithmic}
\end{verbatim}
\end{minipage}
\begin{minipage}[b]{0.5\linewidth}
\begin{algorithmic}[1]
\algrenewcommand\algorithmicwhile{\textbf{am\'\i g}}
\algrenewcommand\algorithmicdo{\textbf{v\'egezd el}}
\algrenewcommand\algorithmicend{\textbf{v\'ege}}
\algrenewtext{EndWhile}{\algorithmicwhile\ \algorithmicend}
\State $x \gets 1$
\While{$x < 10$}
\State $x \gets x - 1$
\EndWhile
\Statex
\end{algorithmic}
\end{minipage}
\bigskip\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\algnewcommand\algorithmicto{\textbf{to}}
\algrenewtext{For}[3]%
{\algorithmicfor\ #1 \gets #2 \algorithmicto\ #3 \algorithmicdo}
\begin{algorithmic}[1]
\State $p \gets 1$
\For{i}{1}{n}
\State $p \gets p * i$
\EndFor
\end{algorithmic}
\end{verbatim}
\end{minipage}
\begin{minipage}[b]{0.5\linewidth}
\begin{algorithmic}[1]
\algnewcommand\algorithmicto{\textbf{to}}
\algrenewtext{For}[3]%
{\algorithmicfor\ $#1 \gets #2$ \algorithmicto\ $#3$ \algorithmicdo}
\State $p \gets 1$
\For{i}{1}{n}
\State $p \gets p * i$
\EndFor
\Statex
\end{algorithmic}
\end{minipage}\bigskip
You could create a translation package, that included after the \textbf{algpseudocode}
package translates the keywords to the language you need.
\subsection{The \textbf{algpascal} layout}
\alglanguage{pascal}
The most important feature of the \textbf{algpascal} layout is that
\textit{it performs automatically the block indentation}. In
section \ref{algorithmicx} you will see how to define such
automatically indented loops. Here is an example to demonstrate this
feature:
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\Begin
\State $sum:=0$;
\For{i=1}{n}\Comment{sum(i)}
\State $sum:=sum+i$;
\State writeln($sum$);
\End.
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Begin
\State $sum:=0$;
\For{i=1}{n}\Comment{sum(i)}
\State $sum:=sum+i$;
\State writeln($sum$);
\End.
\Statex
\end{algorithmic}
\AENDSKIP
Note, that the \verb:\For: is not closed explicitly, its end is
detected automatically. Again, the indentation in the source doesn't
affect the output.
In this layout every parameter passed to a command is put in
mathematical mode.
\subsubsection{The \textbf{begin} \dots{} \textbf{end} block}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\Begin
<body>
\End
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Begin
\State $<$body$>$
\End
\end{algorithmic}
\AENDSKIP
The \verb:\Begin: \dots{} \verb:\End: block and the
\verb:\Repeat: \dots{} \verb:\Until: block are the only blocks in
the \textbf{algpascal} style (instead of \verb:\Begin: you may write
\verb:\Asm:). This means, that every other loop is ended automatically
after the following command (another loop, or a block).
\subsubsection{The \textbf{for} loop}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\For{<assign>}{<expr>}
<command>
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\For{<$\relax$assign$\relax$>}{<$\relax$expr$\relax$>}
\State $<$command$>$
\end{algorithmic}
\AENDSKIP
The \textbf{For} loop (as all other loops) ends after the following command (a block counts
also as a single command).
\ASTART
\begin{verbatim}
\begin{algorithmic}[1]
\Begin
\State $sum:=0$;
\State $prod:=1$;
\For{i:=1}{10}
\Begin
\State $sum:=sum+i$;
\State $prod:=prod*i$;
\End
\End.
\end{algorithmic}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Begin
\State $sum:=0$;
\State $prod:=1$;
\For{i:=1}{10}
\Begin
\State $sum:=sum+i$;
\State $prod:=prod*i$;
\End
\End.
\Statex
\end{algorithmic}
\AEND
\subsubsection{The \textbf{while} loop}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\While{<expression>}
<command>
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\While{<$\relax$expression$\relax$>}
\State $<$command$>$
\end{algorithmic}
\AEND
\subsubsection{The \textbf{repeat}\dots\ \textbf{until} block}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\Repeat
<body>
\Until{<expression>}
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Repeat
\State $<$body$>$
\Until{<$\relax$expression$\relax$>}
\end{algorithmic}
\AEND
\subsubsection{The \textbf{if} command}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\If{<expression>}
<command>
[
\Else
<command>
]
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\If{<$\relax$expression$\relax$>}
\State $<$command$>$
\Statex \hskip-\algorithmicindent\hskip-\algorithmicindent[
\Else
\State $<$command$>$
\Statex \hskip-\algorithmicindent\hskip-\algorithmicindent]
\end{algorithmic}
\AENDSKIP
Every \verb:\Else: matches the nearest \verb:\If:.
\subsubsection{The \textbf{procedure} command}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\Procedure <some text>
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Procedure $<$some text$>$
\end{algorithmic}
\AENDSKIP
\verb:\Procedure: just writes the ``procedure'' word on a new
line... You will probably put a \verb:\Begin:\dots\ \verb:\End:
block after it.
\subsubsection{The \textbf{function} command}
\noindent\begin{minipage}[b]{0.5\linewidth}
\begin{verbatim}
\Function<some text>
\end{verbatim}
\ACONTINUE
\begin{algorithmic}[1]
\Function $<$some text$>$
\end{algorithmic}
\AENDSKIP
Just like \textbf{Procedure}.
\subsection{The \textbf{algc} layout}
Sorry, the \textbf{algc} layout is unfinished.
The commands defined are:
\begin{itemize}
\item\verb:\{:\dots\ \verb:\}: block
\item\verb:\For: with 3 params
\item\verb:\If: with 1 param
\item\verb:\Else: with no params
\item\verb:\While: with 1 param
\item\verb:\Do: with no params
\item\verb:\Function: with 3 params
\item\verb:\Return: with no params
\end{itemize}
\section{Custom algorithmic blocks}\label{algorithmicx}
\alglanguage{default}
\subsection{Blocks and loops}
Most of the environments defined in the standard layouts (and most probably
the ones you will define) are divided in two categories:
\begin{description}
\item[Blocks] are the environments witch contain an arbitrary number of
commands or nested blocks. Each block has a name, begins with a starting command
and ends with an ending command. The commands in a block are
indented by \verb:\algorithmicindent: (or another amount).
If your algorithm ends without closing all blocks, the \alg\ package gives
you a nice error. So be good, and close them all!
Blocks are all the environments defined in the \verb:algpseudocode:
package, the \verb:\Begin: \dots \verb:\End: block in the
\verb:algpascal: package, and some other ones.
\item[Loops] (Let us call them loops\dots) The loops are environments
that include only one command, loop or block; a loop is closed
automatically after this command. So loops have no ending commands. If
your algorithm (or a block) ends before the single command of a loop,
then this is considered an empty command, and the loop is closed. Feel
free to leave open loops at the end of blocks!
Loops are most of the environments in the \verb:algpascal: and
\verb:algc: packages.
\end{description}
For some rare constructions you can create mixtures of the two
environments (see section \ref{setblock}).
Each block and loop may be continued with another one (like the \verb:If:
with \verb:Else:).
\subsection{Defining blocks}\label{defblocks}
There are several commands to define blocks. The difference is in what is defined
beyond the block. The macro \verb:\algblock: defines a new block with starting and
ending entity.
\begin{verbatim}
\algblock[<block>]{<start>}{<end>}
\end{verbatim}
The defined commands have no parameters, and the text displayed by them is
\verb:\textbf{<start>}: and \verb:\textbf{<end>}:. You can change these texts later
(\ref{custom text}).
With \verb:\algblockdefx: you can give the text to be output by the starting
and ending command and the number of parameters for these commands. In the text
reference with \#$n$ to the parameter number $n$. Observe that the text
is given in the form you define or redefine macros, and really, this is what happens.
\begin{verbatim}
\algblockdefx[<block>]{<start>}{<end>}
[<startparamcount>][<default value>]{<start text>}
[<endparamcount>][<default value>]{<end text>}
\end{verbatim}
This defines a new block called \verb:<block>:, \verb:<start>: opens the block,
\verb:<end>: closes the block,
\verb:<start>: displays \verb:<start text>:, and has \verb:<startparamcount>: parameters,
\verb:<end>: displays \verb:<end text>:, and has \verb:<endparamcount>: parameters.
For both \verb:<start>: and \verb:<end>:, if
\verb:<default value>: is given, then the first parameter is optional, and its default value