-
Notifications
You must be signed in to change notification settings - Fork 1
/
algorithm2e.sty
3288 lines (3287 loc) · 163 KB
/
algorithm2e.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
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
% algorithm2e.sty --- style file for algorithms
% almost everything can be customized by users. See the document for more explanations
%% Copyright 1996-2017 Christophe Fiorio
%
% This work may be distributed and/or modified under the conditions of the LaTeX Project
% Public License, either version 1.3 of this license or (at your option) any later version.
% The latest version of this license is in http://www.latex-project.org/lppl.txt and
% version 1.3 or later is part of all distributions of LaTeX version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is M. Christophe Fiorio
%
% This work consists of the files algorithm2e.sty and algorithm2e.tex
% and the associated example files algorithm2e_exAlgoDisjdecomp.tex,
% algorithm2e_exIR.tex, algorithm2e_ex01.tex, algorithm2e_exProg.tex,
% algorithm2e_ex02.tex, algorithm2e_exfor.tex, algorithm2e_ex03.tex,
% algorithm2e_exgeneric.tex, algorithm2e_ex04.tex, algorithm2e_exgeneric2.tex,
% algorithm2e_ex05.tex, algorithm2e_exnlsty.tex, algorithm2e_ex06.tex,
% algorithm2e_exrepeat.tex, algorithm2e_ex07.tex, algorithm2e_exswitch.tex
%
% Report bugs and comments to:
% - algorithm2e-announce@lirmm.fr mailing list for announcement about releases^^J%
% - algorithm2e-discussion@lirmm.fr mailing list for discussion about package^^J%
% subscribe by emailing sympa@lirmm.fr with 'subscribe <list> <firstname name>'^^J%
%
% $Id: algorithm2e.sty,v 5.2 2017/07/18 15:26:26 fioriochristophe Exp $
%
% PACKAGES REQUIRED:
%
% - float (in contrib/supported/float)
% - ifthen (in base)
% - xspace (in packages/tools)
% - relsize (in contrib/misc/relsize.sty)
%
%%%%%%%%%%%%%%% Release 5.2
%
% Package options:
% ---------------
% - oldcommands : to use old command names
% - french, english, german, ngerman
% portuguese, czech, italiano,
% slovak, croatian, spanish : for the name of the algorithm and some keyword code
% - onelanguage : to simply switch keyword from one language to another without changing
% keyword commands
% - boxed, boxruled, ruled, tworuled,
% algoruled, plain : layout of the algorithm
% - algo2e : environment is algorithm2e instead of algorithms and \listofalgorithmes
% instead of \listofalgorithms
% - slide : to use when making slides
% - noline,lined,vlined : how block are designed.
% - shortend, longend, noend : short or long end keyword as endif for e.g.
% - linesnumbered : auto numbering of the algorithm's lines
% - linesnumberedhidden : to hide autonumbered lines (show number on a line with \ShowLn
% - commentsnumbered, inoutnumbered : to autonumber comments and inout keywords (by defaut not numbered)
% - rightnl : to have line number on the right instead of on the left as default
% - algonl : line numbers preceded by algo number
% - scright, scleft : right or left justified side comments
% - fillcomment, nofillcomment : end mark of comment is flushed to the right so comments fill the line
% - dotocloa : add an entry in the toc for list of algorithms (require tocbibind package)
% - endfloat : add algoendfloat environment pushing algorithm so written to the end of document
% - resetcount, noresetcount : start value of line numbers.
% - algopart,algochapter,algosection : algo numbering within part, chapter or section
% - titlenumbered,titlenotnumbered : numbering of title set by \Titleofalgo
% - figure : algorithms are figures, numbered as figures, and put in the list of figures.
% - procnumbered : procedure or function are numbered as algorithm
% - nokwfunc : procedure or function name doens't become a command
% - norelsize : don't use relsize package (useful if it breaks the compatibily)
% - displayblockmarkers : display begin, end keywords at start of each block
%
% defaults are; english,plain,resetcount,titlenotnumbered
%
%%%%%%%%%%%%%%
%
% Short summary
% -------------
%
% algorithm is an environment for writing algorithm in LaTeX2e.
% Almost all is customizable. You can add keywords, change style, change the layout, ...
% It provide macros that allow you to create differents sorts of key words, therefore a set of predefined key
% word is gived.
%
% IT should be used as follows
%
% \begin{algorithm}
% ...
% ...
% \end{algorithm}
%
%
% IMPORTANT : each line MUST end with \;
%
% Note that if you define macros outside algorithm environment they
% are avaible in all the document and particulary you can use then
% inside all algorithms without re-define them.
%
% an example:
%
% \begin{algorithm}
% \SetAlgoLined
% \KwIn{this text}
% \KwOut{how to write algorithm with \LaTeX2e }
%
% initialization\;
% \While{not at end of this document}{
% read current section\;
% \eIf{understand}{
% go to next section\;
% current section becomes this one\;
% }{
% go back to the beginning of current section\;
% }
% }
% \caption{How to write algorithm}
% \end{algorithm}
%
%
%%%%%%%%%%%%%% predefined keywords
%
% \KwIn{input}
% \KwOut{output}
% \KwData{input}
% \KwResult{output}
% \KwTo % a simple keyword
% \KwFrom % a simple keyword
% \KwRet{[value]}
% \Return{[value]}
% \Begin{block inside}
% \eIf{condition}{Then Block}{Else block} % in blocks
% \If{condition}{Then block} % in a block
% \uIf{condition}{Then block} % in a block unended
% \lIf{condition}{Else text} % on the same line
% \Else{inside Else} % in a block
% \lElse{Else text} % on the same line
% \uElse{Else text} % in a block unended
% \ElseIf{inside Elseif} % in a block
% \lElseIf{Elseif text} % on the same line
% \uElseIf{Elseif text} % in a block unended
% \Switch{Condition}{Switch block}
% \Case{a case}{case block} % in a block
% \lCase{a case}{case text} % on the same line
% \Other{otherwise block} % in a block
% \lOther{otherwise block} % on the same line
% \For{condition}{text loop} % in a block
% \lFor{condition}{text} % on the same line
% \ForEach{condition}{text loop} % in a block
% \lForEach{condition}{text} % on the same line
% \ForPar{condition}{text loop} % in a block
% \lForPar{condition}{text} % on the same line
% \While{condition}{text loop} % in a block
% \lWhile{condition}{text loop} % on the same line
% \Repeat{End condition}{text loop} % in a block
% \lRepeat{condition}{text} % on the same line
%
%%%%%%%%%%%%%%
%
% History:
%
% - July 18 2017 - revision 5.2
% * FIX: There was an overfull box 0.8pt with boxruled algorithm. This is fixed.
% * FIX: problem when mixing caption package and figure option. This bug was partially corrected
% in 5.0 for one side document but with false indentation and not working for two side
% document. This is now fully fixed.
% * FIX: misaligned lines numbers with noline option
% * FIX: Fix croation keywords thanks to Ivan Kolan
% * FIX: there was an overfull hbox of 8.5pt when using [H] and ruled caption. This is fixed.
% * FIX: \SetCustomAlgoRuledWidth only changed midrule. This is fixed, it changes all the rules
% - October 19 2015 - revision 5.1
% * CHANGE/ADD: l commands (the ones as \lIf) can now be used with a
% star. If done, no end of line are done, so you can enclose
% l command into another one. For example, you can write:
% \lForEach{$i$}{\lIf*{foo}{bar}}.
% Note that when you use a star, side comments are not allowed.
% * CHANGE/ADD: Now you can have a side comment at end of macros which
% have a block. For example you can do :
% \Begin(\tcc*[h]{side comment for begin}{text}(\tcc*[h]{side comment after end})
% Note: there are some side effects for some commands:
% \SetKwProg : macro defined thanks to \SetKwProg are allowed
% to have no end marker. In this case,
% beware to end side comment, this can
% do weird output (but no error)
% * CHANGE: \SetKwSwitch defines also an \uOther command which was not
% the case until this release.
% * CHANGE: \SetStartEndCondition{typo1}{typo2}{typo3} defined two end
% conditions (type2 and typo3) : typo3 is no more used for
% case condition as typo2 is used which is more consistent
% since there is a condition followed by a keyword as for
% if-then or other command that are defined using typo2.
% * CHANGE: keywords was not language dependant to allow to use multiple
% languages in the same document, but some keywords in
% different language were having the same keyword that lead
% last one declared to replace first one and so you could have some
% spanish keywords printed as you were in portuguese. So
% this behavior is changed from now:
% - localized keywords are defined if according language
% option is defined ;
% - if you want to keep old behaviour and use localized
% keywords without using language option (keep old
% behavior), you can use languagekw option
% (e.g. frenchkw, germankw, ...). Note that you can use
% multiple languagekw options at the same time, in
% contrary of language option. Note also that if you use
% multiple languagekw options, you can get back the same
% problem as previously when keywords was not language
% dependant.
% * ADD: new typo styles:
% - Arguments of functions have now their own style. By
% default, FuncArgSty is the same as ArgSty.
% - Name of KwProg have now their own sty (they used ArgSty
% previously). By default, ProgSty is still ArgSty.
% As usual, \SetFuncArgSty{}, FuncArgSty{} and
% \SetProgSty{}, ProgSty{} are macros to set and use these
% typo styles.
% * ADD: Options to manage algorithm hangindent
% Until this revision, long statement acts as classical text and
% continue on next line starting from the same point. This makes
% difficult to notice that the new lines of text is not a new
% statement. Now, hangindent is managed and when a long
% statement continue on next lines, lines, except the first,
% are indented.
% Option: noalgohanging
% By default, long statement are indented on subsequent
% lines; with this option you get old behavior and lines are
% no more indented
% Option: hangingcomment
% Comment that are alone on a line (not side comment) are
% not driven by algo hanging: subsequent lines of long
% comments are indented according to width of start comment
% marker. With this option, comment are indented like normal
% statement.
% Option: hanginginout
% As for comment, by default, in/out keywords and input
% keywords (defined by \SetKwInOut or \SetKwInput) are not
% indented by algorithm hangindent. They are indented
% according to width of input keyword. If you want that these
% keywords acts like normal statement, use this option.
% \SetAlgoHangIndent{length}
% This macro allows you to set your own indent length. By
% default, \SetAlgoHangIndent{0.5em}
% * ADD: Group markers.
% This option is related to block markers of release 5.0. Some
% have ask to put also block markers for single line
% command. This new feature has been implemented to do this.
% So, you can now ask package to put begin and end keywords
% automatically at start and end of single line command (see
% example in documentation).
% These new group markers macros are:
% - \AlgoDisplayGroupMarkers and \AlgoDontDisplayGroupMarkers
% Note that a new option has also been added: displaygroupmarkers
% * ADD: \SetCustomAlgoRuledWidth{length}
% set a custom ruled width of ruled and algoruled options ;
% note that by the moment you use this macro, rules will have
% this fixed length: this can cause strange behavior in
% multicolumn mode or other layout that change the line
% width.
% * ADD: turkish language option
% * FIX: a spurious whitespace which results in a spurious indent in the
% user's text right after the end of the algorithm environment
% was introduced in release 5.0. This is fixed now thanks to
% Alexander Malkis.
% * FIX: there was a problem with margins in caption when option figure
% was use with caption package. This is fixed.
% * FIX: when changing font size in caption with, interline skip stayed
% unchanged and so with a small font was to large. This is fixed.
% * FIX: in contrary of that is written in the documentation, ':' what
% not in KwSty typo for KwIn and KwOut command. This is fixed.
% * FIX: strange behaviour with label and lines numbered: @currentlabel
% was updated according to algo line number not in all
% situation. For e.g, with hyperref package, always updated ;
% with \nlset also updated, but with \nl or linesnumbered
% option, not updated. This "feature" causes different output
% with \label command according to options used which is not
% that one want. This is solved now, and @currentlabel is
% always updated so \label command works always is the same way.
% * FIX: some czech keywords
% * FIX: some spanish keywords
% * FIX: some croatian keywords
% * FIX: krantz class changes definition of chapter and get one more
% option that classical classes as book.cls or report.cls and
% broke fix for hyperref on chapter definition. This is now
% fixed and algorithm detects use of krantz class and use then
% a definition of chapter working with krantz class.
% * FIX: an issue with internal macro which causes ([Q]) use as argument
% of some environment to be misprinted (thanks to Martin Schröder).
% * FIX: mispelled name of hyperrefcounter inside internal macro.
%
% - January 06 2013 - revision 5.0
% * CHANGE: SetKwSwitch takes now 9 args: 9th arg is the same as
% previous 8th arg ('end of switch' keyword). New 8th arg is
% 'end of case' keyword. This is due to change of release
% 3.2 which introduce end after case block... as I never
% test with longend option, I never see that the 'end
% switch' used for case was not good.
% * CHANGE: when no end keyword is defined in a block macro, then
% algorithm2e does no more try to print it. So even with lined or noline
% option, no empty line is printed (before: a blank end was
% printed, so a blank line appeared)
% * Internal Change: add some internal function to improve readibility
% (thanks to Philip K. F. H\ölzenspies)
% * ADD: Block markers.
% You can now ask package to put begin and end keywords automatically at begin
% and end of blocks, it means each group of commands shifted and enclosed in
% braces.
% This is tricky to use but, combined with \SetStartEndCondition and
% redefinition of keywords, you should be abble to simulate any syntax. See
% examples in documentation where a generic example is derived in pseudo-code,
% python and C by keeping code and changing only style using block markers
% macros, \SetStartEndCondition and some redefinition of keywords.
% These new block markers macros are:
% - \AlgoDisplayBlockMarkers and \AlgoDontDisplayBlockMarkers
% - \SetAlgoBlockMarkers{begin marker}{end marker}
% - \BlockMarkersSty{text} and \SetBlockMarkersSty
% Note that a new option has also been added: displayblockmarkers
% * ADD: \leIf macro automatically defined by \SetKwIF: allow to define
% an if-then-else on a single line.
% * ADD: new macro \SetStartEndCondition{typo1}{typo2}{typo3} which
% sets typo around condition in For, If, Switch, Case and
% Repeat macros. First two are used around For, If, Swith
% conditions, First and third are used for Case and Repeat
% condition where condition ends the line. Default definition
% is \SetStartEndCondition{ }{ }{}.
% A common alternative is \SetStartEndCondition{ (}{) }{)}
% Can also be used to remove space around condition, for
% example if you want python style commands:
% \SetStartEndCondition{ }{}{} and \SetKwFor{For}{for}{:}{}
% * ADD: new environment algomathdisplay which allow display math (like inside \[ \] or $$ $$)
% handling end line and line number
% * ADD: new command \SetKwProg{Env}{Title}{is}{end} which defines a macro
% \Env{args}{text}. Env is a block with 'Title' (in \CapSty) at the beginning
% followed by args followed by 'is' then 'text' is put below inside a block ended
% by 'end'. If no 'end' is specified, nothing is written (no
% blank line is inserted). Useful to typeset function or prog for example:
% \SetKwProg{Fn}{Function}{ is}{end} makes \Fn{afunc(i: int) : int}{return 0\;}
% writes:
% Function afunc(i: int) : int is
% | return 0;
% end
% or \SetKwProg{Def}{def}{:}{} makes \Def{afunc(i: int)}{return 0\;} writes:
% def afunc(i: int):
% | return 0
% Tip: combine it with \SetKwFunction to write recursive function algorithm. With
% example above, you could define \SetKwFunction{\Afunc}{afunc} and then write:
% \Def{\Afunc{i:int}{\eIf{i>0}{\KwRet \Afunc{i-1}}{\KwRet 0\;}} that writes:
% def afunc(i: int):
% | if(i>0):
% | return afunc(i-1)
% | else:
% | return 0
% with appropriate typo.
% * ADD: option croatian: croatian keywords (thanks to Ivan Kohan and
% Yvan Gavran)
% * ADD: option ngerman: same as german option but so can be used with global option ngerman
% of babel
% * ADD: option spanish: Spanish support (thanks to Mario Abarca)
% * ADD: unterminated block: useful to add part separator that doesn't necessary need an end
% keyword.
% Designed on the pattern of unterminated if (see \uIf macro) allowing to
% add a block that is not terminated by a keyword. Such block are defined in the same
% time as a block is defined by adding a macro beginning with u. So, for example,
% predefined \SetKwBlock{Begin}{begin}{end} defines now two commands:
% - \Begin{} as previously which print a begin - end block
% - \uBegin{} that defines a begin only block
% * FIX: problem when numbering line inside until condition of
% \SetKwRepeat macro: line number was not correctly aligned.
% * FIX: dotocloa option which was broken
% * FIX: uIf and uCase didn't have same behavior when used with
% noline, vlined or lined option. This is fixed. Side effect: no empty
% line after an uIf or uCase when used with options lined or vlined
% * FIX: a bug with Repeat Until command when use with side comment on Until
% * FIX: a bug with side text -- text put into () -- of command macro (SetKwIf and so on)
% which was always setting a ';' even after a \DontPrintSemicolon
% * FIX: a bug with hyperref and chapter definition (thanks to Hubert Meier)
% * FIX: bugs with l macro and side comment
% * FIX: revision number
% * FIX: fix non ascii character (utf8 not yet recognized by all latex engine)
% * FIX: fnum@algocf had an useless parameter which sometimes broke expansion and output an error
% * FIX: works now with multicol package
%
% - december 14 2009 - revision 4.1
% * ADD: new command \SetKwHangingKw{Name}{text} (hanging indent with keyword): This creates a
% hanging indent much like \texttt{SetKwInput}, except that it removes the trailing `:'
% and does not reset numbering (thanks to Nathan Tallent)
%
% - november 17 2009 - revision 4.00 -
%
% * CHANGE: IMPORTANT: some commands have been renamed to have consistent naming (CamlCase
% syntax) and old commands are no more available. If you doesn't want to change
% your mind or use old latex files, you can use oldcommands option to enable old
% commands back.
% text. Here are these commands:
% - \SetNoLine becomes \SetAlgoNoLine
% - \SetVline becomes \SetAlgoVlined
% - \Setvlineskip becomes \SetVlineSkip
% - \SetLine becomes \SetAlgoLined
% - \dontprintsemicolon becomes \DontPrintSemicolon
% - \printsemicolon becomes \PrintSemicolon
% - \incmargin becomes \IncMargin
% - \decmargin becomes \DecMargin
% - \setnlskip becomes \SetNlSkip
% - \Setnlskip becomes \SetNlSkip
% - \setalcapskip becomes \SetAlCapSkip
% - \setalcaphskip becomes \SetAlCapHSkip
% - \nlSty becomes \NlSty
% - \Setnlsty becomes \SetNlSty
% - \linesnumbered becomes \LinesNumbered
% - \linesnotnumbered becomes \LinesNotNumbered
% - \linesnumberedhidden becomes \LinesNumberedHidden
% - \showln becomes \ShowLn
% - \showlnlabel becomes \ShowLnLabel
% - \nocaptionofalgo becomes \NoCaptionOfAlgo
% - \restorecaptionofalgo becomes \RestoreCaptionOfAlgo
% - \restylealgo becomes \RestyleAlgo
% - gIf macros and so on do no more exist
% * NEW: - Compatibily with other packages improven by changing name of internal
% macros. Algorithm2e can now be used with arabtex for example, if this last is
% loaded after algorithm2e package.
% * ADD: - OPTION endfloat: endfloat packages doesn't allow float environment inside other
% environment. So using it with figure option of algorithm2e makes error. This
% option enables a new environment algoendfloat to be used instead of algorithm
% environment that put algorithm at the end. algoendfloat environment make
% algorithm acting as endfloat figures. This option requires endfloat packages.
% * ADD: - OPTION norelsize: starting from this release (v4.00), algorithm2e package uses
% relsize package in order to get relative size for lines numbers; but it seems
% that some rare classes (such as inform1.cls) are not compatible with relsize; to
% have algorithm2e working, this option makes algorithm2e not to load relsize
% package and go back to previous definition by using \scriptsize font for lines
% numbers.
% * ADD: - OPTION onelanguage: allow, if using standard keywords listed below, to switch
% from one language to another without changing keywords by using appropriate
% language option:
% . KwIn, KwOut, KwData, KwResult
% . KwTo KwFrom
% . KwRet, Return
% . Begin
% . Repeat
% . If, ElseIf, Else
% . Switch, Case, Other
% . For, ForPar, ForEach, ForAll, While
% .
% * ADD: - OPTION rightnl: put lines numbers to the right of the algorithm instead of left.
% * ADD: new commands \setRightLinesNumbers and \setLeftLinesNumbers which sets the lines
% numbers to the right or to the left of the algorithm.
% * ADD: - new kind of keywords: KwArray used to define arrays:
% \SetKwArray{Kw}{array} defines an array keywords Kw called array and printed in
% DataSty style when call with \Kw. It can be used with one argument which
% denotes the element index: \Kw{n} prints array[n] with array in DataSty and n in
% ArgSty.
% * ADD/FIX: rules of ruled, algoruled, tworuled styles used rules of different sizes! This
% is now fixed. Moreover size of the rules is now controlled by a length and so
% can be customized by the user.
% \algoheightrule is the height of the rules and can be changed via \setlength
% \algoheightruledefault is the default height of he rules (0.8pt)
% \algotitleheightrule is the height of the rule that comes just after the
% caption in ruled and algoruled style; it can be changed via \setlength
% \algotitleheightruledefault is the default height of this rules (0.8pt)
% Thanks to Philippe Dumas who reports the bug and make the suggestion.
% * ADD: - \SetAlgoCaptionSeparator which sets the separator between Algorithm 1 and the
% title. By default it's ':' and caption looks like "Algorithm 2: title" but now
% you can change it by using for example \SetAlgoCaptionSeparator{.} which will
% give "Algorithm 3. title"
% * ADD: - \SetAlgoLongEnd and \SetAlgoShortEnd and \SetAlgoNoEnd commands which act as
% corresponding package options
% * ADD: - OPTIONS italiano and slovak as new language (thanks to Roberto Posenato and
% Miroslav Binas)
% * CHANGE: - Fnt and Sty macro to have consistent use and naming (see below)
% * ADD: - \AlCapSty, \AlCapNameSty, \AlCapFnt, \AlCapNameFnt, \ProcSty, \ProcFnt,
% \ProcNameSty, \ProcNameFnt, \ProcArgSty, ProcArgFnt and corresponding "set macro"
% \SetAlCapSty, \SetAlCapNameSty, \SetAlCapFnt, \SetAlCapNameFnt, \SetProcSty,
% \SetProcFnt, \SetProcNameSty, \SetProcNameFnt, \SetProcArgSty, \SetProcArgFnt which
% control the way caption is printed. Sty macro use command taking one parameter as
% argument, Fnt macros use directly command. In Fact caption is printed as follow:
% \AlCapSty{\AlCapFnt Algorithm 1:}\AlCapNameSty{\AlCapNameFnt my algorithm}
% By default, \AlCapSty is textbf and \AlCapFnt is nothing. \AlCapNameSty keep text
% as it is, and \AlCapNameFnt do nothing also.
% You can redefine \AlCapFnt and \AlCapNameFnt by giving macro to \Set commands. For
% example, you can do \SetAlCapFnt{\large} to see Algorithm printed in \large font.
% You can redefine \AlCapSty, \AlCapFnt, \AlCapNameSty and \AlCapNameFnt with the
% corresponding \Set command. For the Sty commands, you have to give in parameter
% name of a macro (whithout \) which takes one argument. For example,
% \SetAlCapFnt{textbf} defines the default behaviour. If you want to do more
% complicated thing, you should define your own macro and give it to \SetAlCapFnt or
% \SetAlCapNameFnt. Here are two examples:
% - \newcommand{\mycapsty}[1]{\tiny #1}\SetAlCapNameSty{mycapsty}
% - \newcommand{\mycapsty}[1]{\textsl{\small #1}}\SetAlCapNameSty{mycapsty}
% Or you can combine the two, for the last example you can also do:
% \SetAlCapNameSty{textsl}\SetAlCapNameFnt{\small}
% Thanks to Jan Stilhammer who gives me the idea of \AlCapNameFnt.
% * CHANGE \AlTitleFnt to match definition of all other Fnt macros and add a \AlTitleSty
% macro (see below) . Now you set \AlTitleFnt by calling \SetAlTitleFnt with
% directly a macro without parameter in argument:
% Example: \SetAlTitleFnt{\small} to set title in small font.
% * ADD: - \AlTitleSty and \SetAlTitleSty commands to set a style for title. These commands
% are defined from a macro taking the text in argument, as \textbf for example.
% To set the TitleSty you have to give name of the macro (without the '\')
% to \SetAlTitleSty. For example \SetAlTitleSty{textbf} to set \textbf style.
% * ADD: - new command \SetAlgorithmName{algorithmname}{list of algorithms name} which
% redefines name of the algorithms and the sentence list of algorithms. Second
% argument is the name that \autoref, from hyperref package, will use. Example:
% \SetAlgorithmName{Protocol}{List of protocols} if you prefer protocol than
% algorithm.
% * ADD: - new \SetAlgoRefName{QXY} which change the default ref (number of the algorithm) by
% the name given in parameter (QXY in the example).
% * ADD: - new command \SetAlgoRefRelativeSize{-2} which sets the output size of refs, defined
% by \SetAlgoRefName, used in list of algorithms.
% * ADD: - two dimensions to control the layout of caption in ruled, algoruled and boxruled
% algorithms:
% - interspacetitleruled (2pt by defaut) which controls the vertical space between
% rules and title in ruled and algoruled algorithms.
% - interspaceboxruled (2\lineskip by default) which controls the vertical space
% between rules and title in boxruled algorithms.
% These two dimensions can be changed by using \setlength command.
% * ADD: - With the fix (see below) of procedure and function environments, a new feature has
% been added: the name of the procedure or function set in caption is automatically
% defined as a KwFunction and so can be used as a macro. For example, if inside a
% procedure environment you set \caption{myproc()}, you can use \myproc macro in you
% main text. Beware that the macro is only defined after the \caption!
% * ADD: - OPTION nokwfunc to unable the new feature described above in function and
% procedure environment. Useful if you use name of procedure or function that cannot
% be a command name as a math display for example.
% * ADD: - \SetAlgoNlRelativeSize{number} command which sets the relative size of line
% numbers. By default, line numbers are two size smaller than algorithm text. Use
% this macro to change this behavior. For example, \SetAlgoNlRelativeSize{0} sets it
% to the same size, \SetAlgoNlRelativeSize{-1} to one size smaller and
% \SetAlgoNlRelativeSize{1} to one size bigger
% * ADD: - \SetAlgoProcName{aname} command which sets the name of Procedure printed by
% procedure environment (the environment prints Procedure by default). Second
% argument is the name that \autoref, from hyperref package, will use.
% * ADD: - \SetAlgoFuncName{aname} command which sets the name of Function printed by
% procedure environment (the environment prints Function by default). Second
% argument is the name that \autoref, from hyperref package, will use.
% * ADD: - \SetAlgoCaptionLayout{style} command which sets style of the caption; style must
% be the name of a macro taking one argument (the text of the caption). Examples
% below show how to use it:
% . \SetAlgoCaptionLayout{centerline} to have centered caption
% . \SetAlgoCaptionLayout{textbf} to have bold caption
% If you want to apply two styles in the same time, such as centered bold, you have
% to define you own macro and then use \SetAlgoCaptionLayout with its name.
% * ADD: - OPTION procnumbered: which makes the procedure and function to be numbered as
% algorithm
% * ADD: - OPTIONS tworuled and boxruled
% these are two new layouts: tworuled acts like ruled but doesn't put a line after
% caption ; boxruled surround algorithm by a box, puts caption above and add a line
% after caption.
% * REMOVE: - SetKwInParam has been deleted since not useful itself because of different
% macros which can do the same in a better and a more consistent way as
% SetKwFunction or SetKw.
% * FIX: - line number is now correctly vertically aligned with math display.
% * FIX: - references with hyperref. No more same identifier or missing name error. BUT now
% you must NOT use naturalnames option of hyperref packages if you do PdfLaTeX
% * FIX: - autoref with hyperref package (thanks to Jörg Sommer who notices the problem).
% * FIX: - titlenumbered was not working! fixed.
% * FIX: - Else(){} acted like uElse. Corrected.
% * FIX: - noend management: when a block was inside another and end of block was following
% each other, a blank line was added: it's now corrected.
% * FIX: - Function and Procedure environment was no more working as defined originally: the
% label was no more name of the procedure, it acts always as if procumbered option
% has been used.
% * FIX: - line numbers had a fixed size which can be bigger than algorithm text accordingly
% to \AlFnt set (see also new command \SetAlgoNlRelativeSize above)
% * FIX: - semicolon in comments when dontprintsemicolon is used.
% * FIX: - listofalgorithms adds a vertical space before first algo of a chapter as for
% listoffigures or listoftables
% * FIX: - listofalgorithms with twocolumns mode and some classes which don't allow onecolumn
% and so don't define \if@restonecol as prescribed in LaTeX (sig-alternate for
% example)
% * FIX: - algorithm2e now works with elsart cls and some more classes.
% * FIX: - blocks defined by SetKwBlock act now as other blocks (if for instance) and don't
% write end in vlined mode, instead they print a small horizontal line as required
% by the option.
% * FIX: - underfull hbox warning at each end of algorithm environment removed.
%
% * INTERNAL CHANGE: - short end keyword are deduce from long end keyword by keeping the
% first one. Allows to avoid double definition.
% * INTERNAL CHANGE: - procedure, function and algorithm are now resolved by the same
% environment to avoid code duplication.
%
% - October 04 2005 - revision 3.9 -
% * ADD: - \setalcaphskip command which sets the horizontal skip before Algorithm: in caption
% when used in ruled algorithm.
% * ADD: - \SetAlgoInsideSkip command which allows to add an extra vertical space before and
% after the core of the algorithm (ie: \SetAlgoInsideSkip{bigskip})
% * CHANGE: - caption, when used with figure option, is no more controlled by algorithm2e
% package and so follows the exact behaviour of figures. The drawback is that you
% cannot change the typo with AlTitleFnt or CapFnt. The avantage is that if you
% use caption package, it works.
% * FIX: - problem with numbering line and pdflatex
% * FIX: - error when algorithm2e package was used with beamer and listings together
% - February 12 2005 - revision 3.8 -
% * FIX: - extra line with noend option.
% - February 10 2005 - revision 3.7 -
% * ADD: - sidecomment: different macros allowing to put text right after code on the same
% line. They are defined in the same time comment macros are defined with a star
% after the macro name. By default comments are right justified but this can be
% change with appropriate option in the macro. Ex:
% . default: \tcc*{side comment}
% . same as previous: \tcc*[r]{side comment}
% . left justify: \tcc*[l]{side comment}
% . here: \tcc*[h]{side comment} don't put the end of line mark before
% comment (; by default) and don't end the line.
% . flushed: \tcc*[f]{side comment} same as the precedent but right
% justified
% * ADD: - OPTION scright (default): right justified side comments (side comments
% are flushed to the righr)
% * ADD: - OPTION scleft: left justified side comments (side comments are put right after the
% code line)
% * ADD: - \SetSideCommentLeft acts as scleft option
% * ADD: - \SetSideCommentRight acts as scright option
% * ADD: - block like macro side text: all macro defining a block allows now to put text right
% after key words by putting text into (). Done to be used with sidecomment macros,
% but all text can be used.
% Ex: \eIf(\tcc*[f]{then comment}){test}{then text}(else side text){else text}
% * ADD: - OPTION fillcomment (default): end mark of comment is flushed to the right so
% comments fill all the width of text.
% * ADD: - OPTION nofillcomment: end mark of comment is put right after the comment.
% * ADD: - \SetNoFillComment acts as nofillcomment option.
% * ADD: - \SetFillComment acts as fillcomment option.
% * ADD: - OPTION dotocloa: which adds an entry in the toc for the list of algorithms. This
% option load package tocbibind if not already done and so list of figures and list
% of tables are also added in the toc. If you want to control which ones of the lists
% will be added in the toc, please load package tocbibind before package algorithm
% and give it the options you want.
% * FIX: - vertical spacing for uif macro with noend option
% * FIX: - all the compatibility problems between caption and other packages
% * FIX: - typographical differences between list of algorithms and other lists when in
% report or book
%
% - January 24 2005 - revision 3.6 -
% * FIX: - vertical spacing and space characters at the beginning or end of comments.
% line numbers of comments not in the NlSty.
% Thanks to Arnaud Giersch for his comments and suggestions.
% * FIX: - Set*Sty macro: the styles defined was not protected and was modified by surrounding
% context. For example KwTo in a \For{}{} was in bold AND italic instead of just in
% bold.
% * FIX: - line number misplacement after \Indp
%
% - January 21 2005 - revision 3.5 -
% * ADD: - hidden numbering of the lines. Lines are auto-numbered but numbers are shown only
% on lines you specify:
% * linesnumberedhidden option or \LinesNumberedHidden macro activate this
% functionnality.
% * \ShowLn and \ShowLnLabel{lab} macros make the number visible on the
% line. \ShowLnLabel{lab} allows to set a label for this line.
% Thanks to Samson de Jager who makes this suggestion and provides the macros.
% * ADD: - \AlCapFnt and \SetAlCapFnt which allow to have a different font for
% caption. Works like \AlFnt and \SetAlFnt and by default is the same.
% * ADD: - \AlCapSkip skip length. This vertical space is added before caption in plain ou
% boxed mode. It allows to change distance between text and caption.
% * FIX: - caption compatible with IEEEtran class.
% * FIX: - some vertical spacing error with \uIf macros (Thanks to Arnaud Giersch)
% * FIX: - Procedure and Function: lines are also numbered like algorithms
% * FIX: - CommentSty was not used for Comments
%
% - January 10 2005 - revision 3.4 -
% * FIX: - caption compatible with new release of Beamer class.
%
% - June 16 2004 - revision 3.3 -
% * FIX: - Hyperlink references of Hyperref package works now if compiled with pdflatex
% and [naturalnames] option of hyperref package is used.
% * FIX: - algorithm[H] had problem in an list environment - corrected
% * FIX: - interline was not so regular in nested blocks - corrected
% * ADD - \SetVlineSkip macro which sets the vertical skip after the little horizontal
% rule which closes a block in Vlined mode. By default 0.8ex
%
% - June 11 2004 - revision 3.2 - AUTO NUMBERING LINES !!!
% * ADD: auto numbering of the lines (the so asked and so long awaiting feature)
% this feature is managed by 3 options and 3 commands:
% - linesnumbered option: lines of the algo are numbered except for comments and
% input/output (KwInput and KwInOut)
% - commentsnumbered option: makes comments be numbered
% - inoutnumbered option: makes data input/output be numbered
% - \nllabel{lab} labels the line so you can cite with \ref{lab}
% - \LinesNumbered make the following algorithms having auto-numbered lines
% - \linesnotnumbered make the following algorithms having no auto-numbered lines
% * Change: algo2e option renames listofalgorithms in listofalgorithmes
% * FIX: new solution for compatibility with color package, more robust and not tricky.
% Many thanks to David Carlisle for his advices
%
% - June 09 2004 - revision 3.1 -
% * Change: \SetKwSwitch command defines an additionnal macro \uCase and \Case prints end
% * Change: now macros SetKw* do a renewcommand if the keyword is already defined. So you can
% redefine default definition at your own convenience or change your definition
% without introducing a new macro and changing your text.
% * ADD: new macro \SetKwIF which do \SetKwIf and
% \SetKwIfElseIf.The following default definition has been added:
% \SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
% and so you get the macros;
% \If \eIf \lIf \uIf \ElseIf \uElseIf \lElseIf \Else \uElse \lElse
% * ADD: new macro \SetAlgoSkip which allow to fix the vertical skip before and after the
% algorithms. Default is smallskip, do \SetAlgoSkip{} if you don't want an extra space
% or \SetAlgoSkip{medskip} or \SetAlgoSkip{bigskip} if you want bigger space.
% * ADD: macro \SetKwIf defines in addition a new macro \uElse (depending on wat name you
% have given in #2 arg).
% * ADD: macro \SetKwIfElseIf defines in addition a new macro \uElse and \ugElseIf (depending
% on what name you have given in #2 and #3 arg).
% * Change: baseline of algorithm is now top, so two algorithms can be put side by side.
% * FIX: Compatibility with color package solved. The problem was due to a redefinition of
% standard macros by color package. This solves compatibility problem with other
% packages as pstcol or colortbl. (notified by Dirk Fressmann, Antti Tarvainen and Koby
% Crammer)
% * Fix: extra little shift to the right with boxed style algorithm removed (notified by
% P. Tanovski)
% * Fix: algoln option was buggy (notified bye Jiaying Shen)
% * Fix: german and portuges option didn't work due to bad typo (notified by Martin Sievers,
% Thorsten Vitt and Jeronimo Pellegrini)
%
% - February 13 2004 - revision 3.0 -
% * Major revision which makes the package independent from float.sty, so now
% - algorithm* works better, in particular can be used in multicols environments
% - (known bug corrected)
% [H] works now for all sort of environment but is handled differently for classic
% environment and star environment (algorithm, figure, procedure and function). For star
% environment, H acts like for classical figure environment, so it doesn't stay here
% absolutely.
% - (known bug corrected)
% you can use now floatflt package with algorithm package and even with figure
% option. Beware that if you want to put an algorithm inside a floatingfigure, it cannot
% be floating, so [H] is required and then figure option should not be used, since
% standard figure[H] are still floating with LaTeX.
% * boxruled: a new style added. Possible now since no style no more defined by the float
% package.
% * nocaptionofalgo: dosen't print Algorithm #: in the caption for algorithm in ruled or
% algoruled style.
% note: this is just documentation of a macro which was already in the package.
% - December 14 2003 - revision 2.52 -
% * output message shorter
% * French keyword macro \PourTous was missing for longend option, it has been added.
% * TitleofAlgo prints Function or Procedure in corresponding environments.
%
% - October 27 2003 - revision 2.51 - Revision submitted to CTAN archive
% * correction of a minor which make caption in procedure
% and function to be blanck with pdfscreen package
% (thanks to Joel Gossens for the notification)
% * add two internal definition to avoid some errors when
% used with Hyperref package (Hyperref package need to
% define new counter macro from existing ones, and
% don't do it for algorithm2e package, so we do it)
%
% - October 17 2003 - revision 2.50 - first revision for CTAN archive
% * add \AlFnt and \SetAlFnt{font} macros: \AlFnt is used at the beginning of the caption and
% the body of algorithm in order to define the fonts used for typesetting algorithms. You
% can use it elsewhere you want to typeset text as algorithm. For example you can do
% \SetAlFnt{\small\sf} to have algorithms typeset in small sf font. Default is nothing so
% algorithm is typeset as the text of the document.
% * add \AlTitleFnt{text} and \SetAlTitleFnt{font} macros: The {Algorithm: } in the caption is
% typeset with \AlTitleFnt{Algorithm:}. You can use it to have text typeset as {Algorithm:}
% of captions. Default is textbf. Default can be redefined by \SetAlTitleFnt{font}, for
% example you can do \SetAlTitleFnt{emph}
% * add CommentSty typo for text comment.
% * add some compatibility with hyperref package (still an error on multiply defined refs but
% pdf correctly generated)
% * flush text to left in order to have correct indentation even with class as amsart which
% center all figures
% * add german, portuguese and czech options for title of algorithms and typo.
% * add portuguese translation of predefined keywords * add czech translation of some
% predefined keywords
%
% - December 23 2002 - revision 2.40
% * add some French keyword missing
% * add function* and procedure* environment like algorithme* environment: print in one column
% even if twocolumn option is specified for the document.
% * add a new macro \SetKwComment to define macro which writes comments in the text. First
% argument is the name of the macro, second is the text put before the comment, third is the
% text put at the end of the comment.Default are \tcc and \tcp
% * add new options to change the way algo are numbered:
% [algopart] algo are numbered within part (counter must exist)
% [algochapter] algo are numbered within chapter
% [algosection] algo are numbered within section
%
% - March 27 2002 - revision 2.39
% * Gilles Geeraerts: added the \SetKwIfElseIf to manage
% if (c)
% i;
% else if (c)
% i;
% ...
% else
% i;
% end
% * Also added \gIf \gElseIf \gElse.
%
% - January 02 2001 - revision 2.38
% * bugs related to the caption in procedure and function
% environment are corrected.
% * bug related to option noend (extra vertical space added
% after block command as If or For) is corrected.
% * czech option language added (thanks to Libor Bus: l.bus@sh.cvut.cz).
%
% - October 16 2000 - revision 2.37
% * option algo2e added: change the name of environment
% algorithm into algorithm2e. So allow to use the package
% with some journal style which already define an algorithm
% environment.
%
% - September 13 2000 - revision 2.36
% * option slide added: require package color
% * Hack for slide class in order to have correct
% margins
%
% - November 25 1999 - revision 2.35
% * revision number match RCS number
% * Thanks to David A. Bader, a new option is added:
% noend: no end keywords are printed.
%
% - November 19 1999 - revision 2.32
% * minor bug on longend option corrected.
%
% - August 26 1999 - revision 2.31
% * add an option: figure
% this option makes algorithms be figure and so are numbered
% as figures, have Figure as caption and are put in
% the \listoffigures
%
% - January 21 1999 - revision 2.3 beta
% add 2 new environments: procedure and function.
% These environments works like algorithm environment but:
% - the ruled (or algoruled) style is imperative.
% - the caption now writes Procedure name....
% - the syntax of the \caption command is restricted as
% follow: you MUST put a name followed by 2 braces like
% this ``()''. You can put arguments inside the braces and
% text after. If no argument is given, the braces will be
% removed in the title.
% - label now puts the name (the text before the braces in the
% caption) of the procedure or function as reference (not
% the number like a classic algorithm environment).
% There are also two new styles: ProcNameSty and
% ProcArgSty. These style are by default the same as FuncSty
% and ArgSty but are used in the caption of a procedure or a
% function.
%
% - November 28 1996 - revision 2.22
% add a new macro \SetKwInParam{arg1}{arg2}{arg3}:
% it defines a macro \arg1{name}{arg} which prints name in keyword
% style followed byt arg surrounded by arg2 and arg3. The main
% application is to a function working as \SetKwInput to be used
% in the head of the algorithm. For example
% \SetKwInParam{Func}{(}{)} allows
% \Func{functionname}{list of arguments} which prints:
% \KwSty{functioname(}list of arguments\KwSty{)}
%
%
% - November 27 1996 - revision 2.21:
% minor bug in length of InOut boxes fixed.
% add algorithm* environment.
%
% - July 12 1996 - revision 2.2: \SetArg and \SetKwArg macros removed.
%
% \SetArg has been removed since it never has been
% documented.
% \SetKwArg has been removed since \SetKw can now
% take an argument in order to be consistent with
% \SetKwData and \SetKwFunction macros.
%
% - July 04 1996 - revision 2.1: still more LaTeX2e! Minor compatibility break
%
% Macros use now \newcommand instead of \def, use of \setlength,
% \newsavebox, ... and other LaTeX2e specific stuff.
% The compatibility break:
% - \SetData becomes \SetKwData to be more consistent. So the old
% \SetKwData becomes \SetKwInput
% - old macros \titleofalgo, \Freetitleofalgo and \freetitleofalgo
% from LaTeX209 version which did print a warning message and call
% \Titleofalgo in version 2.0 are now removed!
%
% - March 13 1996 - revision 2.0: first official major revision.
%
%
%%%%%%%%%%%%%%
%
% Known bugs:
% -----------
% - horizontal spacing (indent) doesn't work with revtex4 class.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% for more complete informations you can see algorithm2e.tex
%
%
%%%%%%%%%%%%%%%%%%%%%%%% Identification Part %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
%
\ProvidesPackage{algorithm2e}[2017/07/18 v5.2 algorithms environments]
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%% Initial Code %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\@makeother\*% some package redefined it as a letter (as color.sty)
\def\@firstword#1 #2\@nil{#1}% an useful fonction
%%%%%%% Utilities:
% \ifArgumentEmpty<c><t><e> ; if c is empty, i.e. {}, then t, else e.
% Function used internally, but may as well expose it to the user; it's useful
\begingroup
\catcode`\Q=3
\catcode`\T=3
\long\gdef\ALGOCF@argemptyaux#1#2Q#3#4#5T{#4}
\long\gdef\ifArgumentEmpty#1{%
\begingroup\long\edef\@tempa{\endgroup
\ALGOCF@argemptyaux#1QQ{\noexpand\@secondoftwo}{\noexpand\@firstoftwo}T%
}\@tempa
}
\endgroup
% \long\def\ifArgumentEmpty#1{\bgroup
% \catcode`\Q=3
% \catcode`\T=3
% \long\def\@tempa##1##2Q##3##4##5T{##4}%
% \xdef\@tempa{\@tempa#1QQ{\noexpand\@secondoftwo}{\noexpand\@firstoftwo}T}%
% \egroup\@tempa}
%%%%%%%%%%%
%
% \algocf@longdef -> shorthand to reduce
% \expandafter\long\expandafter\def
% to
% \expandafter\algocf@longdef
% may look insignificant, but reads that much better ;)
\def\algocf@longdef{\long\def}
%
% \algocf@newcommand (and helper \algocf@new@command) behaves like LaTeX's newcommand,
% with two differences:
% - the argument is not "\<name>", but rather "<name>"; i.e. one level of indirection
% - if the command exists already, then \renewcommand, rather than \newcommand
\def\algocf@newcommand#1{\expandafter\algocf@new@command\csname#1\endcsname}
\def\algocf@new@command#1{%
\begingroup \escapechar\m@ne\xdef\@gtempa{{\string#1}}\endgroup
\expandafter\@ifundefined\@gtempa\newcommand\renewcommand#1}%
%
% \algocf@newcmdside<name><arity><body>
% The largest time-saver; many commands we define have the pattern:
% \<name>(<side_text>)<argX><argY>...
% where "(<side_text>)" is optional. By defining them with this function,
% the arguments are parsed and renumbered, i.e. the body works as if the
% command was:
% \<name><side_text><argX><argY>...
% and can use \ifArgumentEmpty to see whether its #1 exists (default case
% for the side text is (), so there's no difference between calling
% \foo{bar}
% or
% \foo(){bar}
% Technically this is new behaviour, but it shouldn't really occur...
\algocf@longdef\algocf@newcmdside#1#2#3{%
\expandafter\def\csname#1\endcsname{%
\@ifnextchar({\csname algocf@#1strip\endcsname}{\csname algocf@#1strip\endcsname()}%
}%
\expandafter\algocf@longdef\csname algocf@#1strip\endcsname(##1){\csname algocf@#1main\endcsname{##1}}%
\algocf@newcommand{algocf@#1main}[#2]{#3}%
}
%
% a serie of newcmdside macro for block that allows a comment after the end
% \<name>(<side_text>)<argX><argY>...(<side_text>)%
%
% 1. kind of begin-end : for command with only one block as begin-end
\algocf@longdef\algocf@newcmdside@kobe#1#2{% <name of cmd><body of cmd>
% command starts by checking for side comment at beginning
\expandafter\def\csname#1\endcsname{%
\@ifnextchar({\csname algocf@#1strip\endcsname}{\csname algocf@#1strip\endcsname()}%
}%
% next we strip comment and include it in cmd that get body and check last side comment
\expandafter\algocf@longdef\csname algocf@#1strip\endcsname(##1)##2{\csname algocf@#1checkcomfol\endcsname{##1}{##2}}%
% now check for last side comment; args are : <first side com (could be empty)>, <body of cmd>
\expandafter\algocf@longdef\csname algocf@#1checkcomfol\endcsname##1##2{\@ifnextchar({\csname algocf@#1withendcom\endcsname{##1}{##2}}{\csname algocf@#1noendcom\endcsname{##1}{##2}}}% call macro that manages end of command, with or without last side comment
% finally manage end of macro... with end side comment...
\expandafter\algocf@longdef\csname algocf@#1withendcom\endcsname##1##2(##3){%
\csname algocf@#1main\endcsname{##1}{##2}{##3}}%
% ... or without end side comment
\expandafter\algocf@longdef\csname algocf@#1noendcom\endcsname##1##2{%
\csname algocf@#1main\endcsname{##1}{##2}{}}%
\algocf@newcommand{algocf@#1main}[3]{#2}%
}%
%
% 2. kind of if-else : for command with two blocks as if-then-else
\algocf@longdef\algocf@newcmdside@koif#1#2{% <name of cmd><body of cmd>
% command starts by checking for side comment at beginning
\expandafter\def\csname#1\endcsname{%
\@ifnextchar({\csname algocf@#1strip\endcsname}{\csname algocf@#1strip\endcsname()}%
}%
% next we strip comment and include it in cmd that get body and check last side comment
\expandafter\algocf@longdef\csname algocf@#1strip\endcsname(##1)##2##3{\csname algocf@#1checkcomfol\endcsname{##1}{##2}{##3}}%
% now check for last side comment; args are : <1st side com>, <1st body of cmd>, <2nd body of cmd>
\expandafter\algocf@longdef\csname algocf@#1checkcomfol\endcsname##1##2##3{\@ifnextchar({\csname algocf@#1withendcom\endcsname{##1}{##2}{##3}}{\csname algocf@#1noendcom\endcsname{##1}{##2}{##3}}}% call macro that manages end of command, with or without last side comment
% finally manage end of macro... with end side comment...
\expandafter\algocf@longdef\csname algocf@#1withendcom\endcsname##1##2##3(##4){%
\csname algocf@#1main\endcsname{##1}{##2}{##3}{##4}}%
% ... or without end side comment
\expandafter\algocf@longdef\csname algocf@#1noendcom\endcsname##1##2##3{%
\csname algocf@#1main\endcsname{##1}{##2}{##3}{}}%
\algocf@newcommand{algocf@#1main}[4]{#2}%
}%
%
% \algocf@newcmdsides<name><arity><body><tail><closing>
% Like the command above, but with an optional side text at the end
% of the command as well, i.e.:
% \<name>(<side_text>)<argX><argY>(<end_text>)
% It may be a bit confusing that <arity> doesn't count <end_text>,
% but since it is for internal use, the naming can be a little more
% fuzzy. This function behaves as if:
% <body'> = <body><if end_text: tail{end_text}><closing>
\algocf@longdef\algocf@newcmdsides#1#2#3#4#5{%
\expandafter\def\csname#1\endcsname{%
\@ifnextchar({\csname algocf@#1strip\endcsname}{\csname algocf@#1strip\endcsname()}%
}%