diff options
author | Norbert Preining <norbert@preining.info> | 2023-06-28 03:01:13 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2023-06-28 03:01:13 +0000 |
commit | b5165768775a2cbc0577b7f8c532cc33d301c28f (patch) | |
tree | cbd889d0de6f6224cb5485c03afde29a2f5eaf89 /macros/latex/contrib/algxpar | |
parent | a0b68c12d9e67182560eb31f202f3e86f0e4a5de (diff) |
CTAN sync 202306280301
Diffstat (limited to 'macros/latex/contrib/algxpar')
-rw-r--r-- | macros/latex/contrib/algxpar/README.md | 22 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar-brazilian.kw.tex | 38 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar-doc.pdf | bin | 0 -> 765435 bytes | |||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar-doc.tex | 1248 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar-english.kw.tex | 52 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar-lzw.tex | 19 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar.dtx | 1181 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar.ins | 36 | ||||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar.pdf | bin | 212046 -> 0 bytes | |||
-rw-r--r-- | macros/latex/contrib/algxpar/algxpar.sty | 834 |
10 files changed, 2209 insertions, 1221 deletions
diff --git a/macros/latex/contrib/algxpar/README.md b/macros/latex/contrib/algxpar/README.md index f69460d098..717f316923 100644 --- a/macros/latex/contrib/algxpar/README.md +++ b/macros/latex/contrib/algxpar/README.md @@ -1,8 +1,13 @@ # algxpar This package extends the algorithmicx package (algpseudocode) to support multiline text. -# Licence -Copyright (C) 2019 by Jander Moreira (moreira.jander@gmail.com) +## How to use +Copy `algxpar.sty` and `*.kw.tex` from `src` to somewhere in your TeX path. + +**Important**: as `algxpar` is included in the CTAN/Tex Live distribution, make sure this versions comes first in the search path when outdated. + +## Licence +Copyright (C) 2019-2023 by Jander Moreira (moreira.jander@gmail.com) This work may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version 1.3 @@ -16,5 +21,14 @@ This work has the LPPL maintenance status `maintained'. The Current Maintainer of this work is Jander Moreira. -This work consists of the files algxpar.dtx and algxpar.ins -and the derived file algxpar.sty. +Files to use the package: +* algxpar.sty +* algxpar-english.kw.tex +* algxpar-brazilian.kw.tex + +Manual: +* algxpar-doc.pdf . + +Files to compile the manual (requires `pygmentize`) +* algxpar-doc.tex +* algxpar-lzw.tex diff --git a/macros/latex/contrib/algxpar/algxpar-brazilian.kw.tex b/macros/latex/contrib/algxpar/algxpar-brazilian.kw.tex new file mode 100644 index 0000000000..899c01b903 --- /dev/null +++ b/macros/latex/contrib/algxpar/algxpar-brazilian.kw.tex @@ -0,0 +1,38 @@ +% Brazilian Portuguese keywords +% Moreira, J. (moreira.jander@gmail.com) +\AlgLanguageSet{brazilian}{ + description = Descrição, + input = Entrada, + output = Saída, + require = {Pré-condição}, + ensure = {Pós-condição}, + end = fim, + if = se, + then = então, + else = senão, + switch = seleção, + of = de, + case = caso, + otherwise = {caso~contrário}, + do = faça, + while = enquanto, + repeat = repita, + until = {até~que}, + loop = {laço}, + foreach = {para~cada}, + forall = {para~todo}, + for = para, + to = até, + downto = {regressivo~até}, + step = passo, + continue = {reinicie~a~repetição}, + break = {interrompa~a~repetição}, + function = função, + procedure = procedimento, + return = retorne, + true = Verdadeiro, + false = Falso, + nil = Nil, + read = leia, + write = escreva, +} diff --git a/macros/latex/contrib/algxpar/algxpar-doc.pdf b/macros/latex/contrib/algxpar/algxpar-doc.pdf Binary files differnew file mode 100644 index 0000000000..04c22e9193 --- /dev/null +++ b/macros/latex/contrib/algxpar/algxpar-doc.pdf diff --git a/macros/latex/contrib/algxpar/algxpar-doc.tex b/macros/latex/contrib/algxpar/algxpar-doc.tex new file mode 100644 index 0000000000..c864162f1e --- /dev/null +++ b/macros/latex/contrib/algxpar/algxpar-doc.tex @@ -0,0 +1,1248 @@ +%! Author = Jander Moreira +%! Email = moreira.jander@gmail.com + +\documentclass[a4paper, 11pt]{article} +\usepackage[T1]{fontenc} + +\usepackage{amsfonts} +\usepackage{amsmath} +\usepackage{textcomp} +\usepackage[all]{nowidow} +\usepackage{tikz} + +% \usepackage{array} + +\usepackage{enumitem} +\setlist{nosep} + +\usepackage[ + brazilian, + language = english, +]{algxpar} + + +%% Layout + +% geometry +\usepackage{geometry} +\geometry{top = 2.5cm, bottom = 2cm, right = 2.5cm, left = 4cm} + +% hyperref +\usepackage{hyperref} +\hypersetup{ + colorlinks, + urlcolor = blue!20!black, + linkcolor = blue!10!black, + citecolor = black!80, +} + +% cleveref +\usepackage{cleveref} + +% makeidx +\usepackage{makeidx} +\makeindex + +% minted +\usepackage[outputdir = ../out]{minted} +% \usemintedstyle{borland} +\newminted{latex}{autogobble, breaklines, bgcolor = blue!5, fontsize = \footnotesize} +\newmintinline{latex}{} +% +% tcolorbox +\usepackage{tcolorbox} +\usepackage{color} +\usepackage{comment} +\tcbuselibrary{skins, listings, minted, breakable} +\tcbset{ + colback = blue!3, + sharp corners, + box align = top, + boxrule = 0pt, + fontupper = \footnotesize, + fontlower = \footnotesize, + minted options={ + fontsize = \footnotesize, + breaklines, + autogobble, + }, + listing engine = minted, +} + +%% Text support + +% macro arguments formats +\colorlet{argumentcolor}{orange!50!black} +\NewDocumentCommand{\Argument}{ m }{% + \textcolor{argumentcolor}{$\langle$\normalfont\small\textsl{#1}$\rangle$}% +} +\NewDocumentCommand{\MArg}{ m }{\mbox{\texttt{\{}\Argument{#1}\texttt{\}}}} +\NewDocumentCommand{\OArg}{ m }{\mbox{\texttt{[}\Argument{#1}\texttt{]}}} +\NewDocumentCommand{\LArg}{ m }{\mbox{\texttt{<}\Argument{#1}\texttt{>}}} +\NewDocumentCommand{\PackageName}{ m }{\mbox{\textsf{#1}}} +\NewDocumentCommand{\Deprecated}{}{\textcolor{red!80!black}{(deprecated)}} +\NewDocumentCommand{\FromPackage}{ m }{% + \tikz\node[draw, rounded corners = 1.5pt, inner sep = 1.5pt, + font = \sffamily\tiny] {#1};% +} +\NewDocumentCommand{\Empty}{}{% + \mbox{\normalfont\textcolor{black!60}{\textsl{--empty--}}} +} +\NewDocumentCommand{\Option}{ m }{% + \mbox{\textcolor{green!40!black}{\texttt{#1}}}% +} +\NewDocumentCommand{\OptionInd}{ m }{% + \index{#1@\texttt{#1}}% + \Option{#1}% +} +\NewDocumentCommand{\OptionRef}{ m }{% + \hyperref[option:#1]{\Option{#1}}% +} +\NewDocumentCommand{\Macro}{ m }{% + \expandafter\latexinline\expandafter{\csname#1\endcsname}% +} +\NewDocumentCommand{\MacroRef}{ m }{% + \hyperref[macro:#1]{\Macro{#1}}% +} +\NewDocumentCommand{\MacroDef}{ m }{% + \index{#1@\texttt{\textbackslash #1}}% + \refstepcounter{MacroCounter}% + \label{macro:#1}% + \Macro{#1}% +} +\NewDocumentCommand{\MacroRefInd}{ m }{% + \index{#1@\texttt{\textbackslash #1}}% + \MacroRef{#1}% +} + +\tcbset{ + description/.style = { + coltitle = black, + fontupper = \normalsize, + colbacktitle = white, + titlerule = 0.001pt, + enhanced jigsaw, + breakable, + width = \dimexpr \linewidth - 2em \relax, + flush right, + top = 0.5ex, + bottom = 0pt, + left = 0pt, + right = 0pt, + opacitybacktitle = 0, + opacityframe = 0, + opacityback = 0, + } +} + +\NewDocumentEnvironment{macro}{ m O{} o }{% + %! formatter = off + \index{#1@\texttt{\textbackslash#1}}% + \refstepcounter{MacroCounter}% + \label{macro:#1}% + %! parser = off + \IfValueTF{#3}{% + \begin{macro*}{#1}{#2} + }{% + \begin{macro*}{#1}{#2}[#3]% + } + %! parser = on + }{% + %! parser = off + \end{macro*} + %! parser = on + %! formatter = on +} +\newcounter{MacroCounter} +\NewDocumentEnvironment{macro*}{ m m o }{ + \medskip\par% + \begin{tcolorbox}[ + title = {\hspace{-2em}\Macro{#1}#2\IfValueT{#3}{\latexinline!{#3} !}}, + description, + ] + }{ + \end{tcolorbox}% + \medskip% +} + +\newlength{\docassignment} +\NewDocumentEnvironment{option}{ m m o }{% + \label{option:#1}% + \settowidth{\docassignment}{#2}% + \begin{tcolorbox} + [ + title = {% + \hspace{-2em}\OptionInd{#1}% + \ifdim\docassignment>0pt\Option{ = #2}\fi% + \IfValueT{#3}{\hfill\textit{Default:} \Option{#3}} + }, + description, + ] + }{ + \end{tcolorbox}% + \medskip% +} +\NewDocumentEnvironment{option*}{ m }{% + \begin{tcolorbox}[title = {\hspace{-2em}#1}, description] + }{ + \end{tcolorbox}% + \medskip% +} +\NewDocumentEnvironment{optionnoind}{ m m }{% + \begin{tcolorbox}[ + title = {\hspace{-2em}\Option{#1 = #2}}, + description, + ] + }{ + \end{tcolorbox}% + \medskip% +} + +%% Repetitive text +\NewDocumentCommand{\MacroOptionsText}{}{% + Any \Argument{options} specified uniquely affect this macro.% +} +\NewDocumentCommand{\BlockOptionsText}{}{% + Any of the \Argument{options} specified in this macro will affect this command and all items in the inner block, propagating up to and including the closing macro.% +} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\title{% + The \PackageName{algxpar} package\thanks{This document corresponds to \PackageName{algxpar}~v\AlgVersion, dated \AlgDate. + This text was last revised \today.}% +} +\author{Jander Moreira \texttt{moreira.jander@gmail.com}} +\date{June 26, 2023} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +\begin{document} +\maketitle +\sloppy + +\begin{abstract} + The \PackageName{algxpar} package is an extension of the \PackageName{algorithmicx}\footnote{\url{https://ctan.org/pkg/algorithmicx}.}/\PackageName{algpseudocode} package to handle multi-line text with proper indentation and provide a number of other improvements. +\end{abstract} + +\tableofcontents + +\begin{figure} + \tcbinputlisting{listing file = algxpar-lzw.tex, listing only} + \vspace{3em} + \tcbinputlisting{listing file = algxpar-lzw.tex, text only} +\end{figure} + + +% \vspace{2em} + +% \changes{v0.9}{2019/10/24}{First version} +% \changes{v0.91}{2020/04/30}{Macro now can be used as super-/subscripts in math formulas, while still preventing hyphenaton in text mode.} +% \changes{v0.91}{2020/06/14}{New macro for assignments, using $\gets$} +% \changes{v0.91}{2020/06/14}{New macro for assignments (verbose)} + + +\section{Introduction} +I teach algorithms and programming and have adopted the \PackageName{algorithmicx} package (\PackageName{algpseudocode}) for writing my algorithms as it provides clear and easy to read pseudocodes with minimal effort to get a visually pleasing code. + +The process of teaching algorithms requires a slightly different use of pseudocode than that normally presented in scientific articles, in which the solutions are presented in a more formal and synthetic way. Students work on more abstract algorithms often preceding the actual knowledge of a programming language, and thus the logic of the solution is more relevant than the variables themselves. Likewise, the use of the development strategy by successive refinements also requires a less programmatic and more verbose code. Thus, when discussing the reasoning for solving a problem, it is common to use sentences such as ``\emph{accumulate current expenses in the total sum of costs}'', because ``${s \gets s + c}$'' is, in this case, too synthetic and necessarily involves knowing how variables work in programs. + +The consequence of more verbose pseudocode leads, however, to longer sentences that often span two or more lines. As pseudocodes, by nature, value visual organization, with regard to control structures and indentations, it became necessary to develop a package that supports the use of commands and comments that could be easily displayed when more than one line was needed. + +The \PackageName{algorithmx} and \PackageName{algpseudocode} packages do not natively support multi-line statements. This package therefore extends several macros to handle multiple lines correctly. Some new commands and a number of features have also been added. + + +\section{Package usage and options}\label{sec:package-usage-and-options} +This package depends on the following packages: + +\begin{center} + \begin{tabular}{ll} + \PackageName{algorithmicx} & (\url{https://ctan.org/pkg/algorithmicx}) \\ + \PackageName{algpseudocode} & (\url{https://ctan.org/pkg/algorithmicx}) \\ + \PackageName{amssymb} & (\url{https://ctan.org/pkg/amsfonts}) \\ + \PackageName{fancyvrb} & (\url{https://ctan.org/pkg/fancyvrb}) \\ + \PackageName{pgfmath} & (\url{https://ctan.org/pkg/pgf}) \\ + \PackageName{pgfopts} & (\url{https://ctan.org/pkg/pgf}) \\ + \PackageName{ragged2e} & (\url{https://ctan.org/pkg/ragged2e}) \\ + \PackageName{tcolorbox} & (\url{https://www.ctan.org/pkg/tcolorbox}) \\ + \PackageName{varwidth} & (\url{https://www.ctan.org/pkg/varwidth}) \\ + \PackageName{xcolor} & (\url{https://www.ctan.org/pkg/xcolor}) \\ + \end{tabular} +\end{center} + +\medskip +To use the package, simply request its use in the preamble of the document. + +\begin{macro*}{usepackage}{\OArg{package options list}}[algxpar] + + Currently, the list of package options includes the following. + + \begin{option*}{\Argument{language name}} + By default, algorithm keywords are developed in English. The English language keyword set is always loaded. When available, other sets of keywords in other languages can be used simply by specifying the language names. The last language in the list is automatically set as the document's default language. + + Currently supported languages: + \begin{itemize} + \item \OptionInd{english} (default language, always loaded) + \item \OptionInd{brazilian} Brazilian Portuguese + \end{itemize} + \end{option*} + + \begin{latexcode} + % Loads Brazilian keyword set and sets it as default + \usepackage[brazilian]{algxpar} + \end{latexcode} + + \begin{optionnoind}{language}{\Argument{language name}} + This option chooses the set of keywords corresponding to \Argument{language name} as the default for the document. This option is available as a general option (see \OptionRef{language}). + + This option is useful when other languages are loaded. + \end{optionnoind} + + \begin{latexcode} + % Loads Brazilian keyword set but keeps English as default + \usepackage[brazilian, language = english]{algxpar} + \end{latexcode} + + \begin{option*}{\Option{noend}} + The \OptionInd{noend} suppresses the line that indicates the end of a block, keeping the indentation. + + See more information in \OptionRef{end} and \OptionInd{noend} options. + \end{option*} + + \begin{latexcode} + % Supresses all end-lines that close a block + \usepackage[noend]{algxpar} + \end{latexcode} +\end{macro*} + + +\section{Writting pseudocode} +Algorithms, following the functionality of the \PackageName{algorithmicx} package, are written within the \latexinline{algorithmic} environment. The possibility of using a number to determine how the lines will be numbered is maintained as in the original version. + +An algorithm is composed of instructions and control structures such as conditionals and loops. And also, some documentation and comments. + +\begin{tcblisting}{} + \begin{algorithmic} + \Description Calculation of the factorial of a natural number + \Input $n \in \mathbb{N}$ + \Output $n!$ + \Statex + \Statep{\Read $n$} + \Statep{$\Id{factorial} \gets 1$}[$0! = 1! = 1$] + \For{$k \gets 2$ \To $n$}[from 2 up] + \Statep{$\Id{factorial} \gets \Id{factorial} \times k$}[$(k-1)! \times k$] + \EndFor + \Statep{\Write \Id{factorial}} + \end{algorithmic} +\end{tcblisting} + +\subsection{A preamble on comments}\label{sec:a-preamble-on-comments} +This is the Euclid's algorithm as provided in the \PackageName{algorithmicx} package documentation\footnote{A label was supressed here.}. + +\begingroup +%! formatter = off +\AlgSet{comment font = {}} +\begin{tcblisting}{} + \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 + \State \textbf{return} $b$\Comment{The gcd is b} + \EndProcedure + \end{algorithmic} +\end{tcblisting} +%! formatter = on +\endgroup + +Comments are added \textit{in loco} with the \MacroRef{Comment} macro, which makes them appear along the right margin. The \PackageName{algxpar} package embeded comments as part of the commands themselves in order to add multi-line support. + +Until \PackageName{algxpar} v0.95, they could be added as an optional parameter before the text, in the style of most \LaTeX\ macros. + +%! formatter = off +\begingroup +\AlgSet{comment font = {}} +\begin{tcblisting}{} + \begin{algorithmic}[1] + \Procedure[The g.c.d. of a and b]{Euclid}{$a,b$} % <-- Comment + \State $r\gets a\bmod b$ + \While[We have the answer if r is 0]{$r\not=0$} % <-- Comment + \State $a\gets b$ + \State $b\gets r$ + \State $r\gets a\bmod b$ + \EndWhile + \Statep[The gcd is b]{\Keyword{return} $b$} % <-- Comment + \EndProcedure + \end{algorithmic} +\end{tcblisting} +\endgroup +%! formatter = on + +Using the comment before the text always bothered me somewhat, as it seemed more natural to put it after. Thus, as of v0.99, the comment can be placed after the text (as the second parameter of the macro), certainly making writing algorithms more user-friendly. To maintain backward compatibility, the use of comments before text is still supported, although it is discouraged. + +In addition to this change, the use of comments in the new format has been extended to most pseudocode macros, such as \MacroRef{EndWhile} for example. + +%! formatter = off +\begingroup +\AlgSet{comment font = {}} +\begin{tcblisting}{} + \begin{algorithmic}[1] + \Procedure{Euclid}{$a,b$}[The g.c.d. of a and b] % <-- Comment + \State $r\gets a\bmod b$ + \While{$r\not=0$}[We have the answer if r is 0] % <-- Comment + \State $a\gets b$ + \State $b\gets r$ + \State $r\gets a\bmod b$ + \EndWhile[end loop] % <-- Comment + \Statep{\Keyword{return} $b$}[The gcd is b] % <-- Comment + \EndProcedure + \end{algorithmic} +\end{tcblisting} +\endgroup +%! formatter = on + +Using \MacroRef{Comment} still produces the expected result, although it may break automatic tracking of longer lines. + +Throughout this documentation, former style comments are denoted as \Argument{comment*}, while the new format uses \Argument{comment}. + +See more about comments in \cref{sec:comments}. + +\subsection{A preamble on options}\label{sec:a-preamble-on-options} +As of version 0.99, a list of options can be added to each command, changing some algorithm presentation settings. These settings are optional and must be entered using angle brackets at the end of the command. + +%! formatter = off +\begin{tcblisting}{} + \begin{algorithmic}<keyword font = \scshape\bfseries, comment width = nice> + \If{$a > b$}[check conditions] + \While{$a > 0$}<keyword color = blue!70> + \Statep{\Call{Process}{$a$}}[process current data] + \EndWhile + \EndIf + \end{algorithmic} +\end{tcblisting} +%! formatter = on + +There is a lot of additional information about options and how they can be used. See discussion and full list in \cref{sec:customization-and-fine-tunning}. + +\subsection{Statements}\label{sec:statements} + +The macros \Macro{State} and \Macro{Statex} defined in \PackageName{algorithmicx} can still be used for single statements and have the same general behaviour. + +For automatic handling of comments and multi-line text, the \Macro{Statep} macro is available, which should be used instead of \Macro{State}. + +\begin{macro}{Statep}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + The \Macro{Statep} macro corresponds to an statement that can extrapolate a single line. The continuation of each line is indented from the baseline and this indentation is based on the value indicated in the \OptionRef{statement indent} option. + + \MacroOptionsText +\end{macro} + +As an example, observe lines~\ref{alg:lzw:output} and \ref{alg:lzw:add-to-table} of the LZW compression algorithm on \cpageref{alg:lzw:add-to-table}. + +\subsection{Flow Control Blocks}\label{sec:flow-control-blocks} +Flow control is essentially based on conditionals and loop. + +\subsubsection{The \Keyword{if} block} +This block is the standard \textit{if} block. + +\begin{tcblisting}{} + \begin{algorithmic} + \State \Read $v$ + \If{$v < 0$}[is it negative?] + \Statep{$v \gets -v$}[make it positive] + \EndIf + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{If}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + \Macro{If} shows \Argument{text} (the condition) and must be closed with an \MacroRef{EndIf}, creating a block of nested commands. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndIf}[\OArg{comment}\LArg{options}] + \Macro{EndIf} closes its respective \MacroRef{If}. + + \MacroOptionsText +\end{macro} + +\begin{macro}{Else}[\OArg{comment}\LArg{options}] + This macro defines the \Keyword{else} part of the \MacroRef{If} statement. + + \BlockOptionsText +\end{macro} + +\begin{macro}{Elsif}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + \Macro{ElsIf} defines the \MacroRef{If} chaining. The argument \Argument{text} is the new condition. + + \BlockOptionsText +\end{macro} + +\subsubsection{The \Keyword{switch} block} + +%! formatter = off +\begin{tcblisting}{} + \begin{algorithmic} + \Statep{Get \Id{option}} + \Switch{\Id{option}} + \Case{1}[inserts new record] + \Statep{\Call{Insert}{\Id{record}}} + \EndCase + \Case{2}[deletes a record] + \Statep{\Call{Delete}{\Id{key}}} + \EndCase + \Otherwise + \Statep{Print ``invalid option''} + \EndOtherwise + \EndSwitch + \end{algorithmic} +\end{tcblisting} +%! formatter = on + + +\begin{macro}{Switch}[\OArg{comment*}\MArg{expression}\OArg{comment}\LArg{options}] + The \Macro{Switch} is closed by a matching \MacroRef{EndSwitch}. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndSwitch}[\OArg{comment}\LArg{options}] + This macro closes a \MacroRef{Switch} block. + + \MacroOptionsText +\end{macro} + +\begin{macro}{Case}[\OArg{comment*}\MArg{constant-list}\OArg{comment}\LArg{options}] + When the result of the \Keyword{switch} expression matches one of the constants in \Argument{constants-list}, then the \Keyword{case} is executed. Usually the \Argument{constant-list} is a single constant, a comma-separated list of constants or some kind of range specification. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndCase}[\OArg{comment}\LArg{options}] + This macro closes a corresponding \MacroRef{Case} statement. + + \MacroOptionsText +\end{macro} + +\begin{macro}{Otherwise}[\OArg{comment}\LArg{options}] + A \Keyword{switch} structure can optionally use an \Keyword{otherwise} clause, which is executed when no previous \Keyword{case}s had a hit. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndOtherwise}[\OArg{comment}\LArg{options}] + This macro closes a corresponding \MacroRef{Otherwise} statement. + + \MacroOptionsText +\end{macro} + +\subsubsection{The \Keyword{for} block} +The \Keyword{for} loop uses \Macro{For} and is also flavored with two variants: \Keyword{foreach} (\Macro{ForEach}) and \Keyword{forall} (\Macro{ForAll}). + +%! parser = off +\begin{tcblisting}{} + \begin{algorithmic} + \For{$i \gets 0$ \To $n$} + \Statep{Do something with $i$} + \EndFor + \ForAll{$\Id{item} \in C$} + \Statep{Do something with \Id{item}} + \EndFor + \ForEach{\Id{item} in queue $Q$} + \Statep{Do something with \Id{item}} + \EndFor + \end{algorithmic} +\end{tcblisting} +%! parser = on + + +\begin{macro}{For}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + The \Argument{text} is used to establish the loop scope. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndFor}[\OArg{comment}\LArg{option}] + This macro closes a corresponding \MacroRef{For}, \MacroRef{ForEach} or \MacroRef{ForAll}. + + \MacroOptionsText +\end{macro} + +\begin{macro}{ForEach}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + Same as \MacroRef{For}. +\end{macro} + +\begin{macro}{ForAll}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + Same as \MacroRef{For}. +\end{macro} + +\subsubsection{The \Keyword{while} block} +\Macro{While} is the loop with testing condition at the top. + +\begin{tcblisting}{} + \begin{algorithmic} + \While{$n > 0$} + \Statep{Do something} + \Statep{$n \gets n - 1$} + \EndWhile + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{While}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + In \Argument{text} is the boolean expression that, when \False, will end the loop. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndWhile}[\OArg{comment}\LArg{options}] + This macro closes a matching \MacroRef{While} block. + + \MacroOptionsText +\end{macro} + +\subsubsection{The \Keyword{repeat}-\Keyword{until} block} +The loop with testing condition at the bottom is the \Macro{Repeat}/\Macro{Until} block. + +\begin{tcblisting}{} + \begin{algorithmic} + \Repeat + \Statep{Do something} + \Statep{$n \gets n - 1$} + \Until{$n \leq 0$} + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{Repeat}[\OArg{comment}\LArg{options}] + This macro starts the \Keyword{repeat} loop, which is closed with \MacroRef{Until}. + + \BlockOptionsText +\end{macro} + +\begin{macro}{Until}[\OArg{comment*}\MArg{text}\OArg{comment}\LArg{options}] + In \Argument{text} is the boolean expression that, when \MacroRef{True}, will end the loop. + + \MacroOptionsText +\end{macro} + +\subsubsection{The \Keyword{loop} block} +A generic loop is build with \Macro{Loop}. + +\begin{tcblisting}{} + \begin{algorithmic} + \Loop + \Statep{Do something} + \Statep{$n \gets n + 1$} + \If{$n$ is multiple of 5} + \Statep{\Continue}[restarts loop] + \EndIf + \Statep{Do something else} + \If{$n \leq 0$} + \Statep{\Break}[ends loop] + \EndIf + \Statep{Keep working} + \EndLoop + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{Loop}[\OArg{comment}\LArg{options}] + The generic loop starts with \Macro{Loop} and ends with \MacroRef{EndLoop}. Usually the infinite loop is interrupted by and internal \MacroDef{Break} or restarted with \MacroDef{Continue}. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndLoop}[\OArg{comment}\LArg{options}] + \Macro{EndLoop} closes a matching \MacroRef{Loop} block. + + \MacroOptionsText +\end{macro} + +\subsection{Constants and Identifiers}\label{sec:constants-and-identifiers} +A few macros for well known constants were defined: \MacroDef{True} (\True), \MacroDef{False} (\False), and \MacroDef{Nil} (\Nil). + +The macro \Macro{Id} was created to handle ``program-like'' named identifiers, such as \Id{sum}, \Id{word\_counter} and so on. + +\begin{macro}{Id}[\MArg{identifier}] + Identifiers are in italics: \latexinline!\Id{value}! is \Id{value}. Its designed to work in both text and math modes: \latexinline!$\Id{offer}_k$! is $\Id{offer}_k$. +\end{macro} + +\subsection{Assignments and I/O}\label{sec:assignments-and-i/o} + +To support teaching-like, basic pseudocode writing, the macros \MacroDef{Read} and \MacroDef{Write} are provided. + +\begin{tcblisting}{} + \begin{algorithmic} + \Statep{\Read $v_1, v_2$} + \Statep{$\Id{mean} \gets \dfrac{v_1 + v_2}{2}$}[calculate] + \Statep{\Write \Id{mean}} + \end{algorithmic} +\end{tcblisting} + +The macro \Macro{Set} can be used for assignments. + +\begin{macro}{Set}[\MArg{lvalue}\MArg{expression} \Deprecated] + This macro expands to \MacroRef{Id}\latexinline!{#1} \gets #2!. + + As the handling of text and math modes should be done and its usage brings no evident advantage, this macro will no longer be supported. It will be kept as is for backward compatibility however. +\end{macro} + +\subsection{Procedures and Functions}\label{sec:procedures-and-functions} +Modularization uses \Macro{Procedure} or \Macro{Function}. + +\begin{tcblisting}{} + \begin{algorithmic} + \Procedure{SaveNode}{\Id{node}} + [saves a B\textsuperscript{+}-tree node to disk] + \If{\Id{node}.\Id{is\_modified}} + \If{$\Id{node}.\Id{address} == -1$} + \Statep{Set file writting position after file's last byte}[creates a new node on disk] + \Else + \Statep{Set file writting position to \Id{node}.\Id{address}}[updates the node] + \EndIf + \Statep{Write \Id{node} to disk} + \Statep{$\Id{node}.\Id{is\_modified} \gets \False$} + \EndIf + \EndProcedure + \end{algorithmic} +\end{tcblisting} + +\begin{tcblisting}{} + \begin{algorithmic} + \Function{Factorial}{$n$}[$n \geq 0$] + \If{$n \in \{0, 1\}$} + \Statep{\Return $1$}[base case] + \Else + \Statep{\Return $n \times \Call{Factorial}{n-1}$}[recursive case] + \EndIf + \EndFunction + \end{algorithmic} +\end{tcblisting} + + +\begin{macro}{Procedure}[\MArg{name}\MArg{argument list}\OArg{comment}\LArg{options}] + This macro creates a \Keyword{procedure} block that must be ended with \MacroRef{EndProcedure}. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndProcedure}[\OArg{comment}\LArg{optons}] + This macro closes the \MacroRef{Procedure} block. + + \MacroOptionsText +\end{macro} + +\begin{macro}{Function}[\MArg{name}\MArg{argument list}\OArg{comment}\LArg{options}] + This macro creates a \Keyword{function} block that must be ended with \MacroRef{EndFunction}. A \MacroDef{Return} is defined. + + \BlockOptionsText +\end{macro} + +\begin{macro}{EndFunction}[\OArg{comment}\LArg{optons}] + This macro closes the \MacroRef{Function} block. + + \MacroOptionsText +\end{macro} + +For calling a procedure or function, \Macro{Call} should be used. + +\begin{macro}{Call}[\MArg{name}\MArg{arguments}\LArg{options}] + \label{call} + \Macro{Call} is used to state a function or procedure call. The module's \Argument{name} and \Argument{arguments} are mandatory. + + \MacroOptionsText +\end{macro} + +\subsection{Comments}\label{sec:comments} + +The \Macro{Comment} macro defined by \PackageName{algorithmicx} has the same original behavior and has been redefined to handle styling options. + +\begin{macro}{Comment}[\MArg{text}\LArg{options}] + The redesigned version of \Macro{Comment} can be used with \MacroRef{State}, \MacroRef{Statex} and \MacroRef{Statep}. When used with \MacroRef{Statep}, it must be enclosed inside the text braces, but multi-line statements should work differently than expected. + + \MacroOptionsText +\end{macro} + +%! formatter = off +\begin{tcblisting}{sidebyside = false} + \begin{minipage}{7.5cm} + \begin{algorithmic}<comment color = blue>% for viewing purposes only + \State Store the value zero in variable $x$\Comment{first assignment} + \Statep{Store the value zero in variable $x$\Comment{first assignment}} + \Statep{Store the value zero in variable $x$}[first assignment]% best choice + \end{algorithmic} + \end{minipage} +\end{tcblisting} +%! formatter = on + +\begin{macro}{Commentl}[\MArg{text}\LArg{options}] + While \MacroRef{Comment} pushes text to the end of the line, the macro \Macro{Commentl} is ``local''. In other words, it just puts a comment in place. + + Local comments follows regular text and no line changes are checked. + + \MacroOptionsText +\end{macro} + +\begin{tcblisting}{} + \begin{algorithmic} + \If{$a > 0$~~\Commentl{special case}\\ + or\\ + $a < b$~~\Commentl{general case}\\} + \Statep{Process data~~\Commentl{may take a while}} + \EndIf + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{CommentIn}[\MArg{text}\LArg{options}] + \Macro{CommentIn} is an alternative to \textit{line comments} which usually extends to the end of the line. This macro defines a comment with a begin and an end. A comment starts with \CommentSymbol\ and ends with \CommentSymbolRight. + + \MacroOptionsText +\end{macro} + + +\begin{tcblisting}{} + \begin{algorithmic} + \If{$a > 0$ \CommentIn{special case} or $a < b$ \CommentIn{general case}} + \Statep{Process data~~\Commentl{may take a while}} + \EndIf + \end{algorithmic} +\end{tcblisting} + +\subsection{Documentation}\label{sec:documentation} +A series of macros are defined to provide the header documentation for a pseudocode. + +\begin{tcblisting}{} + \begin{algorithmic} + \Description Calculation of the factorial of a natural number through successive multiplications + \Require $n \in \mathbb{N}$ + \Ensure $f = n!$ + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{Description} + [~\Argument{description text}] + The \Macro{Description} is intended to hold the general description of the pseudocode. +\end{macro} + +\begin{macro}{Require} + [~\Argument{pre-conditions}] + The required initial state that the code relies on. These are \textit{pre-conditions}. +\end{macro} + +\begin{macro}{Ensure} + [~\Argument{post-conditions}] + The final state produced by the code. These are \textit{post-conditions}. +\end{macro} + +\begin{tcblisting}{} + \begin{algorithmic} + \Description Calculation of the factorial of a natural number through successive multiplications + \Input $n$ (integer) + \Output $n!$ (integer) + \end{algorithmic} +\end{tcblisting} + +\begin{macro}{Input} + [~\Argument{inputs}] + This works as an alternative to \MacroRef{Require}, presenting \Keyword{input}. +\end{macro} +\begin{macro}{Output} + [~\Argument{outputs}] + This works as an alternative to \MacroRef{Ensure}, presenting \Keyword{output}. +\end{macro} + + +\section{Customization and Fine Tunning}\label{sec:customization-and-fine-tunning} +As of version 0.99 of \PackageName{algxpar}, a series of options have been introduced to customize the presentation of algorithms. Colors and fonts that only apply to keywords, for example, can be specified, providing an easier and more convenient way to customize each algorithm. + +The \MacroRef{AlgSet} macro serves this purpose. + +\begin{macro}{AlgSet}[\MArg{options list}] + This macro sets algorithmic settings as specified in the \Argument{options list}, which is key/value comma-separated list. + + All settings will be applied to the entire document, starting from the point of the macro call. The scope of a definition made with \MacroRef{AlgSet} can be restricted to a part of the document simply by including it in a \TeX\ group. +\end{macro} + +\begingroup +\begin{tcblisting}{} + \AlgSet{algorithmic indent = 1.5cm} + \begin{algorithmic} + \Statep{\Read $k$} + \If{$k < 0$} + \Statep{$k \gets -k$} + \EndIf + \Statep{\Write $k$} + \end{algorithmic} +\end{tcblisting} +\endgroup% ends the scope + +If the settings are only applied to a single algorithm and not a group of algorithms in a text section, the easiest way is to include the options in the \texttt{algorithmicx} environment. + +%! formatter = off +\begin{tcblisting}{} + \begin{algorithmic}<keyword font = \sffamily\bfseries\itshape> + \Statep{\Read $k$} + \If{$k < 0$} + \Statep{$k \gets -k$} + \EndIf + \Statep{\Write $k$} + \end{algorithmic} +\end{tcblisting} +%! formatter = on + +Named styles can also be defined using the \PackageName{pgfkeys} syntax. + +%! formatter = off +\begin{tcblisting}{} + \AlgSet{ + fancy/.style = { + text color = green!40!black, + keyword color = blue!75!black, + comment color = brown!80!black, + comment symbol = \texttt{//}, + } + } + \begin{algorithmic}<fancy> + \Statep{\Commentl{Process $k$}} + \Statep{\Read $k$} + \If{$k < 0$} + \Statep{$k \gets -k$}[back to positive] + \EndIf + \Statep{\Write $k$} + \end{algorithmic} +\end{tcblisting} +%! formatter = on + +Sometimes some settings need to be applied exclusively to one command, for example to highlight a segment of the algorithm. + +%! formatter = off +\begin{tcblisting}{} + \AlgSet{ + highlight/.style = { + text color = red!60!black, + keyword color = red!60!black, + } + } + \begin{algorithmic} + \Statep{\Commentl{Process $k$}} + \Statep{\Read $k$} + \If{$k < 0$}<highlight> + \Statep{$k \gets -k$}[back to positive] + \EndIf + \Statep{\Write $k$} + \end{algorithmic} +\end{tcblisting} +%! formatter = on + +\subsection{Options}\label{sec:options} +This section presents the options that can be specified for the algorithms, either using \MacroRef{AlgSet} or the \Argument{options} parameter of the various macros. + +\begin{option}{language}{\Argument{language}}[english] + This key is used to choose the keyword language set for the current scope. The language keyword set should already have been loaded through the package options (see \cref{sec:package-usage-and-options}). +\end{option} + +\begin{option}{noend}{} + Structured algorithms use blocks for its structures, marking their begin and end. In pseudocode it is common to use a line to finish a block. + Using the option \Option{end}, this line is suppressed. + + The result is similar to a program written in Python. +\end{option} + +\begin{option}{end}{} + This option reverses the behaviour of \OptionRef{end}, and the closing line of a block presented. +\end{option} + +\begin{tcblisting}{} + \begin{algorithmic} + <noend> + \For{$i \gets 0$ \To $N - 1$} + \For{$j \gets$ \To $N - 1$} + \If{$m_{ij} < 0$} + <end> + \Statep{$m_{ij} \gets 0$} + \EndIf + \EndFor + \EndFor + \end{algorithmic} +\end{tcblisting} + +\begin{option}{keywords}{\Argument{list of keywords assignments}} + This option allows to change a keyword (or define a new one). See \cref{sec:languages-and-translations} for more information on keywords and translations. +\end{option} + +%! formatter = off +\begin{tcblisting}{} + \begin{algorithmic}< + keywords = { + terminate = Terminate, % new keyword + then = \{, % redefined + endif = \}, % redefined + while = whilst, % redefined + } + > + \While{\True} + \If{$t < 0$} + \Statep{Run the \Keyword{terminate} module} + \EndIf + \EndWhile + \end{algorithmic} +\end{tcblisting} +%! formatter = on + +\begin{option}{algorithmic indent}{\Argument{width}}[1em] + The algorithmic indent is the amount of horizontal space used for indentation inner commands. + + This option actually sets the \PackageName{algorithmicx}'s \Macro{algorithmicindent}. +\end{option} + +\begin{option}{comment symbol}{\Argument{symbol}}[\Macro{triangleright}] + The default symbol that preceeds the text in comments is \Macro{triangleright} (\CommentSymbol), as used by \PackageName{algorithmicx}, and can be changed with this key. + + The current comment symbol is available with \MacroDef{CommentSymbol}. Do not change this symbol by redefining \Macro{CommentSymbol}, as font, shape and color settings will no longer be respected. Always use \Option{comment symbol}. +\end{option} + +\begin{option}{comment symbol right}{\Argument{symbol}}[\Macro{triangleleft}] + This is the symbol that closes a \MacroRef{CommentIn}. This symbol is set to \CommentSymbolRight\ and can be retrieved with the \MacroDef{CommentSymbolRight} macro. Do not attempt to change the symbol by redefining \Macro{CommentSymbolRight}, as font, shape and color settings will no longer be respected. Always use \Option{comment symbol right}. +\end{option} + +\subsubsection{Fonts, shapes and sizes} +The options ins this section allows setting font family, shape, weight and size for several parts of an algorithm. + +Notice that color are handled separately (see \cref{sec:colors}) and using \Macro{color} with font options will tend to break the document. + +\begin{option}{text font}{\Argument{font, shape and size}}[\Empty] + This setting corresponds to the font family, its shape and size and applies to the \Argument{text} field in each of the commands. +\end{option} + +\begin{option}{comment font}{\Argument{font, shape and size}}[\Macro{slshape}] + This setting corresponds to the font family, its shape and size and applies to all comments. +\end{option} + +\begin{option}{keyword font}{\Argument{font, shape and size}}[\Macro{bfseries}] + This setting sets the font family, shape, and size, and applies to all keywords, such as \Keyword{function} or \Keyword{end}. +\end{option} + +\begin{option}{constant font}{\Argument{font, shape and size}}[\Macro{scshape}] + This setting sets the font family, shape, and size, and applies to all constants, such as \Nil, \True\ and \False. + + This setting also applies when \MacroRef{Constant} is used. +\end{option} + +\begin{option}{module font}{\Argument{font, shape and size}}[\Macro{scshape}] + This setting sets the font family, shape, and size, and applies to both procedure and function identifiers, as well as their callings with \MacroRef{Call}. +\end{option} + +\subsubsection{Colors}\label{sec:colors} +Colors are defined using the \PackageName{xcolors} package. + +\begin{option}{text color}{\Argument{color}} + [.\mbox{\normalfont\normalcolor\normalsize~(dot)}] + This setting corresponds to the color that applies to the \Argument{text} field in each of the commands. +\end{option} + +\begin{option}{comment color}{\Argument{color}}[.!70] + This setting corresponds to the color that applies to all comments. +\end{option} + +\begin{option}{keyword color}{\Argument{color}} + [.\mbox{\normalfont\normalcolor\normalsize~(dot)}] + This key is used to set the color for all keywords. +\end{option} + +\begin{option}{constant color}{\Argument{color}} + [.\mbox{\normalfont\normalcolor\normalsize~(dot)}] + This setting corresponds to the color that applies to the defined constant (see \cref{sec:constants-and-identifiers}) and also when macro \MacroRef{Constant} is used. +\end{option} + +\begin{option}{module color}{\Argument{color}} + [.\mbox{\normalfont\normalcolor\normalsize~(dot)}] + This color is applied to the identifier used in both \MacroRef{Procedure} and \MacroRef{Function} definitions, as well as module calls with \MacroRef{Call}. Notice that the arguments use \Option{text color}. +\end{option} + +\subsubsection{Paragraphs} +Multi-line support are internally handled by \Macro{parbox}es. + +\medskip +%! formatter = off +\begingroup +\begin{algorithmic}<show boxes> + \Procedure{Euclid}{$a,b$}[The g.c.d. of a and b] + \Statep{$r\gets a\bmod b$} + \While{$r\not=0$}[We have the answer if r is 0] + \Statep{$a\gets b$} + \Statep{$b\gets r$} + \Statep{$r\gets a\bmod b$} + \EndWhile + \Statep{\Keyword{return} $b$}[The g.c.d. is b] + \EndProcedure +\end{algorithmic} +\endgroup +%! formatter = on + +\medskip +The options in this section should be used to set how these paragraphs will be presented. + +\begin{option}{text style}{\Argument{style}}[\Macro{RaggedRight}] + This \Argument{style} is applied to the paragraph box that holds the \Argument{text} field in all commands. +\end{option} + +\begin{option}{comment style}{\Argument{style}}[\Macro{RaggedRight}] + This \Argument{style} is applied to the paragraph box that holds the \Argument{comment} field in all algorithmic commands. This setting will not be used with \MacroRef{Comment}, \MacroRef{Commentl} or \MacroRef{CommentIn}. +\end{option} + +\begin{option}{comment separator width}{\Argument{width}}[1em] + The minimum space between the text box and the \Macro{CommentSymbol}. This affects the available space in a line for keywords, text and comment. +\end{option} + +\begin{option}{statement indent}{\Argument{width}}[1em] + This is the \Macro{hangindent} set inside \MacroRef{Statep} statements. +\end{option} + +\begin{option}{comment width}{\Option{auto}|\Option{nice}|\Argument{width}}[auto] + There are two ways to balance the lengths of \Argument{text} and \Argument{comments} on a line, each providing different visual experiences. + + In automatic mode (\Option{auto}), the balance is chosen considering the widths that the actual text and comment have, trying to reduce the total number of lines, given there is not enough space in a single line for the keywords, text , comment and comment symbol. The consequence is that each line with a comment will have its own balance. + + The second mode, \Option{nice}, sets a fixed width for the entire algorithm, maintaining consistency across all comments. In that case, longer comments will tend to span a larger number of lines. The ``nice value'' is hardcoded and sets the comment width to \latexinline{0.4\linewidth}. + + Also, a fixed comment width can be specified. +\end{option} + +% todo: insert the parameter indent when it starts working +\begin{comment} + parameter indent = 0pt, +\end{comment} + +\subsection{Languages and translations}\label{sec:languages-and-translations} +A simple mechanism is employed to allow keywords to be translated into other languages. + +%! formatter = off +\begingroup +\begin{tcblisting}{} + \begin{algorithmic}<language = brazilian> + \Procedure{Euclid}{$a,b$} + \State $r\gets a\bmod b$ + \While{$r\not=0$} + \State $a\gets b$ + \State $b\gets r$ + \State $r\gets a\bmod b$ + \EndWhile + \Statep{\Keyword{return} $b$} + \EndProcedure + \end{algorithmic} +\end{tcblisting} +\endgroup +%! formatter = on + + +Creating a new keyword set uses the \Macro{AlgLanguageSet} macro. + +\begin{macro}{AlgLanguageSet}[\MArg{language name}\MArg{keyword assignments}] + This macro sets new values for known keywords as well as new ones. Once created, keywords cannot be deleted. + + In case a default keyword is not reset, the English version will be used. + + To create a new set, copy the file \texttt{algxpar-english.kw.tex} and edit it accordingly. + + Note that there is a set of keywords for the lines that close each block. These keys are provided to allow for more versatility in changing how these lines are presented. It is highly recommended that references to other keywords use the {Keyworkd} macro so that font, color and language changes can be made without any problems. + + In translations, these \textit{compound keywords} do not necessarily need to appear (see file \texttt{brazilian.kw.tex}, which follows the settings in \texttt{algxpar-english.kw.tex}). However, if defined, there will be different versions for each language. +\end{macro} + +\begin{figure} + \tcbinputlisting{listing file = algxpar-english.kw.tex, listing only} +\end{figure} + +The mechanism behind \MacroRef{AlgLanguageSet} uses the \Macro{SetKeyword} macro, which is called to adjust the value of a single keyword\footnote{Macros like \Macro{algorithmicwhile} from the \PackageName{algorithimicx} package are no longer used.}. To retrieve the value of a given keyword, the \Macro{Keyword} macro must be used. It returns the formatted value according to the options currently in use for keywords. + +\begin{macro}{SetKeyword}[\OArg{language}\MArg{keyword}\MArg{value}] + The macro \MacroRef{SetKeyword} changes a given \Argument{keyword} to \Argument{value} if it exists; otherwise a new keyword is created. + + If \Argument{language} is omitted, the language currently in use is changed. + + See also the \OptionRef{keywords} option. +\end{macro} + +\begin{macro}{Keyword}[\OArg{language}\MArg{keyword}] + This macro expands to the value of a keyword in a \Argument{language} using the font, shape, size, and color determined for the keyword set. + + If \Argument{language} is not specified, the current language is used. \Argument{keyword} is any keyword defined for a language, including custom ones. +\end{macro} + +\begin{tcblisting}{} + \SetKeyword[german]{if}{wenn} % new + Depending on the language, a keyword can take different forms: \Keyword{if} (English), \Keyword[german]{if} (german) or \Keyword[brazilian]{if} (Brazilian Portuguese). +\end{tcblisting} + +\subsection{Other features} + +\begin{macro}{Constant}[\OArg{name}] + This macro presents \Argument{name} using font, shape, size and color defined for constants. +\end{macro} + +\begin{macro}{Module}[\OArg{name}] + This macro presents \Argument{name} using font, shape, size and color defined for procedures and functions. +\end{macro} + + +\section{To do} +This is a \textit{todo} list: +\begin{itemize} + \item Add font, shape, size and color settings to a whole algorithm; + \item Add font, shape, size and color settings to line numbers; + \item Add font, shape, size and color settings to identifiers. +\end{itemize} + + +\section{Examples} + +\subsection{LZW revisited} +\begingroup +\begin{latexcode} + \AlgSet{ + comment color = purple, + comment width = nice, + comment style = \raggedleft, + } +\end{latexcode} + +\AlgSet{ + comment color = purple, + comment width = nice, + comment style = \raggedleft, +} +\input{algxpar-lzw} +\endgroup + +\subsection{LZW revisited again} +\begingroup +\begin{latexcode} + \AlgSet{ + keyword font = \ttfamily, + keyword color = green!40!black, + text font = \itshape, + comment font = \footnotesize, + algorithmic indent = 1.5em, + noend, + } +\end{latexcode} + +\AlgSet{ + keyword font = \ttfamily, + keyword color = green!40!black, + text font = \itshape, + comment font = \footnotesize, + algorithmic indent = 1.5em, + noend, +} +\input{algxpar-lzw} +\endgroup + + +%% Index +\printindex + +\end{document} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ No newline at end of file diff --git a/macros/latex/contrib/algxpar/algxpar-english.kw.tex b/macros/latex/contrib/algxpar/algxpar-english.kw.tex new file mode 100644 index 0000000000..55505ff5e6 --- /dev/null +++ b/macros/latex/contrib/algxpar/algxpar-english.kw.tex @@ -0,0 +1,52 @@ +% English keywords +% Moreira, J. (moreira.jander@gmail.com) +\AlgLanguageSet{english}{% + description = Description, + input = Input, + output = Output, + require = Require, + ensure = Ensure, + end = end, + if = if, + then = then, + else = else, + switch = switch, + of = of, + case = case, + otherwise = otherwise, + do = do, + while = while, + repeat = repeat, + until = until, + loop = loop, + foreach = {for~each}, + forall = {for~all}, + for = for, + to = to, + downto = {down~to}, + step = step, + continue = continue, + break = break, + function = function, + procedure = procedure, + return = return, + true = True, + false = False, + nil = Nil, + read = read, + write = write, + set = set, +} + +% Compound keywords +\AlgLanguageSet{english}{ + endwhile = \Keyword{end}~\Keyword{while}, + endfor = \Keyword{end}~\Keyword{for}, + endloop = \Keyword{end}~\Keyword{loop}, + endif = \Keyword{end}~\Keyword{if}, + endswitch = \Keyword{end}~\Keyword{switch}, + endcase = \Keyword{end}~\Keyword{case}, + endotherwise = \Keyword{end}~\Keyword{otherwise}, + endprocedure = \Keyword{end}~\Keyword{procedure}, + endfunction = \Keyword{end}~\Keyword{function}, +}
\ No newline at end of file diff --git a/macros/latex/contrib/algxpar/algxpar-lzw.tex b/macros/latex/contrib/algxpar/algxpar-lzw.tex new file mode 100644 index 0000000000..37fd820c5f --- /dev/null +++ b/macros/latex/contrib/algxpar/algxpar-lzw.tex @@ -0,0 +1,19 @@ +\begin{algorithmic}[1] + \Description LZW Compression using a table with all known sequences of bytes. + \Input A flow of bytes + \Output A flow of bits with the compressed representation of the input bytes + \Statex + \Statep{Initialize a table with all bytes}[each position of the table has a single byte] + \Statep{Initilize \Id{sequence} with the first byte in the input flow} + \While{there are bytes in the input}[wait until all bytes are processed] + \Statep{Get a single byte from input and store it in \Id{byte}} + \If{the concatention of \Id{sequence} and \Id{byte} is in the table} + \Statep{Set \Id{sequence} to $\Id{sequence} + \Id{byte}$}[concatenate without producing any output] + \Else + \Statep{Output the code for \Id{sequence}}[i.e., the binary representation of its position in the table]\label{alg:lzw:output} + \Statep{Add the concatention of \Id{sequence} and \Id{byte} to the table}[the table learns a longer sequence]\label{alg:lzw:add-to-table} + \Statep{Set \Id{sequence} to \Id{byte}}[starts a new sequence with the remaining byte] + \EndIf + \EndWhile + \Statep{Output the code for \Id{sequence}}[the remaining sequence of bits] +\end{algorithmic}
\ No newline at end of file diff --git a/macros/latex/contrib/algxpar/algxpar.dtx b/macros/latex/contrib/algxpar/algxpar.dtx deleted file mode 100644 index d1263d7997..0000000000 --- a/macros/latex/contrib/algxpar/algxpar.dtx +++ /dev/null @@ -1,1181 +0,0 @@ -% \iffalse -%<*driver> -\ProvidesFile{algxpar.dtx} -\documentclass{ltxdoc} -\usepackage[english]{babel} - -\usepackage{xcolor} -\usepackage{fancyvrb} -\usepackage{tcolorbox} -\usepackage[utf8]{inputenc} -\usepackage[T1]{fontenc} -\tcbuselibrary{listings, skins, xparse, breakable} -\newenvironment{commands}{ - \VerbatimEnvironment - \begin{tcolorbox}[ - bottomrule = 0pt, - toprule = 0pt, - leftrule = 0pt, - rightrule = 0pt, - titlerule = 0pt, - colframe = white, - colback = white, - sharp corners, - pad at break*=1pc, - enhanced jigsaw, - overlay first and middle={ - \coordinate (A1) at ($(interior.south east) + (-10pt,5pt)$); - \coordinate (C1) at ($(interior.south east) + (-6pt,7.5pt)$); - \draw[fill=tcbcolframe] (A1) -- +(0,5pt) -- (C1) -- cycle; - }, - ]% - \begin{Verbatim}[gobble = 2, tabsize=4, commandchars = &\[\]]% -}{% - \end{Verbatim}% - \end{tcolorbox}% -} -\newtcolorbox{codigo}[1]{% - bottomrule = 0pt, - toprule = 0pt, - leftrule = 0pt, - rightrule = 0pt, - titlerule = 0pt, - sharp corners, - sidebyside, - sidebyside align = top, - #1 -} -\newcommand{\afile}[1]{\mbox{\texttt{#1}}} -\newcommand{\package}[1]{\mbox{\textsf{#1}}} -\newcommand{\inlinecommand}[1]{\mbox{\texttt{#1}}} - -\usetikzlibrary{calc} -\newenvironment{example}{ - \begingroup\tcbverbatimwrite{\jobname_code.tmp} -}{ - \endtcbverbatimwrite\endgroup% - \begin{tcolorbox}[ - bottomrule = 0pt, - toprule = 0pt, - leftrule = 0pt, - rightrule = 0pt, - titlerule = 0pt, - colframe = white, - segmentation style = {black!30, solid}, - colback = white, - sharp corners, - pad at break*=1pc, - enhanced jigsaw, - breakable, - overlay first and middle={ - \coordinate (A1) at ($(interior.south east) + (-10pt,5pt)$); - \coordinate (C1) at ($(interior.south east) + (-6pt,7.5pt)$); - \draw[fill=tcbcolframe] (A1) -- +(0,5pt) -- (C1) -- cycle; - }, - ]% - \small\VerbatimInput[tabsize=4, gobble = 2]{\jobname_code.tmp} - \tcblower -% \begin{minipage}{0.85\linewidth} - \begin{algorithmic} - \input{\jobname_code.tmp} - \end{algorithmic} -% \end{minipage} - \end{tcolorbox} -} - -\usepackage[backref=page]{hyperref} -\hypersetup{ - colorlinks, - urlcolor = blue!20!black, - linkcolor = blue!10!black, - citecolor = black!80, -} - -\usepackage{algxpar} - -\usepackage{imakeidx} -\makeindex - -\EnableCrossrefs -% \CodelineIndex -% \PageIndex -\RecordChanges -\begin{document} - \sloppy - \DocInput{algxpar.dtx} -\end{document} -%</driver> -% \fi -% -% \let\DM\DescribeMacro -% \makeatletter -% \def\cstostr#1{% -% \expandafter\@gobble\detokenize\expandafter{\string#1}} -% \makeatother -% \renewcommand{\DescribeMacro}[1]{% -% \DM{#1}% -% } -% -% \CheckSum{0} -% -% \CharacterTable -% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z -% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z -% Digits \0\1\2\3\4\5\6\7\8\9 -% Exclamation \! Double quote \" Hash (number) \# -% Dollar \$ Percent \% Ampersand \& -% Acute accent \' Left paren \( Right paren \) -% Asterisk \* Plus \+ Comma \, -% Minus \- Point \. Solidus \/ -% Colon \: Semicolon \; Less than \< -% Equals \= Greater than \> Question mark \? -% Commercial at \@ Left bracket \[ Backslash \\ -% Right bracket \] Circumflex \^ Underscore \_ -% Grave accent \` Left brace \{ Vertical bar \| -% Right brace \} Tilde \~} -% -% -% \changes{v0.9}{2019/10/24}{Initial version} -% -% \GetFileInfo{algxpar.sty} -% -% -% \DoNotIndex{\newcommand,\newenvironment,\renewcommand,\renewenvironment} -% \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ } -% \DoNotIndex{\@ne} -% \DoNotIndex{\advance,\begingroup,\catcode,\closein} -% \DoNotIndex{\closeout,\day,\def,\edef,\else,\empty,\endgroup} -% \DoNotIndex{\def,\dimexpr,\parbox,\relax,\else,\fi,\hfill} -% \DoNotIndex{\setlength,\settowidth,\strut,\ifdim,\ifx} -% \DoNotIndex{\linewidth,\temp,\theALG@nested} -% \DoNotIndex{\\} -% \DoNotIndex{\RaggedRight} -% -% -% \title{The \package{algxpar} package\thanks{This document -% corresponds to \package{algxpar}~\fileversion, dated \filedate.}} -% \author{Jander Moreira\\\texttt{moreira.jander@gmail.com}} -% -% \maketitle -% % -% \begin{abstract} -% The \textsf{algxpar} packages is an extension of the \textsf{algorithmicx} package to handle multiline text with the proper indentation. -% \end{abstract} -% -% \tableofcontents -% -% \PrintChanges -% -% \section{Introduction} -% I teach algorithms and programming and adopted the \package{algorithmicx} package (\package{algpseudocode}) to typeset my code, as it provides a clean, easy to read pseudolanguage algorithms with a minimum effort to write. -% -% As part of the teaching process, I use very verbose commands in my algorithms before the students start to use more sintetic text. For example, I use ``\textit{Iniciate a counter $c$ with the value $0$}'', what will become ``${c \gets 0}$'' later. This leads to sentences that often span the text for multiple lines, specially in two-column documents with nested structures. -% -% Unfortunatelly, \package{algorithmicx} has no support for multiline statements natively, but it can adapted to use |\parbox|es to achive this goal. -% -% This package, therefore, extends macros to handle multiple lines in a seamlessly way. Some new commands and features are also added. -% -% \section{Instalation} -% The package \package{algxpar} is provided by the files \afile{algxpar.ins} and \afile{algxpar.dtx}. -% -% If the \afile{.sty} file is not available, it can be generated by running the following at a command line prompt. -% -% \begin{commands} -% latex algxpar.ins -% \end{commands} -% -% Then the generated \afile{algxpar.sty} must be copied to a directory searched by \LaTeX. Package dependencies can be checked in section~\ref{sec:implementation}. -% -% -% \section{Usage} -% The package must be loaded using -% \begin{commands} -% \usepackage&oarg[options]{algxpar} -% \end{commands} -% -% The only option to the package is |brazilian|, which sets the pseudocode ``reserved words'' to Brazilian Portuguese, so |\While| is rendered \textbf{enquanto} instead of \textbf{while}, for example. No other language is supported so far, but a translation can be easily achieved (see section~\ref{sec:customization}). -% -% -% \section{Writing pseudocode} -% The algorithms must be written using the |algorithmic| environment and use basically the same set of macros defined by \package{algpseudcode}. -% -% \begin{commands} -% \begin{algorithmic} -% &ensuremath[&langle]&textrm[&textit[contents]]&ensuremath[&rangle] -% \end{algorithmic} -% \end{commands} -% -% \subsubsection*{Example} -% Consider the following code. -% -% \begin{commands} -% \begin{algorithmic} -% \Function{Max}{$a, b$} -% \If{$a > b$} -% \Statep{\Return $a$} -% \Else -% \Statep{\Return $b$} -% \EndIf -% \EndFunction -% \end{algorithmic} -% \end{commands} -% -% The corresponding typeset is shown below. -% -% \begin{algorithmic} -% \Function{Max}{$a, b$} -% \If{$a > b$} -% \Statep{\Return $a$} -% \Else -% \Statep{\Return $b$} -% \EndIf -% \EndFunction -% \end{algorithmic} -% -% -% \subsection{Header} -% A header for the algorithm is proposed so the algorithm can provide a description, its inputs and outputs, as well as the preconditions and post-conditions Therefore, new macros are defined. -% -% \DescribeMacro{\Description} -% A description can be provided for the sake of code documentation. The macro |\Description| is used to provide such a text. -% \DescribeMacro{\Input} -% \DescribeMacro{\Output} -% The input requirements for the algorithm uses the clause |\Input| and the produced by the code should be expressed with |\Output|. -% \DescribeMacro{\Require} -% \DescribeMacro{\Ensure} -% Also, the possibility to use |\Require| and |\Ensure| remains. -% -% \subsubsection*{Examples} -% -% \begin{example} -% \Description Evaluates and prints the factorial of $n$ -% \Input A non-negative integer number $n$ -% \Output The value of the factorial $n$\end{example} -% -% \begin{example} -% \Require $n \in \{1, 2, \ldots, 10\}$ -% \Ensure $k = \max(1, 2, \ldots, 10)$\end{example} -% -% -% \subsection{Constants and identifiers} -% \DescribeMacro{\True} -% \DescribeMacro{\False} -% \DescribeMacro{\Nil} -% Some additional macros were added: |\True|, |\False|, and |\Nil|, producing \True, \False, and \Nil, respectively. -% -% \DescribeMacro{\Id} -% The macro |\Id|\marg{id} was included to support long variable names, such as \Id{maxval} or \Id{count}, for example. This macro handles better ligatures and accented characters than the regular math mode. |$offered$| results in $offered$ and |\Id{offered}| produces \Id{offered}. With accented characters, |$magnético$| and |\Id{magnético}| result in $magnético$ and \Id{magnético}, respectively. -% -% \DescribeMacro{\TextString} -% For literal constants, usually represented quoted in programs and algorithms, the macro |\TextString|\marg{text} is provided, so |\TextString{Error}| produces \TextString{Error}. -% -% \DescribeMacro{\VisibleSpace} -% An additional macro called |\VisibleSpace| is also provided to produce \VisibleSpace. Sometimes the number of spaces is relevant in text strings, so one can write |\TextString{a\VisibleSpace\VisibleSpace\VisibleSpace b}| to get \TextString{a\VisibleSpace\VisibleSpace\VisibleSpace b}. -% -% The macros |\Id| and |\TextString| work in text and math modes. -% -% \subsection{Assignment, reading and writing} -% \DescribeMacro{\gets} -% The default symbol for assigning values to variables is $\gets$, provided by |\gets|. This is a clearer option, once the equal sign is left just for comparisons. -% -% \DescribeMacro{\Read} -% \DescribeMacro{\Write} -% Although not common in algorithms published in scientific journals, explicit reading and writing is necessary for basic algorithms. Therefore |\Read| and |\Write| fulfills this need. -% -% \begin{example} -% \Statep{\Read\ $a, b$} -% \Statep{$s \gets a + b$} -% \Statep{\Write\ $s$}\end{example} -% -% \DescribeMacro{\Set} -% \DescribeMacro{\Setl} -% Besides |\gets|, the macros |\Set| and |\Setl| can be used for assignments. -% -% |\Set|\marg{id}\marg{value} is a shortcut to |\Id{id} \gets value|. The ``long'' version for the assigment is |\Setl|\marg{id}\marg{value}, to get the verbose ``\algorithmicset\ \Id{id} \algorithmicsetto\ \Id{value}''. -% -% \subsection{Comments} -% Comments use the symbol \makeatletter\axp@commentleftsymbol\makeatother\ preceding the commented text and stay close to the left margin. Comment macros are intended to be used with |\State| or |\Statex|, when no multiline handling is done. Comments with multiline control are considered starting at section~\ref{sec:statements}. -% -% \DescribeMacro{\Comment} -% The macro |\Comment|\marg{text} puts \meta{text} at the end of the line. -% -% \DescribeMacro{\Commentl} -% A variant, |\Commentl|\marg{text}, places the commented text without moving it to the left margin. It is a ``local'' comment. -% -% \DescribeMacro{\CommentIn} -% A third option is |\CommentIn|\marg{text}, that places the comment locally, but finishes it with \makeatletter\axp@commentrightsymbol\makeatother. Yes, that is really ugly. -% -% \begin{example} -% \State\Commentl{Simple counter} -% \State $c \gets 1$\Comment{initialize conter} -% \State $n \gets \Call{FirstInstance}{}$ -% \While{$n < 0$} -% \State $c \gets c + 1$\Comment{counts one more} -% \State $n \gets \mbox{\CommentIn{all new} } \Call{NewInstance}{}$ -% \EndWhile -% \end{example} -% -% \subsection{Statements}\label{sec:statements}% -% \DescribeMacro{\Statep} -% \DescribeMacro{\State} -% \DescribeMacro{\Statex} -% The statements should use |\Statep|\marg{text}, which defines a hang indent for continued lines. The \package{algorithmicx}'s |\State| and |\Statex| can be used as well. -% -% In opposition to |\State| and |\Statex|, which uses justified text, |\Statep| aligns only to the left, what is aesthetically better than justification in my opinion. -% -% Since |\Statep| uses a |\parbox| to span the text over multiple lines, no room is left for a comment. When needed a comment can be added through the optional argument: |\Statep|\oarg{comment}\marg{text}. -% -% \subsubsection*{Example} -% \begin{example} -% \Statep{Calculate the value of $x$ using $k$ and $m$, -% considering the stochastic distribution} -% \Statep[$k \neq 0$, $m > k$]{Calculate the value of $x$ -% using $k$ and $m$, considering the stochastic distribution}\end{example} -% -% \subsection{Conditionals} -% The traditional \textbf{if}-\textbf{then}-\textbf{else} structure is suported, handling nested commands as well. -% An \textbf{else~if} construction avoids nesting \textbf{if}s and getting too much indentation. The macros are: |\If|, |\Else|, and |\ElsIf|. -% -% \DescribeMacro{\If} -% \DescribeMacro{\Else} -% |\If|\oarg{comment}\marg{condition} is used for conditional execution and is ended with a |\EndIf|. The optional \meta{comment} is typeset to the left and the \meta{condition} is put in a |\parbox|. Regular |\Comment| and |\Commentl| can be used after |\Else|. -% -% \DescribeMacro{\ElsIf} -% The \textbf{else~if} clause is specified by |\ElsIf|\oarg{comment}\marg{condition}. -% -% \DescribeMacro{\Switch} -% \DescribeMacro{\EndSwitch} -% \DescribeMacro{\Case} -% \DescribeMacro{\EndCase} -% \DescribeMacro{\Otherwise} -% \DescribeMacro{\EndOtherwise} -% Flow control using a selection structure are provided by the macro |\Switch|\oarg{comment}\marg{selector}, ended with |\EndSwitch|. Each matching clause uses |\Case|\oarg{comment}\marg{value} and |\EndCase|. The default uses |\Otherwise| and |\EndOtherwise|. -% -% To specify ranges, the macro |\Range|\oarg{step}\marg{start}\marg{end} can be used. For example, |\Range{1}{10}| outputs \Range{1}{10} and |\Range[2]{0}{10}| prints \Range[2]{0}{10}. -% -% \subsubsection*{Examples} -% -% \begin{example} -% \If{$a < 0$} -% \Statep{$a \gets 0$} -% \EndIf\end{example} -% -% \begin{example} -% \If[closing doors]{the building is empty and the -% security system is active} -% \Statep{$\Id{status} \gets \TextString{ok}$} -% \Else -% \Statep{$\Id{status} \gets \TextString{not ok}$} -% \EndIf\end{example} -% -% \begin{example} -% \If[desired status]{$n \geq 0.8$} -% \Statep{$\Id{status} \gets \TextString{excelent}$} -% \ElsIf{$n \geq 0.7$} -% \Statep{$\Id{status} \gets \TextString{great}$} -% \ElsIf{$n \geq 0.5$} -% \Statep{$\Id{status} \gets \TextString{good}$} -% \ElsIf{$n \geq 0.2$} -% \Statep{$\Id{status} \gets \TextString{not so good}$} -% \Else\Comment{minimum not achieved} -% \Statep{$\Id{status} \gets \TextString{call for help}$} -% \EndIf\end{example} -% -% \begin{example} -% \Switch[$1 \leq \Id{month} \leq 12$]{\Id{month}} -% \Case{2} -% \If{\Call{IsLeapYear}{\Id{year}}} -% \Statep{$n_{days} \gets 29$} -% \Else -% \Statep{$n_{days} \gets 28$} -% \EndIf -% \EndCase -% \Case{4, 6, 9, 11} -% \Statep{$n_{days} \gets 30$} -% \EndCase -% \Otherwise\Comment{1, 3, 5, 7, 8, 10, 12} -% \Statep{$n_{days} \gets 31$} -% \EndOtherwise -% \EndSwitch\end{example} -% -% \subsection{Loops} -% Loops uses \textbf{while}, \textbf{repeat~until}, and \textbf{for} flow control. -% -% \DescribeMacro{\While} -% \DescribeMacro{\EndWhile} -% Loops with condition on top uses |\While|\oarg{comment}\marg{condition} and are ended with |\EndWhile|. -% -% \DescribeMacro{\Repeat} -% \DescribeMacro{\Until} -% When loops have their termination condition tested at the bottom, the macros |\Repeat| and |\Until|\oarg{comment}\marg{condition} are used. -% -% \DescribeMacro{\For} -% \DescribeMacro{\ForAll} -% \DescribeMacro{\ForEach} -% The \textbf{for} loop starts with |\For|\oarg{comment}\marg{condition} and ends with |\EndFor|. To make things more versatile, |\For| can be replaced by |\ForAll| or |\ForEach|. -% -% \DescribeMacro{\To} -% \DescribeMacro{\DownTo} -% \DescribeMacro{\Step} -% Some macros for supporting loops are also provided: |\To|, |\DownTo|, and |\Step|, which defaults to \To, \DownTo, and \Step, repectively. -% -% \subsection*{Examples} -% \begin{example} -% \While{there is data in the input stream and no -% termination signal was received} -% \Statep{Get element $e$ from the input stream} -% \Statep{\Call{Process}{$e$}} -% \EndWhile\end{example} -% -% \begin{example} -% \Statep[$n_1, n_2 > 0$]{Let $n_1$ and $n_2$ -% be the two integers in order to find the greatest -% number that divides both} -% \Repeat -% \Statep[$n_1 \bmod n_2$]{Set \Id{rest} as the -% rest of the integer -% division of $n_1$ by $n_2$} -% \Statep{Redefine $n_1$ with the value of $n_2$} -% \Statep{Redefine $n_2$ with the value of \Id{rest}} -% \Until[terminates]{$\Id{rest} = 0$} -% \Statep[greatest common divisor]{Set $m$ to the value of $n_1$}\end{example} -% -% \begin{example} -% \For{$i \gets n-1$ \DownTo\ $0$} -% \Statep{$s \gets s + i$} -% \EndFor\end{example} -% -% \begin{example} -% \ForEach[main transactions]{transaction $t$ in the flow -% of transactions for month $m$} -% \Statep{\Call{ProcessTransaction}{$t$}} -% \EndFor\end{example} -% -% \begin{example} -% \ForAll{$e$ in set $M$} -% \Statep{\Call{ProcessElement}{$e$}} -% \EndFor\end{example} -% -% \subsection{Procedures and functions} -% \DescribeMacro{\Procedure} -% \DescribeMacro{\EndProcedure} -% \DescribeMacro{\Function} -% \DescribeMacro{\EndFunction} -% \DescribeMacro{\Return} -% Procedure and functions are supported with |\Procedure|\marg{name}\marg{arguments} and |\EndProcedure| and |\Function|\marg{name}\marg{arguments} and |\EndFunction|. The return value for functions use |\Return|. -% -% \subsection*{Examples} -% \begin{example} -% \Procedure{PrintError}{$code$} -% \Switch{$code$} -% \Case{1} -% \Statep{\Write\ \TextString{Not found}} -% \EndCase -% \Case{2} -% \Statep{\Write\ \TextString{Access denied}} -% \EndCase -% \Case{3} -% \Statep{\Write\ \TextString{Blocked}} -% \EndCase -% \Otherwise -% \Statep{\Write\ \TextString{Unknown}} -% \EndOtherwise -% \EndSwitch -% \EndProcedure\end{example} -% -% \begin{example} -% \Function{CelsiusToFahrenheit}{$t$} -% \Statep{\Return $\dfrac{9}{5}t + 32$} -% \EndFunction\end{example} -% -% \begin{example} -% \Function[many parameters]{MyFunction} -% {$a$, $b$, $c$, $d$, $e$, $f$, $g$, $h$, $i$, $j$, $k$, $l$} -% \Statep{\Return $\dfrac{a+b+c+d}{f+g+hi^{j}}kl$} -% \EndFunction\end{example} -% % -% \section{Extras} -% Sometimes just letting the |\parbox| handle the line breaks is not enough. -% \DescribeMacro{\NewLine} -% The macro |\NewLine| can be used to manually break lines. -% -% \DescribeEnv{DefineCode} -% \DescribeMacro{\UseCode} -% \DescribeMacro{\ShowCode} -% It is possible to define pieces of code for later use. Using the environment |DefineCode| with a \meta{name}, a part of the pseudocode can be specified and used with |\UseCode|\marg{name}. The \meta{name} provided should be unique; when repeated the code is overwritten. The macro |\ShowCode|\oarg{options}\marg{name} displays the saved code \textit{verbatim}. Any option for |\VerbatimInput| from \package{fancyvrb} can be specified in \meta{options}. All chuncks of code are written to temporary files. -% -% -% \subsection*{Examples} -% -% \begin{example} -% \If{$h > 0$ and\NewLine -% ($n_1 \neq 0$ or $n_2 < n_1$) and \NewLine -% $p \neq \Nil$} -% \Statep{\Call{DoSomething}{}} -% \Else -% \Statep{\Call{DoSomethingElse}{}} -% \EndIf -% \end{example} -% -% \begin{DefineCode}{half_in_out} -% \Input A number $n$ -% \Output Half of $n$ (i.e., $n/2$) -% \end{DefineCode} -% \begin{DefineCode}{half_code} -% \Statep[in]{Get $n$} -% \Statep[out]{Print $n/2$} -% \end{DefineCode} -% -% \begin{minipage}{\linewidth} -% \small -% \begin{Verbatim}[gobble = 2, tabsize = 4] -% \begin{DefineCode}{half_in_out} -% \Input A number $n$ -% \Output Half of $n$ (i.e., $n/2$) -% \end{DefineCode} -% \begin{DefineCode}{half_code} -% \Statep[in]{Get $n$} -% \Statep[out]{Print $n/2$} -% \end{DefineCode} -% \end{Verbatim} -% \end{minipage} -% -% \vspace{\baselineskip}Inside |algorithmic| one can use the following definitions. -% \begin{example} -% \UseCode{half_in_out} -% \Statep{\Commentl{Code}} -% \UseCode{half_code} -% \end{example} -% -% The source is shown by |\ShowCode{half_code}|. -% \ShowCode[gobble = 2]{half_code} -% -% \StopEventually{} -% -% -% \section{Implementation}\label{sec:implementation}% -% This package is \package{algxpar}~\fileversion~--~\LaTeXe. -% \vspace{\baselineskip} -% \begin{macrocode} -\NeedsTeXFormat{LaTeX2e}[2005/12/01] -\ProvidesPackage{algxpar} - [2020/05/30 v0.91 Algorithms with multiline/paragraph support] -% \end{macrocode} - -% \iffalse -%% Package options -% \fi -% \begin{macrocode} -\newif\ifaxp@brazilian\axp@brazilianfalse -\DeclareOption{brazilian}{\axp@braziliantrue} -\DeclareOption*{\PackageWarning{algxpar}{Unknown ‘\CurrentOption’}} -\ProcessOptions\relax -% \end{macrocode} - -% \iffalse -%% Required packages -% \fi -% \package{ragged2e}: for |\RaggedRight|\par -% \package{listings}: to get accented characters in verbatim mode (pt\_BR)\par -% \package{amsmath}, amssymb: for |\triangleright| and |\triangleleft|\par -% \package{xcolor}: gray color for |\VisibleSpace|\par -% \package{tcolorbox}: verbatim save to file\par -% \package{fancyvrb}: verbatim read from file with tabs\par -% \begin{macrocode} -\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 -% \end{macrocode} - -% \iffalse -%% Constants and identifiers -% \fi -% \begin{macro}{\True} -% \begin{macro}{\False} -% \begin{macro}{\Nil} -% \begin{macro}{\Id} -% \changes{v0.91}{2020/04/30}{Macro now can be used as super-/subscripts in math formulas, while still preventing hyphenaton in text mode.} -% \begin{macro}{\TextString} -% \begin{macro}{\VisibleSpace} -% \begin{macrocode} -\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}} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% Headings -% \fi -% \begin{macro}{\Description} -% \begin{macro}{\Input} -% \begin{macro}{\Output} -% \begin{macro}{\Ensure} -% \begin{macro}{\Require} -% \begin{macrocode} -\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:]} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% Read, write -% \fi -% \begin{macro}{\Read} -% \begin{macro}{\Write} -% \begin{macro}{\Set} -% \changes{v0.91}{2020/06/14}{New macro for assignments, using $\gets$} -% \begin{macro}{\Setl} -% \changes{v0.91}{2020/06/14}{New macro for assignments (verbose)} -% \begin{macrocode} -\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} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% Comments -% \fi -% \begin{macro}{\Comment} -% \begin{macro}{\Commentl} -% \begin{macro}{\CommentIn} -% \begin{macrocode} -\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 -} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% - -% \iffalse -%% Statep -% \fi -% \begin{macro}{\Statep} -% \begin{macrocode} -\newlength{\axp@stateindent} -\setlength{\axp@stateindent}{\dimexpr\algorithmicindent/2\relax} -\algnewcommand{\Statep}[2][]{\State\algparbox[#1]{#2}{\axp@stateindent}} -% \end{macrocode} -%\end{macro} - -% \iffalse -%% While -% \fi -% \begin{macro}{\While} -% \begin{macro}{\EndWhile} -% \begin{macrocode} -\newlength{\axp@whilewidth} -\algblockdefx{While}{EndWhile}% - [2][]{% - \settowidth{\axp@whilewidth}{\algorithmicwhile\ }% - \algparbox[x#1]{\algorithmicwhile\ #2~\algorithmicdo}{\axp@whilewidth}% - }% - {\algorithmicend\ \algorithmicwhile} -% \end{macrocode} -% \end{macro} -% \end{macro} - -% \iffalse -%% Repeat -% \fi -% \begin{macro}{\Repeat} -% \begin{macro}{\Until} -% \begin{macrocode} -\newlength{\axp@untilwidth} -\algblockdefx{Repeat}{Until}% - {\algorithmicrepeat}% - [2][]{% - \settowidth{\axp@untilwidth}{\algorithmicuntil\ }% - \axp@algparbox{#1}{\algorithmicuntil\ #2}{\axp@untilwidth}{0}% - } -% \end{macrocode} -% \end{macro} -% \end{macro} - -% \iffalse -%% If, else if, else -% \fi -% \begin{macro}{\If} -% \begin{macro}{\Else} -% \begin{macro}{\ElsIf} -% \begin{macro}{\EndIf} -% \begin{macrocode} -\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}} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% Switch, case, otherwise -% \fi -% \begin{macro}{\Switch} -% \begin{macro}{\EndSwitch} -% \begin{macro}{\Case} -% \begin{macro}{\EndCase} -% \begin{macro}{\Otherwise} -% \begin{macro}{\EndOtherwise} -% \begin{macro}{\Range} -% \begin{macrocode} -\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% - }% -} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% For, for each, for all -% \fi -% \begin{macro}{\For} -% \begin{macro}{\ForEch} -% \begin{macro}{\ForAll} -% \begin{macro}{\EndFor} -% \begin{macro}{\To} -% \begin{macro}{\DownTo} -% \begin{macro}{\Step} -% \begin{macrocode} -\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} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% Procedure, function, calls -% \fi -% \begin{macro}{\Procedure} -% \begin{macro}{\EndProcedure} -% \begin{macro}{\Function} -% \begin{macro}{\EndFunction} -% \begin{macro}{\Call} -% \begin{macrocode} -\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% -} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} -% \end{macro} - -% \iffalse -%% NewLine -% \fi -% \begin{macro}{\NewLine} -% \begin{macrocode} -\newcommand{\NewLine}{\\} -% \end{macrocode} -% \end{macro} - -% \iffalse -%% DefineCode, UseCode, \ShowCode -% \fi -% \begin{environment}{DefineCode} -% \begin{macro}{\UseCode} -% \begin{macro}{\ShowCode} -% \begin{macrocode} -\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}}} -% \end{macrocode} -% \end{macro} -% \end{macro} -% \end{environment} - -% \changes{v0.91}{2020/04/30}{Small fix in the position of the triangle in line numbers.} -% \begin{macro}{\alglinenumber} -% \begin{macrocode} -\algrenewcommand{\alglinenumber}[1]% - {\hspace{-1.5em}\color{black!35}{\scriptsize#1}\raisebox{0.2ex}{\tiny$\blacktriangleright$}} -% \end{macrocode} -% \end{macro} - -% \iffalse -%% axp@algparbox -% \fi -% \begin{macro}{\axp@algparbox} -% \begin{macrocode} -\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}{\\}% -} -% \end{macrocode} -% \end{macro} -% -% \begin{macrocode} -\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 -} -% \end{macrocode} - -% -% \section{Customization}\label{sec:customization}% -% By default, the longest width for a comment at the right margin is -%|0.3\linewidth|. This can be changed using something like the code below. -% -% \begin{commands} -% \makeatletter -% \setlength{\axp@largestcommentwidth}{&meta[new length]} -% \makeatother -% \end{commands} -% -% The assignment sign can be changed from $\gets$ to anything else, as well as the symbols used in comments. -% -% \begin{commands} -% \renewcommand{\gets}{\mathop{::=}} -% \renewcommand{\axp@commentleftsymbol}{\texttt{//}} -% \renewcommand{\axp@commentrightsymbol}{\texttt{*/}} -% \end{commands} -% -% To handle languages, the macro |\algxparset| should be used. -% -% \begin{macrocode} -\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 -% \end{macrocode} -% -% \section{To do\ldots} -% There are lots of improvements to make in the code. I recognize it! -% -% \section*{Appendix} -% \appendix -%\section{An example} -% \newcommand*{\Leaf}{\mbox{\textsc{Leaf}}} -% \newcommand*{\Internal}{\mbox{\textsc{Internal}}} -% \newcommand*{\InvalidItem}{\mbox{\textsc{InvalidItem}}} -% -% \begin{example} -% \Description Inserts a new item in the B-tree structure, -% handling only the root node -% \Input The \Id{item} to be inserted -% \Output Returns \True\ in case of success, \False\ in -% case of failure (i.e., duplicated keys) -% \Function{Insert}{\Id{item}} -% \If{\Id{tree.root address} is \Nil} -% \Statep{\Commentl{Create first node}} -% \Statep[\Nil\ = new node]{$\Id{new root node} -% \gets \Call{GetNode}{\Nil}$} -% \Statep[only item]{Insert \Id{item} in \Id{new -% root node} and set both its left and right -% childs to \Nil; also set \Id{new root -% node.count} to 1} -% \Statep[first node is always a leaf]{Set \Id{new -% root node.type} to \Leaf} -% \Statep[flag that node must be updated in file] -% {Set \Id{new root node.modified} to \True} -% \Statep{\Call{WriteNode}{\Id{new root node}}} -% \Statep{$\Id{tree.root address} \gets -% \Id{new root node.address}$} -% \Statep[update root address in file] -% {\Call{WriteRootAddress}{}} -% \Statep{\Return \True} -% \Else -% \Statep{\Commentl{Insert in existing tree}} -% \Statep[]{$\Id{success}$, -% $\Id{promoted item}$, $\Id{new node address} \gets -% \Call{SearchInsert}{\Id{tree.root address}, -% \Id{item}}$} -% \If[root has splitted]{\Id{success} and -% ${\Id{new node address}\neq\Nil}$} -% \Statep[new root]{$\Id{new root node} \gets -% \Call{GetNode}{\Nil}$} -% \Statep{Insert \Id{promoted item} in \Id{new -% root node} and set \Id{new root node.count} -% to 1} -% \Statep[tree height grows]{Set \Id{item}'s -% left child to \Id{tree.root -% address} and right child to \Id{new -% node address}} -% \Statep[not a leaf]{Set \Id{new root -% node.type} to \Internal} -% \Statep{Set \Id{new root node.modified} -% to \True} -% \Statep{\Call{WriteNode}{\Id{new root -% node}}} -% \Statep{$\Id{tree.root address} \gets -% \Id{new root node.address}$} -% \Statep[update root address in -% file]{\Call{WriteRootAddress}{}} -% \EndIf -% \Statep[insertion status]{\Return \Id{success}} -% \EndIf -% \EndFunction -% \end{example} -% -% \clearpage\PrintIndex -% -% \Finale -\endinput
\ No newline at end of file diff --git a/macros/latex/contrib/algxpar/algxpar.ins b/macros/latex/contrib/algxpar/algxpar.ins deleted file mode 100644 index 7e7a9c0eba..0000000000 --- a/macros/latex/contrib/algxpar/algxpar.ins +++ /dev/null @@ -1,36 +0,0 @@ -% !TeX program = createsty.sh % | txs:///view -%% -%% Copyright (C) 2019 by Jander Moreira -%% -%% This is an extension of the algorithmicx package to handle -%% multiline contents. -%% - -\input docstrip.tex -\keepsilent -\usedir{tex/latex/algxpar} - -\preamble - - -Copyright (C) 2020 by Jander Moreira (moreira.jander@gmail.com) - -\endpreamble - -\generate{ - \nopreamble\nopostamble - \file{algxpar.sty}{ - \from{algxpar.dtx}{algxpar} - } -} - -\obeyspaces -\Msg{**********************************************************} -\Msg{* The algxpar.sty file was generated} -\Msg{*} -\Msg{* To install it, move or copy it to a directory} -\Msg{* searched by LaTeX.} -\Msg{*} -\Msg{**********************************************************} - -\endbatchfile diff --git a/macros/latex/contrib/algxpar/algxpar.pdf b/macros/latex/contrib/algxpar/algxpar.pdf Binary files differdeleted file mode 100644 index 7446f04ac1..0000000000 --- a/macros/latex/contrib/algxpar/algxpar.pdf +++ /dev/null diff --git a/macros/latex/contrib/algxpar/algxpar.sty b/macros/latex/contrib/algxpar/algxpar.sty new file mode 100644 index 0000000000..3da15d9415 --- /dev/null +++ b/macros/latex/contrib/algxpar/algxpar.sty @@ -0,0 +1,834 @@ +%! Package: algxpar +%! Author: Jander Moreira +%! Email = moreira.jander@gmail.com + +\NeedsTeXFormat{LaTeX2e}[2005/12/01] +\ProvidesPackage{algxpar}[2023/06/26 v0.99 Algorithms with multi-line/paragraph support and some enhancements] + +%% Documentation +\NewDocumentCommand{\AlgVersion}{}{0.99} +\NewDocumentCommand{\AlgDate}{}{2023/06/26} + + +%% Required packages +\RequirePackage{algorithmicx} +\RequirePackage{algpseudocode} +\RequirePackage{ragged2e} +\RequirePackage{xcolor} +\RequirePackage{fancyvrb} % to load verbatim preserving tabs +\RequirePackage{pgfopts} +\RequirePackage{amssymb} +\RequirePackage{pgfmath} +\RequirePackage{varwidth} +\RequirePackage{ifthen} +% \RequirePackage{listings} % to load verbatim with tabs +% \RequirePackage{tcolorbox} % to save verbatim + +%%% Internal commands +\ExplSyntaxOn + +% set_keyword:nnn creates or update a keyword in a language +% #1: language (e.g., brazilian) +% #2: keyword (e.g. while) +% #3: value (e.g, enquanto) +\cs_new:Npn \set_keyword:nnn #1#2#3 { + \tl_clear_new:c { g_keyword_#1_#2_tl } + \tl_set:cn { g_keyword_#1_#2_tl } { #3 } +} + +% get_keyword:nn returs a keyword in a language +% #1: language (e.g., brazilian) +% #2: keyword (e.g. while) +% returns: keyword text (e.g. enquanto) +\cs_new:Npn \get_keyword:nn #1#2 { + \cs_if_exist_use:cTF { g_keyword_#1_#2_tl } { } { + \cs_if_exist_use:cTF { g_keyword_english_#2_tl } { } { + \msg_new:nnn { algxpar } { keyword-not-found } { + Keyword~'#2'~not~found. + } + \msg_fatal:nn { algxpar } { keyword-not-found } + } + } +} + +%%% User commands + +% SetKeyword: create or update a keyword in a language +% #1: language (e.g., brazilian) +% #2: keyword (e.g. while) +% #3: value (e.g, enquanto) +\NewDocumentCommand{\SetKeyword}{ O{\axp@Language} m m }{ + \set_keyword:nnn { #1 } { #2 } { #3 } +} + +% Keyword: returns a formatted keyword in the current language +% #1: keyword (e.g. while) +\NewDocumentCommand{\Keyword} { O{\axp@Language} m }{ + \mbox{\textcolor{\axp@KeywordColor}{\axp@KeywordFont\get_keyword:nn{#1}{#2}}} +} + +% Constant: returns a formatted constant in the current language +% #1: keyword (e.g. True) +\NewDocumentCommand{\Constant} { m }{ + \mbox{\textcolor{\axp@ConstantColor}{\axp@ConstantFont\get_keyword:nn{\axp@Language}{#1}}} +} + +% Module: returns a formatted name for a procedure ou function +% #1: name +\NewDocumentCommand{\Module} { m }{ + \mbox{\textcolor{\axp@ModuleColor}{\axp@ModuleFont#1}} +} + +% CommentSymbol: returns the formatted comment symbol +\NewDocumentCommand{\CommentSymbol} { }{ + \mbox{\textcolor{\axp@CommentColor}{\axp@CommentFont\axp@CommentSymbol}} +} + +% CommentSymbol: returns the formatted comment symbol right +\NewDocumentCommand{\CommentSymbolRight} { }{ + \mbox{\textcolor{\axp@CommentColor}{\axp@CommentFont\axp@CommentSymbolRight}} +} + + +% RenewFullyExpandedDocumentCommand: fully expands argument before +% renewing document command +% #1: macro +% #2: value +\NewDocumentCommand{\RenewFullyExpandedDocumentCommand} { m m }{ + \exp_args:NNnV \RenewDocumentCommand #1 { } { #2 } +} + +\ExplSyntaxOff + +\NewDocumentCommand{\axp@DisableEnds}{}{% + \RenewDocumentCommand{\ALG@noend}{}{t}% + \algtext*{EndWhile}% + \algtext*{EndFor}% + \algtext*{EndLoop}% + \algtext*{EndIf}% + \algtext*{EndProcedure}% + \algtext*{EndFunction}% + \algtext*{EndSwitch}% + \algtext*{EndCase}% + \algtext*{EndOtherwise}% +} + +\NewDocumentCommand{\axp@EnableEnds}{}{% + \RenewDocumentCommand{\ALG@noend}{}{f}% + \algrenewtext{EndWhile}{\Keyword{endwhile}}% + \algrenewtext{EndFor}{\Keyword{endfor}}% + \algrenewtext{EndLoop}{\Keyword{endloop}}% + \algrenewtext{EndIf}{\Keyword{endif}}% + \algrenewtext{EndProcedure}{\Keyword{endprocedure}}% + \algrenewtext{EndFunction}{\Keyword{endfunction}}% + \algrenewtext{EndSwitch}{\Keyword{endswitch}}% + \algrenewtext{EndCase}{\Keyword{endcase}}% + \algrenewtext{EndOtherwise}{\Keyword{endotherwise}}% +} +\axp@EnableEnds % update end-line texts + +%% Settings +\newif\ifaxp@NoEnd\axp@NoEndfalse % whether line with 'end' is used or not +\newif\ifaxp@IsStatement\axp@IsStatementfalse % set in \Statep +\newif\ifaxp@ShowBoxes\axp@ShowBoxesfalse % show parboxes for debugging +\newif\ifaxp@LineBalanceAuto % true for 'auto'; false for 'fixed' + +%% General options +%! formatter = off +\pgfkeys{ + /algxpar/options/.cd, + % language + language/.store in = \axp@Language, + % styles + text font/.store in = \axp@TextFont, + text color/.store in = \axp@TextColor, + text style/.store in = \axp@TextStyle, + comment font/.store in = \axp@CommentFont, + comment color/.store in = \axp@CommentColor, + comment style/.store in = \axp@CommentStyle, + keyword font/.store in = \axp@KeywordFont, + keyword color/.store in = \axp@KeywordColor, + constant font/.store in = \axp@ConstantFont, + constant color/.store in = \axp@ConstantColor, + module font/.store in = \axp@ModuleFont, + module color/.store in = \axp@ModuleColor, + % + comment symbol/.store in = \axp@CommentSymbol, + comment symbol right/.store in = \axp@CommentSymbolRight, + comment separator width/.store in = \axp@CommentSeparatorWidth, + % + noend/.code = {\axp@DisableEnds}, + no end/.code = {\axp@DisableEnds}, + end/.code = {\axp@EnableEnds}, + % + % keywords/.code = {\pgfkeysalso{/algxpar/languages/.cd, \trim@spaces{#1}}}, + keywords/.code = {\axp@LanguageSet{#1}}, + % + algorithmic indent/.store in = \algorithmicindent, + statement indent/.store in = \axp@StatementIndent, + parameter indent/.store in = \axp@ParameterIndent, + % + show boxes/.is if = axp@ShowBoxes, + % + comment width/.code = {% + \axp@LineBalanceAutotrue% + \ifthenelse{\equal{auto}{#1}}{% + \axp@LineBalanceAutotrue% + }{% + \axp@LineBalanceAutofalse% + \ifthenelse{\equal{nice}{#1}}{% + \DeclareDocumentCommand{\axp@CommentFixedWidth}{}{0.4\linewidth}% + }{% + \DeclareDocumentCommand{\axp@CommentFixedWidth}{}{#1}% + }% + }% + }, +} +%! formatter = on + +\NewDocumentCommand{\AlgSet}{ > { \TrimSpaces } m }{% + \pgfkeys{% + /algxpar/options/.cd, + /utils/exec = \pgfkeysalsofrom{#1}, + }% +} + + +%% Languages and translations +\pgfkeys{ + /algxpar/languages/.cd, + .unknown/.code = {\SetKeyword{\pgfkeyscurrentname}{#1}} +} + +\NewDocumentCommand{\AlgLanguageSet}{ > { \TrimSpaces } m > { \TrimSpaces } m }{% + \AlgSet{language = #1} + \pgfkeys{ + /algxpar/languages/.cd, + #2 + } +} +\NewDocumentCommand{\axp@LanguageSet}{ > { \TrimSpaces } m }{% + \pgfkeys{ + /algxpar/languages/.cd, + #1 + } +} + +\input{algxpar-english.kw} % always loads default language + + +% general settings defaults +\AlgSet{ + language = english, + text font = , + text color = ., + text style = \RaggedRight, + comment font = \slshape, + comment color = .!70, + comment style = \RaggedRight, + keyword font = \bfseries, + keyword color = ., + constant font = \scshape, + constant color = ., + module font = \scshape, + module color = ., + comment symbol = $\triangleright$, + comment symbol right = $\triangleleft$, + comment separator width = 1em, + algorithmic indent = 1.5em, + statement indent = 1em, + parameter indent = 0pt, + show boxes = false, + comment width = auto, +} + + +%% Package options +\pgfkeys{ + /algxpar/.cd, + language/.store in = \axp@Language, + noend/.is if = axp@NoEnd, + .unknown/.code = { + %! suppress = FileNotFound + \input{algxpar-\pgfkeyscurrentname.kw} + \RenewFullyExpandedDocumentCommand{\axp@Language}{\pgfkeyscurrentname} + } +} +\pgfkeys{ + /algxpar/.cd, + language = english, + noend = false, +} +\ProcessPgfOptions{/algxpar} +\ifaxp@NoEnd\axp@DisableEnds\fi% globally disable 'ends' + + +% axp@SetToWidth: \settowidth allowing multi-line text +% #1: existing length +% #2: text +\NewDocumentCommand{\axp@SetToWidth}{ m m }{% + \settowidth{#1}{% + \begin{varwidth}{500cm}% 5 meters = infinity + #2 + \end{varwidth}% + }% +} + +% axp@DefineCommand: defines command structure +% #1: keyword (e.g, while) +% #2: block text (e.g. a > 0) +% #3: complement keyword (e.g. do) +% #4: comment text +% #5: options +\NewDocumentCommand{\axp@DefineCommand}{ m m m m m }{% + \DeclareDocumentCommand{\axp@CommandKeyword}{}{#1}% + \DeclareDocumentCommand{\axp@CommandText}{}{#2}% + \DeclareDocumentCommand{\axp@CommandComplementKeyword}{}{#3}% + \DeclareDocumentCommand{\axp@CommentText}{}{#4}% + \def\axp@CommandOptions{#5}% def: to be accepeted in \pgfkeysalsofrom +} + + +% axp@MakeSingleToken: texto before and after only if token has some width +% #1: text before toke +% #2: token +% #3: text after token +\newlength{\axp@TokenWidth} +\NewDocumentCommand{\axp@MakeSingleToken}{ m m m }{% + \settowidth{\axp@TokenWidth}{#2}% + \ifdim\axp@TokenWidth>0pt\relax#1\fi% + #2% + \ifdim\axp@TokenWidth>0pt\relax#3\fi% +} + +\newif\ifaxp@NoCommentPresent +\newlength{\axp@TextWidth} +\newlength{\axp@CommentWidth} + + +% axp@CalculateDimensions: evaluate space needed for text and comments +% #1: nesting ajustment (1 when openning a block; 0 when closing) +% #2: text +% #3: comment +% sets: +% \axp@TextWidth +% \axp@CommentWidth, when needed +\newlength{\axp@FullTextWidth} +\newlength{\axp@FullCommentWidth} +\newlength{\axp@CommentSymbolWidth} +\newlength{\axp@CurrentLineWidth} +\newlength{\axp@RemainingWidth} +\NewDocumentCommand{\axp@CalculateDimensions}{ m m m }{% + \axp@SetToWidth{\axp@FullTextWidth}{\axp@TextFont#2}% text + \axp@SetToWidth{\axp@FullCommentWidth}{\axp@CommentFont#3}% comment + %! formatter = off + \ifdim\axp@FullCommentWidth=0pt\relax% + % No comments present + \axp@NoCommentPresenttrue% + \setlength{\axp@TextWidth}{ + \dimexpr \linewidth - \axp@CurrentTokenWidth - (\algorithmicindent)*(\theALG@nested - #1) + }% + \else + % Yep, we have a comment + \axp@NoCommentPresentfalse% + \settowidth{\axp@CommentSymbolWidth}{\hspace{\axp@CommentSeparatorWidth}\CommentSymbol~}% + \setlength{\axp@CurrentLineWidth}{ + \dimexpr \linewidth - \axp@CurrentTokenWidth - (\algorithmicindent)*(\theALG@nested - #1) + }% + \ifaxp@LineBalanceAuto% + % 'auto': decide here how to balance text and comment widths + \setlength{\axp@RemainingWidth}{ + \dimexpr \axp@CurrentLineWidth - \axp@FullTextWidth - \axp@FullCommentWidth - \axp@CommentSymbolWidth + }% + \ifdim\axp@RemainingWidth>0pt\relax% + % Both text and comment fit the line + \axp@TextWidth\axp@FullTextWidth% + \axp@CommentWidth\axp@FullCommentWidth% + \else% + % Text/comment will go multi-line + \pgfmathsetmacro{\axp@CurrentRatio}{ + int(round(\axp@FullTextWidth/(\axp@FullTextWidth + \axp@FullCommentWidth) * 100)) + }% + \ifnum\axp@CurrentRatio<25\relax% never less than 0.25 + \edef\axp@CurrentRatio{25}% + \fi% + \ifnum\axp@CurrentRatio>75\relax% never more than 0.75 + \edef\axp@CurrentRatio{75}% + \fi% + \pgfmathparse{ + \axp@CurrentRatio/100 * \axp@CurrentLineWidth - \axp@CommentSymbolWidth/2 + }% + \setlength{\axp@TextWidth}{\pgfmathresult pt}% + \pgfmathparse{\axp@CurrentLineWidth - \axp@TextWidth - \axp@CommentSymbolWidth}% + \setlength{\axp@CommentWidth}{\pgfmathresult pt}% + % fine tunning the widths + \settowidth{\axp@CommentWidth}{% + \begin{varwidth}[t]{\axp@CommentWidth}% + \axp@CommentStyle% + \textcolor{\axp@CommentColor}{\axp@CommentFont#3}\strut% + \end{varwidth}% + }% + \pgfmathparse{\axp@CurrentLineWidth - \axp@CommentWidth - \axp@CommentSymbolWidth}% + \setlength{\axp@TextWidth}{\pgfmathresult pt}% + \fi% + \else% + % 'fixed': limit comment width + % todo: think about using \axp@CurrentLineWidth instead of \linewidth + \setlength{\axp@CommentWidth}{\axp@CommentFixedWidth}% + \setlength{\axp@TextWidth}{ + \dimexpr \axp@CurrentLineWidth - \axp@CommentWidth - \axp@CommentSymbolWidth + }% + \fi% + \setlength{\axp@RemainingWidth}{ + \dimexpr \linewidth - \axp@CurrentTokenWidth - + (\algorithmicindent)*(\theALG@nested - #1) - + \axp@CommentWidth - \axp@CommentSymbolWidth + }% + \fi% + % + \ifdim\axp@FullTextWidth<\axp@TextWidth\relax% + \axp@TextWidth\axp@FullTextWidth% makes the parbox fit the actual text width + \fi% + %! formatter = on +} + +% axp@MakeCommand: builds the command (see axp@DefineCommand) +% #1 (star): star = end block; no star = begin block +\NewDocumentCommand{\axp@MakeCommand}{ s }{% + \axp@DoCommand{% ajustment for opening/closing blocks + \IfBooleanTF{#1}{0}{1}% + }{% command or module+name + \axp@MakeSingleToken{}{\axp@CommandKeyword}{\ifaxp@IsModule(\else\ \fi}% + }{% text or module arguments + \axp@CommandText\ifaxp@IsModule)\fi% + }{% command complement + \axp@MakeSingleToken{ }{\axp@CommandComplementKeyword}{}% + }{% comment + \axp@CommentText% + }{% options + \axp@CommandOptions% + }% +} + +%! formatter = off +\newsavebox{\axp@SaveBox} +\NewDocumentEnvironment{axp@FBox}{}{% + \begin{lrbox}{\axp@SaveBox}% +}{% + \end{lrbox}% + \ifaxp@ShowBoxes% + \begingroup% + \setlength{\fboxrule}{0.001pt}% + \setlength{\fboxsep}{0pt}% + \color{.!20}\fbox{\color{.}\usebox{\axp@SaveBox}}% + \endgroup% + \else% + \usebox{\axp@SaveBox}% + \fi% +} +%! formatter = on + + +% axp@DoCommand: builds the command (see axp@DefineCommand) +% #1: 1 when openning block; 0 when closing +% #2: command +% #3: text +% #4: command complement +% #5: comment +% #6: options +\newlength{\axp@CurrentTokenWidth} +\NewDocumentCommand{\axp@DoCommand}{ m m m m m m }{% + \AlgSet{#6}% + % Dimensions + \settowidth{\axp@CurrentTokenWidth}{#2}% + \axp@CalculateDimensions{#1}{{\axp@TextStyle\axp@TextFont#3}#4}{#5}% + % + % Making the line: body + #2% + %! formatter = off + \begin{axp@FBox}% + \ifaxp@IsModule\hspace{\axp@ParameterIndent}\fi% + \parbox[t]{\axp@TextWidth}{% + \ifaxp@IsStatement% + \setlength{\hangindent}{\axp@StatementIndent}% + \fi% + \axp@TextStyle% + \textcolor{\axp@TextColor}{\axp@TextFont{#3}}#4\strut% + }% + \end{axp@FBox}% + % Making the line: comment + \ifaxp@NoCommentPresent\else% + \hspace{\axp@CommentSeparatorWidth}\hfill\CommentSymbol~% + \begin{axp@FBox}% + \ifaxp@LineBalanceAuto + \begin{varwidth}[t]{\axp@CommentWidth}% + \axp@CommentStyle% + \textcolor{\axp@CommentColor}{\axp@CommentFont#5}\strut% + \end{varwidth}% + \else + \parbox[t]{\axp@CommentWidth}{% + \axp@CommentStyle% + \textcolor{\axp@CommentColor}{\axp@CommentFont{#5}}\strut% + }% + \fi% + \end{axp@FBox}% + \fi% + %! formatter = on +} + + +%% Keyword quick references +\NewDocumentCommand{\To}{}{\axp@MakeSingleToken{}{\Keyword{to}}{ }} +\NewDocumentCommand{\DownTo}{}{\axp@MakeSingleToken{}{\Keyword{downto}}{ }} +\NewDocumentCommand{\Step}{}{\axp@MakeSingleToken{}{\Keyword{step}}{ }} +\NewDocumentCommand{\Continue}{}{\Keyword{continue}} +\NewDocumentCommand{\Break}{}{\Keyword{break}} +\RenewDocumentCommand{\Return}{}{\axp@MakeSingleToken{}{\Keyword{return}}{ }} + + +\NewDocumentCommand{\True}{}{\Constant{true}} +\NewDocumentCommand{\False}{}{\Constant{false}} +\NewDocumentCommand{\Nil}{}{\Constant{nil}} + +\newcommand{\Id}[1]{\ensuremath{\textit{\rmfamily#1}}} +% \newcommand{\TextString}[1]{\textrm{\normalfont``{\ttfamily\mbox{#1}}''}} +% \algnewcommand{\VisibleSpace}{\textrm{\color{black!70}\textvisiblespace}} + +%% Headings +\NewDocumentCommand{\Description}{}{\item[\Keyword{description}:]} +\NewDocumentCommand{\Input}{}{\item[\Keyword{input}:]} +\NewDocumentCommand{\Output}{}{\item[\Keyword{output}:]} +\RenewDocumentCommand{\Ensure}{}{\item[\Keyword{ensure}:]} +\RenewDocumentCommand{\Require}{}{\item[\Keyword{require}:]} + +% Read, write +\NewDocumentCommand{\Set}{ m m }{% + \PackageWarning{alxpar}{The macro \string\Set\ is no longer supported.}% + \Id{#1} \gets #2% +} +\NewDocumentCommand{\Read}{}{\Keyword{read} } +\NewDocumentCommand{\Write}{}{\Keyword{write} } + +% %% Comments +\algnewcommand{\CommentIn}[1]{% + \begin{axp@FBox} + \textcolor{\axp@CommentColor}{\CommentSymbol~\axp@CommentFont#1~\CommentSymbolRight}% + \end{axp@FBox}% +} +\NewDocumentCommand{\Commentl}{ m D<>{} }{% + \begingroup% + \AlgSet{#2}% + \textcolor{\axp@CommentColor}{\CommentSymbol~\axp@CommentFont#1\strut}% + \endgroup% +} +\RenewDocumentCommand{\Comment}{ m D<>{} }{% + \hfill\Commentl{#1}<#2>% +} + +\newlength{\axp@CurrentIndent} + +\NewDocumentCommand{\axp@BeginGroup}{}{\begingroup} +\NewDocumentCommand{\axp@EndGroup}{}{% + \global\edef\axp@SaveIndent{\the\ALG@tlm}% + \endgroup% + \setlength{\ALG@tlm}{\axp@SaveIndent}% +} + + +%% Statep: statement +% #1: old style comment +% #2: text +% #3: comments +% #4: options +\NewDocumentCommand{\Statep}{ O{} +m O{} D<>{} }{% + \axp@IsStatementtrue% + \State\axp@DoCommand{1}{}{#2}{}{#1#3}{#4}% + \axp@IsStatementfalse% +} +\RenewDocumentCommand{\Statex}{}{% + \item[]\strut% +} + + +%% While +\algblockdefx[While]{While}{EndWhile}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\While}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{while}}{#2}{\Keyword{do}}{#1#3}{#4}% + \ALG@makeentity{While}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndWhile}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endwhile}}{}{}{#1}{#2}% + \ALG@makeentity{EndWhile}% + \axp@EndGroup +} + + +%% Repeat +\algblockdefx[Repeat]{Repeat}{Until}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: comments +% #2: options +\RenewDocumentCommand{\Repeat}{ O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{repeat}}{}{}{#1}{#2}% + \ALG@makeentity{Repeat}% +} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\Until}{ O{} m O{} D<>{} }{% + \axp@DefineCommand{\Keyword{until}}{#2}{}{#1#3}{#4}% + \ALG@makeentity{Until}% + \axp@EndGroup +} + +%% Loop +\algblockdefx[Loop]{Loop}{EndLoop}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: comment +% #2: options +\RenewDocumentCommand{\Loop}{ O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{loop}}{}{}{#1}{#2}% + \ALG@makeentity{Loop}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndLoop}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endloop}}{}{}{#1}{#2}% + \ALG@makeentity{EndLoop}% + \axp@EndGroup +} + + +%% If +\algblockdefx[If]{If}{EndIf}{\axp@MakeCommand}{\axp@MakeCommand*} +% + else if +\algcblockdefx[If]{If}{ElsIf}{EndIf}{\axp@MakeCommand}{\axp@MakeCommand*} +% + else +\algcblockdefx[If]{If}{Else}{EndIf}[1][]{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\If}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{if}}{#2}{\Keyword{then}}{#1#3}{#4}% + \ALG@makeentity{If}% +} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\ElsIf}{ O{} m O{} D<>{} }{% + \axp@DefineCommand{\Keyword{else}~\Keyword{if}}{#2}{\Keyword{then}}{#1#3}{#4}% + \ALG@makeentity{ElsIf}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\Else}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{else}}{}{}{#1}{#2}% + \ALG@makeentity{Else}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndIf}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endif}}{}{}{#1}{#2}% + \ALG@makeentity{EndIf}% + \axp@EndGroup +} + + +%% Switch +\algblockdefx[Switch]{Switch}{EndSwitch}{\axp@MakeCommand}{\axp@MakeCommand*} +% + case +\algblockdefx[Case]{Case}{EndCase}{\axp@MakeCommand}{\axp@MakeCommand*} +% + otherwise +\algblockdefx[Otherwise]{Otherwise}{EndOtherwise}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\Switch}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{switch}}{#2}{}{#1#3}{#4}% + \ALG@makeentity{Switch}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndSwitch}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endswitch}}{}{}{#1}{#2}% + \ALG@makeentity{EndSwitch}% + \axp@EndGroup +} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\Case}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{case}}{#2}{\Keyword{do}}{#1#3}{#4}% + \ALG@makeentity{Case}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndCase}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endcase}}{}{}{#1}{#2}% + \ALG@makeentity{EndCase}% + \axp@EndGroup +} +% #1: comment +% #2: options +\RenewDocumentCommand{\Otherwise}{ O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{otherwise}}{}{}{#1}{#2}% + \ALG@makeentity{Otherwise}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndOtherwise}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endotherwise}}{}{}{#1}{#2}% + \ALG@makeentity{EndOtherwise}% + \axp@EndGroup +} + + +% \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 +\algblockdefx[For]{For}{EndFor}{\axp@MakeCommand}{\axp@MakeCommand*} +\algblockdefx[For]{ForEach}{EndFor}{\axp@MakeCommand}{\axp@MakeCommand*} +\algblockdefx[For]{ForAll}{EndFor}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\For}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{for}}{#2}{\Keyword{do}}{#1#3}{#4}% + \ALG@makeentity{For}% +} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\ForEach}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{foreach}}{#2}{\Keyword{do}}{#1#3}{#4}% + \ALG@makeentity{ForEach}% +} +% #1: old style comment +% #2: text +% #3: comment +% #4: options +\RenewDocumentCommand{\ForAll}{ O{} m O{} D<>{} }{% + \axp@BeginGroup + \axp@DefineCommand{\Keyword{forall}}{#2}{\Keyword{do}}{#1#3}{#4}% + \ALG@makeentity{For}% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndFor}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endfor}}{}{}{#1}{#2}% + \ALG@makeentity{EndFor}% + \axp@EndGroup +} + + +%% Procedure, function, calls +\newif\ifaxp@IsModule +\algblockdefx[Procedure]{Procedure}{EndProcedure}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: old style comment +% #2: name +% #3: arguments +% #4: comment +% #5: options +\RenewDocumentCommand{\Procedure}{ O{} m m O{} D<>{} }{% + \axp@BeginGroup + \axp@IsModuletrue% + \axp@DefineCommand{\Keyword{procedure}~\Module{#2}}{#3}{}{#4}{#5}% + \ALG@makeentity{Procedure}% + \axp@IsModulefalse% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndProcedure}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endprocedure}}{}{}{#1}{#2}% + \ALG@makeentity{EndProcedure}% + \axp@EndGroup +} + +\algblockdefx[Function]{Function}{EndFunction}{\axp@MakeCommand}{\axp@MakeCommand*} +% #1: old style comment +% #2: name +% #3: arguments +% #4: comment +% #5: options +\RenewDocumentCommand{\Function}{ O{} m m O{} D<>{} }{% + \axp@BeginGroup + \axp@IsModuletrue% + \axp@DefineCommand{\Keyword{function}~\Module{#2}}{#3}{}{#4}{#5}% + \ALG@makeentity{Function}% + \axp@IsModulefalse% +} +% #1: comment +% #2: options +\RenewDocumentCommand{\EndFunction}{ O{} D<>{} }{% + \axp@DefineCommand{\Keyword{endfunction}}{}{}{#1}{#2}% + \ALG@makeentity{EndFunction}% + \axp@EndGroup +} + +\RenewDocumentCommand{\Call}{ m m D<>{} }{% + \begingroup% + \AlgSet{#3}% + \Module{#1}\textcolor{\axp@TextColor}{\axp@TextFont(#2)}% + \endgroup% +} + + +%% 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 + +%% Adding options to the 'algorithmic' environment +\NewCommandCopy{\axp@AlgorithmicxAlgorithmic}{\algorithmic} +\NewCommandCopy{\axp@AlgorithmicxEndAlgorithmic}{\endalgorithmic} +\RenewDocumentCommand{\algorithmic}{ O{} D<>{} }{% + \begingroup% + \AlgSet{#2}% + \axp@AlgorithmicxAlgorithmic[#1]% +} +\RenewDocumentCommand{\endalgorithmic}{}{% + \axp@AlgorithmicxEndAlgorithmic% + \endgroup% +} + +\endinput
\ No newline at end of file |