-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithmUtil.sty
173 lines (144 loc) · 4.63 KB
/
algorithmUtil.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
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{algorithmUtil}[2018 v1.0 John Berlin algorithm environment utility macros]
\RequirePackage{kvoptions}
\RequirePackage{xparse}
\RequirePackage{xparse}
\DeclareStringOption[.1em]{topSpace}[.1em] % amount of top space to be applied
\DeclareBoolOption[false]{smallerText} % should the text size used by algorithmic environments be \footnotesize
\DeclareBoolOption[false]{disable} % should the amount of top space added by \algoTopSpace be removed
\ProcessKeyvalOptions*
\@ifpackageloaded{algpseudocode}{}%
{\PackageError{algpseudocode}{The package "algorithmUtil" may not be loaded before "algorithmUtil"}{Load "algorithmUtil" only when "algpseudocode" has been loaded previously}}
%%%
% If hyperref is loaded ensure \autoref has the correct name
%%%
\@ifpackageloaded{hyperref}{
\newcommand{\algorithmautorefname}{Algorithm}
}{}
%%%
% If smallerText boolean switch is enabled make the text size of algorithmic environments \footnotesize
%%%
\ifalgorithmUtil@smallerText
\makeatletter
\algrenewcommand\ALG@beginalgorithmic{\footnotesize}
\makeatother
\fi
%%%
% Adds goto keyword
%%%
\algnewcommand{\algorithmicgoto}{\textbf{goto}}
%%%
% Macro for better goto statement requires label within algorithmic env to work
%%%
\algnewcommand{\Goto}[1]{\algorithmicgoto~\ref{#1}}
%%%
% Adds for each loop
%%%
\algnewcommand{\algorithmicforeach}{\textbf{for each}}
\algdef{S}[FOR]{ForEach}[1]{\algorithmicforeach\ #1\ \algorithmicdo}
%%%
% Makes the \Return statement macro better
%%%
\let\oldReturn\Return
\renewcommand{\Return}{\State\oldReturn}
%%%
% Macro to add vspace (default .1em) before a \begin{algorithm} environment
%%%
\NewDocumentCommand{\algoTopSpace}{}{\ifalgorithmUtil@disable\else\vspace*{\algorithmUtil@topSpace}\fi}
%%%
% Macro wrapper around \texttt for algorithmic environments
%%%
\NewDocumentCommand{\vars}{+m}{\texttt}
%%%
% Macro wrapper around \textrm for algorithmic environments
%%%
\NewDocumentCommand{\func}{+m}{\textrm}
%%%
% Macro for print <arg> statements in algorithmic environments
% Required Arguments 1: the text for <arg>
%%%
\NewDocumentCommand{\Print}{m}{%
\State \textbf{print} #1%
}
%%%
% Macro alias for \Return
%%%
\NewDocumentCommand{\Ret}{\State \textbf{return}}
%%%
% Macro for function calls, <function>(<args>), within statements in algorithmic environments
% Required Arguments 2: <function>, <args>
%%%
\NewDocumentCommand{\call}{m m}{#1(#2)}
%%%
% Macro for function call statements, <function>(<args>), in algorithmic environments
% Required Arguments 2: <function>, <args>
%%%
\NewDocumentCommand{\callS}{m m}{\State #1(#2)}
%%%
% Macro for member function call, <obj>.<function>(<args>), within statements in algorithmic environments
% Required Arguments 3: <obj>, <function>, <args>
%%%
\NewDocumentCommand{\memCall}{m m m}{#1$.$\textbf{#2}(#3)}
%%%
% Macro for member function call statements, <obj>.<function>(<args>), in algorithmic environments
% Required Arguments 3: <obj>, <function>, <args>
%%%
\NewDocumentCommand{\memCallS}{m m m}{\State #1$.$\textbf{#2}(#3)}
%%%
% Macro for a goto statement in algorithmic environments
% Required Arguments 1: <goto ref>
%%%
\NewDocumentCommand{\GT}{m}{\State \Goto{#1}}
%%%
% Macro for a break statement in algorithmic environments
%%%
\NewDocumentCommand{\brk}{\State break}
%%%
% Macro for asignment statement, <a> <- <b>, in algorithmic environments
% Required Arguments 2: <a>, <b>
%%%
\NewDocumentCommand{\Let}{+m +m}{%
\State #1 $\gets$ #2%
}
%%%
% Macro for asignment to index statement, <a>[<index/key>] <- <b>, in algorithmic environments
% Required Arguments 3: <a>, <index/key>, <b>
%%%
\NewDocumentCommand{\IndexAssign}{+m +m m}{%
\State #1[#2] $\gets$ #3%
}
%%%
% Macro for asignment to from index statement, <a> <- <b>[<index/key>], in algorithmic environments
% Required Arguments 3: <a>, <b>, <index/key>
%%%
\NewDocumentCommand{\AssignFromIndex}{+m +m +m}{%
\State #1 $\gets$ #2[#3]%
}
%%%
% Macro for asignment to from something yeilded, <a> <- yield <b>, in algorithmic environments
% Required Arguments 2: <a>, <b>
%%%
\NewDocumentCommand{\YieldS}{+m +m}{%
\State #1 $\gets$ \textbf{yield} #2%
}
%%%
% Macro for yeild statments, yield <a>, in algorithmic environments
% Required Arguments 1: <a>
%%%
\NewDocumentCommand{\Yield}{+m}{%
\State \textbf{yield} #1%
}
%%%
% Macro for increment assignment statments, <a> <- <a>, in algorithmic environments
% Required Arguments 1: <a>
%%%
\NewDocumentCommand{\Increment}{+m}{%
\Let{$#1$}{$#1+1$}%
}
%%%
% Macro for a in b, <a> \in <b>, withing statements in algorithmic environments
% Required Arguments 2: <a>, <b>
%%%
\NewDocumentCommand{\AInB}{+m +m}{%
$#1 \in #2$%
}