summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/algxpar/algxpar.sty
blob: 1ed25143b1cc910fe6579f21f8394b1cca2a422d (plain)
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
\NeedsTeXFormat{LaTeX2e}[2005/12/01]
\ProvidesPackage{algxpar}
[2020/05/30 v0.91 Algorithms with multiline/paragraph support]

%% Package options
\newif\ifaxp@brazilian\axp@brazilianfalse
\DeclareOption{brazilian}{\axp@braziliantrue}
\DeclareOption*{\PackageWarning{algxpar}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax

%% Required packages
\RequirePackage{algorithmicx}
\RequirePackage{algpseudocode}
\RequirePackage{ragged2e}
\RequirePackage{listings}
\RequirePackage{amsmath, amssymb}
\RequirePackage{xcolor}
\RequirePackage{tcolorbox} % to save verbatim
\RequirePackage{fancyvrb} % to load verbatim preserving tabs

%% Constants and identifiers
\algnewcommand\algorithmictrue{True}
\algnewcommand\algorithmicfalse{False}
\algnewcommand\algorithmicnil{Nil}
\algnewcommand\True{\ensuremath{\textsc{\rmfamily \algorithmictrue}}}
\algnewcommand\False{\ensuremath{\textsc{\rmfamily \algorithmicfalse}}}
\algnewcommand\Nil{\ensuremath{\textsc{\rmfamily \algorithmicnil}}}
\newcommand{\Id}[1]{\ensuremath{\textit{\rmfamily#1}}}
\newcommand{\TextString}[1]{\textrm{\normalfont``{\ttfamily\mbox{#1}}''}}
\algnewcommand{\VisibleSpace}{\textrm{\color{black!70}\textvisiblespace}}

%% Headings
\algnewcommand\algorithmicdescription{\textbf{Description}}
\algnewcommand\algorithmicinput{\textbf{Input}}
\algnewcommand\algorithmicoutput{\textbf{Output}}
\algrenewcommand\algorithmicensure{\textbf{Ensure}}
\algrenewcommand\algorithmicrequire{\textbf{Require}}
\algnewcommand\Description{\item[\algorithmicdescription:]}
\algnewcommand\Input{\item[\algorithmicinput:]}
\algnewcommand\Output{\item[\algorithmicoutput:]}
\algrenewcommand\Ensure{\item[\algorithmicensure:]}
\algrenewcommand\Require{\item[\algorithmicrequire:]}

%% Read, write
\algnewcommand{\algorithmicread}{\textbf{read}}
\algnewcommand{\algorithmicwrite}{\textbf{write}}
\algnewcommand\algorithmicset{Set}
\algnewcommand\algorithmicsetto{to}
\algnewcommand{\Set}[2]{\Id{#1} $\gets$ #2}
\algnewcommand{\Setl}[2]{\algorithmicset\ #1 \algorithmicsetto\ #2}
\algnewcommand{\Read}{\algorithmicread}
\algnewcommand{\Write}{\algorithmicwrite}

%% Comments
\newcommand{\axp@commentleftsymbol}{$\triangleright$}
\newcommand{\axp@commentrightsymbol}{$\triangleleft$}
\algnewcommand{\CommentIn}[1]{\axp@commentleftsymbol~%
  \textsl{#1}~\axp@commentrightsymbol}
\algnewcommand{\Commentl}[1]{\axp@commentleftsymbol~\textsl{#1}}
\algrenewcommand{\algorithmiccomment}[1]{%
\def\tmp{#1}%
\ifx\tmp\empty\else%
\hfill\Commentl{#1}%
\fi
}

%% Statep
\newlength{\axp@stateindent}
\setlength{\axp@stateindent}{\dimexpr\algorithmicindent/2\relax}
\algnewcommand{\Statep}[2][]{\State\algparbox[#1]{#2}{\axp@stateindent}}

%% While
\newlength{\axp@whilewidth}
\algblockdefx{While}{EndWhile}%
[2][]{%
\settowidth{\axp@whilewidth}{\algorithmicwhile\ }%
\algparbox[x#1]{\algorithmicwhile\ #2~\algorithmicdo}{\axp@whilewidth}%
}%
{\algorithmicend\ \algorithmicwhile}

%% Repeat
\newlength{\axp@untilwidth}
\algblockdefx{Repeat}{Until}%
{\algorithmicrepeat}%
[2][]{%
\settowidth{\axp@untilwidth}{\algorithmicuntil\ }%
\axp@algparbox{#1}{\algorithmicuntil\ #2}{\axp@untilwidth}{0}%
}

%% If, else if, else
\newlength{\axp@ifwidth}
\newlength{\axp@elseifwidth}
\algblockdefx[If]{If}{EndIf}%
[2][]{%
\settowidth{\axp@ifwidth}{\algorithmicif\ }%
\algparbox[#1]{\algorithmicif\ #2~\algorithmicthen}{\axp@ifwidth}%
}
{\algorithmicend\ \algorithmicif}
\algcblockx[If]{If}{ElsIf}{EndIf}
[2][]{%
\settowidth{\axp@elseifwidth}{\algorithmicelse\ \algorithmicif\ }%
\algparbox[#1]{\algorithmicelse~\algorithmicif\ #2~\algorithmicthen}{\axp@elseifwidth}%
}
{\algorithmicend\ \algorithmicif}
\algcblockx{If}{Else}{EndIf}
{\textbf{\algorithmicelse}}
{\textbf{\algorithmicend~\algorithmicif}}

%% Switch, case, otherwise
\algnewcommand{\algorithmicswitch}{\textbf{switch}}
\algnewcommand{\algorithmicof}{\textbf{of}}
\algnewcommand{\algorithmiccase}{\textbf{case}}
\algnewcommand{\algorithmicotherwise}{\textbf{otherwise}}
\newlength{\axp@switchwidth}
\algblockdefx{Switch}{EndSwitch}%
[2][]{%
\settowidth{\axp@switchwidth}{\algorithmicswitch\ }%
\algparbox[#1]{\algorithmicswitch\ #2~\algorithmicof}{\axp@switchwidth}%
}
{\algorithmicend~\algorithmicswitch}
\newlength{\axp@casewidth}
\algblockdefx{Case}{EndCase}%
[2][]{%
\settowidth{\axp@casewidth}{\algorithmiccase\ }%
\algparbox[#1]{\algorithmiccase\ #2~\algorithmicdo}{\axp@casewidth}%
}
{\algorithmicend~\algorithmiccase}
\algblockdefx{Otherwise}{EndOtherwise}%
{\algorithmicotherwise~\algorithmicdo}%
{\textbf{\algorithmicend\ \algorithmicotherwise}}
\newcommand{\Range}[3][]{%
\ensuremath{%
#2%
\def\temp{#1}%
\mathcal{\ldotp\ldotp}#3
\ifx\temp\empty\relax\else{\ensuremath{\mathcal{:}#1}}\fi%
}%
}

%% For, for each, for all
\algnewcommand{\To}{\textbf{to}}
\algnewcommand{\DownTo}{\textbf{downto}}
\algnewcommand{\Step}{\textbf{step}}
\newlength{\axp@forwidth}
\algblockdefx{For}{EndFor}%
[2][]{%
\settowidth{\axp@forwidth}{\algorithmicfor\ }%
\algparbox[#1]{\algorithmicfor\ #2~\algorithmicdo}{\axp@forwidth}%
}
{\algorithmicend\ \algorithmicfor}
\algnewcommand{\algorithmicforeach}{\textbf{for~each}}
\newlength{\axp@foreachwidth}
\algblockdefx{ForEach}{EndFor}%
[2][]{%
\settowidth{\axp@foreachwidth}{\algorithmicforeach\ }%
\algparbox[#1]{\algorithmicforeach\ #2~\algorithmicdo}{\axp@foreachwidth}%
}
{\algorithmicend\~\algorithmicfor}
\newlength{\axp@forallwidth}
\algblockdefx{ForAll}{EndFor}%
[2][]{%
\settowidth{\axp@forallwidth}{\algorithmicforall\ }%
\algparbox[#1]{\algorithmicforall\ #2~\algorithmicdo}{\axp@forallwidth}%
}%
{\algorithmicend\ \algorithmicfor}

%% Procedure, function, calls
\newlength{\axp@procedurewidth}
\newlength{\axp@namewidth}
\algblockdefx{Procedure}{EndProcedure}%
[3][]{%
\settowidth{\axp@procedurewidth}{\algorithmicprocedure~}%
\settowidth{\axp@namewidth}{\textsc{#2}(}%
\addtolength{\axp@procedurewidth}{0.6\axp@namewidth}%
\algparbox[#1]{\algorithmicprocedure\ \textsc{#2}(#3)}{\axp@procedurewidth}
}%
{\algorithmicend\ \algorithmicprocedure}
\newlength{\axp@functionwidth}
\algblockdefx{Function}{EndFunction}%
[3][]{%
\settowidth{\axp@functionwidth}{\algorithmicfunction~}%
\settowidth{\axp@namewidth}{\textsc{#2}(}%
\addtolength{\axp@functionwidth}{0.6\axp@namewidth}%
\algparbox[#1]{\algorithmicfunction\ \textsc{#2}(#3)}{\axp@functionwidth}
}%
{\algorithmicend\ \algorithmicfunction}
\algrenewcommand\Call[2]{%
\def\argstmp{#2}%
\textsc{#1}\ifx\argstmp\empty\mbox{(\hskip0.5ex)}\else(#2)\fi%
}

%% NewLine
\newcommand{\NewLine}{\\}

%% DefineCode, UseCode, \ShowCode
\newenvironment{DefineCode}[1]
{\begingroup\tcbverbatimwrite{\jobname_code_#1.tmp}}
{\endtcbverbatimwrite\endgroup} 
\newcommand{\UseCode}[1]{\input{\jobname_code_#1.tmp}}
\newcommand{\ShowCode}[2][]{{\small\VerbatimInput[tabsize=4, #1]%
{\jobname_code_#2.tmp}}}

\algrenewcommand{\alglinenumber}[1]%
  {\hspace{-1.5em}\color{black!35}{\scriptsize#1}\raisebox{0.2ex}{\tiny$\blacktriangleright$}}

%% axp@algparbox
\newlength{\axp@commentwidth}
\setlength{\axp@commentwidth}{0pt}
\newcommand{\algparbox}[3][]{\axp@algparbox{#1}{#2}{#3}{1}}

\newlength{\axp@largestcommentwidth}
\setlength{\axp@largestcommentwidth}{0.3\linewidth}
\newcommand{\axp@algparbox}[4]{%
    \def\temp{#1}%
    \ifx\temp\empty%
        \setlength{\axp@commentwidth}{-2em}%
    \else%
        \settowidth{\axp@commentwidth}{\axp@commentleftsymbol\ #1}%
        \ifdim\axp@commentwidth>\axp@largestcommentwidth\relax%
            \setlength{\axp@commentwidth}{\axp@largestcommentwidth}%
        \fi%
    \fi%
    \renewcommand{\NewLine}{\\\hspace{#3}}%
    \parbox[t]{\dimexpr\linewidth-\axp@commentwidth-%
        (\algorithmicindent)*(\theALG@nested - #4)-2em}%
        {\RaggedRight\setlength{\hangindent}{#3}#2\strut}%
    \ifx\temp\empty\else%
        \hfill\axp@commentleftsymbol\hspace{0.5em}%
        \parbox[t]{\axp@commentwidth}{\slshape\RaggedRight#1}%
    \fi%
    \renewcommand{\NewLine}{\\}%
}
\lstset{
literate=
{á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
{Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
{à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
{À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
{ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
{ã}{{\~a}}1 {õ}{{\~o}}1
{Ã}{{\~A}}1 {Õ}{{\~O}}1
{Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
{â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
{Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
{ç}{{\c c}}1 {Ç}{{\c C}}1
{ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
{œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1
{ß}{{\ss}}1
{ű}{{\H{u}}}1 {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
{£}{{\pounds}}1
{«}{{\guillemotleft}}1
{»}{{\guillemotright}}1
{ñ}{{\~n}}1 {Ñ}{{\~N}}1 {¿}{{?`}}1
}

\pgfkeys{
algxpar/.cd,
brazilian/.code = {\axp@languagebrazilian},
english/.code = {\axp@languageenglish},
default/.code = {\axp@languageenglish},
}
\newcommand{\algxparset}[1]{
\pgfkeys{
algxpar/.cd,
#1
}
}
\newcommand{\axp@languagebrazilian}{
\algrenewcommand\algorithmicdescription{\textbf{Descrição}}
\algrenewcommand\algorithmicinput{\textbf{Entrada}}
\algrenewcommand\algorithmicoutput{\textbf{Saída}}
\algrenewcommand\algorithmicrequire{\textbf{Pré}}
\algrenewcommand\algorithmicensure{\textbf{Pós}}
\algrenewcommand{\algorithmicend}{\textbf{fim}}
\algrenewcommand{\algorithmicif}{\textbf{se}}
\algrenewcommand{\algorithmicthen}{\textbf{então}}
\algrenewcommand{\algorithmicelse}{\textbf{senão}}
\algrenewcommand{\algorithmicswitch}{\textbf{escolha}}
\algrenewcommand{\algorithmicof}{\textbf{de}}
\algrenewcommand{\algorithmiccase}{\textbf{caso}}
\algrenewcommand{\algorithmicotherwise}{\textbf{caso~contrário}}
\algrenewcommand{\algorithmicfor}{\textbf{para}}
\algrenewcommand{\algorithmicdo}{\textbf{faça}}
\algrenewcommand{\algorithmicwhile}{\textbf{enquanto}}
\algrenewcommand{\algorithmicrepeat}{\textbf{repita}}
\algrenewcommand{\algorithmicuntil}{\textbf{até que}}
\algrenewcommand{\algorithmicloop}{\textbf{repita}}
\algrenewcommand{\algorithmicforeach}{\textbf{para~cada}}
\algrenewcommand{\algorithmicforall}{\textbf{para~todo}}
\algrenewcommand{\algorithmicfunction}{\textbf{função}}
\algrenewcommand{\algorithmicprocedure}{\textbf{procedimento}}
\algrenewcommand{\algorithmicreturn}{\textbf{retorne}}
\algrenewcommand{\algorithmictrue}{Verdadeiro}
\algrenewcommand{\algorithmicfalse}{Falso}
\algrenewcommand{\algorithmicnil}{Nulo}
\algrenewcommand{\algorithmicread}{\textbf{leia}}
\algrenewcommand{\algorithmicwrite}{\textbf{escreva}}
\algrenewcommand{\algorithmicset}{Defina}
\algrenewcommand{\algorithmicsetto}{como}
\algrenewcommand{\To}{\textbf{até}}
\algrenewcommand{\DownTo}{\textbf{decrescente~até}}
\algrenewcommand{\Step}{\textbf{passo}}
}
\newcommand{\axp@languageenglish}{
\algrenewcommand\algorithmicdescription{\textbf{Description}}
\algrenewcommand\algorithmicinput{\textbf{Input}}
\algrenewcommand\algorithmicoutput{\textbf{Output}}
\algrenewcommand\algorithmicrequire{\textbf{Pre}}
\algrenewcommand\algorithmicensure{\textbf{Post}}
\algrenewcommand{\algorithmicend}{\textbf{end}}
\algrenewcommand{\algorithmicif}{\textbf{if}}
\algrenewcommand{\algorithmicthen}{\textbf{then}}
\algrenewcommand{\algorithmicelse}{\textbf{else}}
\algrenewcommand{\algorithmicswitch}{\textbf{swith}}
\algrenewcommand{\algorithmicof}{\textbf{of}}
\algrenewcommand{\algorithmiccase}{\textbf{case}}
\algrenewcommand{\algorithmicotherwise}{\textbf{otherwise}}
\algrenewcommand{\algorithmicfor}{\textbf{for}}
\algrenewcommand{\algorithmicdo}{\textbf{do}}
\algrenewcommand{\algorithmicwhile}{\textbf{while}}
\algrenewcommand{\algorithmicrepeat}{\textbf{repeat}}
\algrenewcommand{\algorithmicuntil}{\textbf{until}}
\algrenewcommand{\algorithmicloop}{\textbf{loop}}
\algrenewcommand{\algorithmicforeach}{\textbf{for~each}}
\algrenewcommand{\algorithmicforall}{\textbf{for~all}}
\algrenewcommand{\algorithmicfunction}{\textbf{function}}
\algrenewcommand{\algorithmicprocedure}{\textbf{procedure}}
\algrenewcommand{\algorithmicreturn}{\textbf{retorne}}
\algrenewcommand{\algorithmictrue}{True}
\algrenewcommand{\algorithmicfalse}{False}
\algrenewcommand{\algorithmicnil}{Nil}
\algrenewcommand{\algorithmicread}{\textbf{read}}
\algrenewcommand{\algorithmicwrite}{\textbf{write}}
\algrenewcommand{\algorithmicset}{Set}
\algrenewcommand{\algorithmicsetto}{to}
\algrenewcommand{\To}{\textbf{to}}
\algrenewcommand{\DownTo}{\textbf{downto}}
\algrenewcommand{\Step}{\textbf{step}}
}
\axp@languageenglish % default language
\ifaxp@brazilian\algxparset{brazilian}\fi