|
22 | 22 | \usepackage[margin=1.0in]{geometry}
|
23 | 23 | \usepackage{microtype}
|
24 | 24 | \usepackage{xcolor}
|
| 25 | +\usepackage{algpseudocodex} |
25 | 26 |
|
26 | 27 | \usepackage[colorlinks,allcolors=blue!70!black]{hyperref}
|
27 | 28 | \usepackage[capitalise,nameinlink,noabbrev,compress]{cleveref}
|
|
37 | 38 | This package allows typesetting pseudocode in \LaTeX. It is based on \texttt{algpseudocode} from the \texttt{algorithmicx} package and uses the same syntax, but adds several new features and improvements. Notable features include customizable indent guide lines and the ability to draw boxes around parts of the code for highlighting differences. This package also has better support for long code lines spanning several lines and improved comments.
|
38 | 39 | \end{abstract}
|
39 | 40 |
|
40 |
| -\section{Introduction} |
41 |
| -Todo |
| 41 | +\section{Basic Usage} |
| 42 | +To use the package, load it in your preamble: |
| 43 | +\begin{verbatim} |
| 44 | +\usepackage{algpseudocodex} |
| 45 | +\end{verbatim} |
42 | 46 |
|
| 47 | +Basic usage is identical to \texttt{algpseudocode} from the \texttt{algorithmicx} package. Pseudocode written for that package should also be compatible with \texttt{algpseudocodex}. |
| 48 | + |
| 49 | +\subsection{Algorithmic Block} |
| 50 | +Pseudocode can be typeset inside a algorithmic blocks: |
| 51 | +\begin{verbatim} |
| 52 | +\begin{algorithmic}[line numbering] |
| 53 | + ... |
| 54 | +\end{algorithmic} |
| 55 | +\end{verbatim} |
| 56 | +The optional argument specifies how lines are numbered. $0$ means no numbering, $n > 0$ means every $n$th line gets a number. The default is $0$, i.e., no line numbers will be typeset if no optional argument is provided. |
| 57 | + |
| 58 | +\subsection{Simple Statements and Commands} |
| 59 | +Statements start with \verb|\State|. The command \verb|\Statex| can be used to start a new line that does not get a new line number. |
| 60 | + |
| 61 | +The commands \verb|\Return| and \verb|\Output| can be used for return values of functions and outputs. They do not start a new line on their own, so they need to be used together with \verb|\State|. |
| 62 | + |
| 63 | +The \verb|\Call| command is used for procedure calls. It takes two arguments: The first one is the name of the procedure and the second one are the arguments. |
| 64 | + |
| 65 | +\subsubsection*{Example} |
| 66 | +\begin{minipage}[t]{0.45\textwidth} |
| 67 | +\begin{verbatim} |
| 68 | +\begin{algorithmic}[1] |
| 69 | + \State first line |
| 70 | + \Statex continuing first line |
| 71 | + \State \Call{Proc}{a1, a2} |
| 72 | + \State \Output Hello World! |
| 73 | +\end{algorithmic} |
| 74 | +\end{verbatim} |
| 75 | +\end{minipage} |
| 76 | +\hfill |
| 77 | +\begin{minipage}[t]{0.45\textwidth} |
| 78 | +\begin{algorithmic}[1] |
| 79 | +\State first line |
| 80 | +\Statex continuing first line |
| 81 | +\State \Call{Proc}{a1, a2} |
| 82 | +\State \Output Hello World! |
| 83 | +\end{algorithmic} |
| 84 | +\end{minipage} |
| 85 | + |
| 86 | + |
| 87 | +\subsection{Blocks} |
| 88 | +Blocks are used for loops, conditional statements, and functions. Blocks can also be nested within other blocks. |
| 89 | + |
| 90 | +\subsubsection{While Loop} |
| 91 | +\begin{minipage}[t]{0.45\textwidth} |
| 92 | +\begin{verbatim} |
| 93 | +\While{condition} |
| 94 | + \State body |
| 95 | +\EndWhile |
| 96 | +\end{verbatim} |
| 97 | +\end{minipage} |
| 98 | +\hfill |
| 99 | +\begin{minipage}[t]{0.45\textwidth} |
| 100 | +\begin{algorithmic} |
| 101 | +\While{condition} |
| 102 | + \State body |
| 103 | +\EndWhile |
| 104 | +\end{algorithmic} |
| 105 | +\end{minipage} |
| 106 | + |
| 107 | + |
| 108 | +\subsubsection{For Loop} |
| 109 | +\begin{minipage}[t]{0.45\textwidth} |
| 110 | +\begin{verbatim} |
| 111 | +\For{$n = 1, \dots, 10$} |
| 112 | + \State body |
| 113 | +\EndFor |
| 114 | +\end{verbatim} |
| 115 | +\end{minipage} |
| 116 | +\hfill |
| 117 | +\begin{minipage}[t]{0.45\textwidth} |
| 118 | +\begin{algorithmic} |
| 119 | +\For{$n = 1, \dots, 10$} |
| 120 | + \State body |
| 121 | +\EndFor |
| 122 | +\end{algorithmic} |
| 123 | +\end{minipage} |
| 124 | + |
| 125 | +\subsubsection{For-All Loop} |
| 126 | +\begin{minipage}[t]{0.45\textwidth} |
| 127 | +\begin{verbatim} |
| 128 | +\ForAll{$n \in \{1, \dots, 10\}$} |
| 129 | + \State body |
| 130 | +\EndFor |
| 131 | +\end{verbatim} |
| 132 | +\end{minipage} |
| 133 | +\hfill |
| 134 | +\begin{minipage}[t]{0.45\textwidth} |
| 135 | +\begin{algorithmic} |
| 136 | +\ForAll{$n \in \{1, \dots, 10\}$} |
| 137 | + \State body |
| 138 | +\EndFor |
| 139 | +\end{algorithmic} |
| 140 | +\end{minipage} |
| 141 | + |
| 142 | +\subsubsection{Loop} |
| 143 | +\begin{minipage}[t]{0.45\textwidth} |
| 144 | +\begin{verbatim} |
| 145 | +\Loop |
| 146 | + \State body |
| 147 | +\EndLoop |
| 148 | +\end{verbatim} |
| 149 | +\end{minipage} |
| 150 | +\hfill |
| 151 | +\begin{minipage}[t]{0.45\textwidth} |
| 152 | +\begin{algorithmic} |
| 153 | +\Loop |
| 154 | + \State body |
| 155 | +\EndLoop |
| 156 | +\end{algorithmic} |
| 157 | +\end{minipage} |
| 158 | + |
| 159 | +\subsubsection{Repeat-Until Loop} |
| 160 | +\begin{minipage}[t]{0.45\textwidth} |
| 161 | +\begin{verbatim} |
| 162 | +\Repeat |
| 163 | + \State body |
| 164 | +\Until{$n > 10$} |
| 165 | +\end{verbatim} |
| 166 | +\end{minipage} |
| 167 | +\hfill |
| 168 | +\begin{minipage}[t]{0.45\textwidth} |
| 169 | +\begin{algorithmic} |
| 170 | +\Repeat |
| 171 | + \State body |
| 172 | +\Until{$n > 10$} |
| 173 | +\end{algorithmic} |
| 174 | +\end{minipage} |
| 175 | + |
| 176 | +\subsubsection{If Statement} |
| 177 | +\begin{minipage}[t]{0.45\textwidth} |
| 178 | +\begin{verbatim} |
| 179 | +\If{condition} |
| 180 | + \State body |
| 181 | +\ElsIf{condition} |
| 182 | + \State body |
| 183 | +\Else |
| 184 | + \State body |
| 185 | +\EndIf |
| 186 | +\end{verbatim} |
| 187 | +\end{minipage} |
| 188 | +\hfill |
| 189 | +\begin{minipage}[t]{0.45\textwidth} |
| 190 | +\begin{algorithmic} |
| 191 | +\If{condition} |
| 192 | + \State body |
| 193 | +\ElsIf{condition} |
| 194 | + \State body |
| 195 | +\Else |
| 196 | + \State body |
| 197 | +\EndIf |
| 198 | +\end{algorithmic} |
| 199 | +\end{minipage} |
| 200 | + |
| 201 | +\bigskip |
| 202 | +The \verb|\ElsIf| and \verb|\Else| parts are optional. |
| 203 | + |
| 204 | + |
| 205 | +\subsubsection{Procedure} |
| 206 | +\begin{minipage}[t]{0.45\textwidth} |
| 207 | +\begin{verbatim} |
| 208 | +\Procedure{name}{parameters} |
| 209 | + \State body |
| 210 | +\EndProcedure |
| 211 | +\end{verbatim} |
| 212 | +\end{minipage} |
| 213 | +\hfill |
| 214 | +\begin{minipage}[t]{0.45\textwidth} |
| 215 | +\begin{algorithmic} |
| 216 | +\Procedure{name}{parameters} |
| 217 | + \State body |
| 218 | +\EndProcedure |
| 219 | +\end{algorithmic} |
| 220 | +\end{minipage} |
| 221 | + |
| 222 | + |
| 223 | +\subsubsection{Function} |
| 224 | +\begin{minipage}[t]{0.45\textwidth} |
| 225 | +\begin{verbatim} |
| 226 | +\Function{name}{parameters} |
| 227 | + \State body |
| 228 | +\EndFunction |
| 229 | +\end{verbatim} |
| 230 | +\end{minipage} |
| 231 | +\hfill |
| 232 | +\begin{minipage}[t]{0.45\textwidth} |
| 233 | +\begin{algorithmic} |
| 234 | +\Function{name}{parameters} |
| 235 | + \State body |
| 236 | +\EndFunction |
| 237 | +\end{algorithmic} |
| 238 | +\end{minipage} |
| 239 | + |
| 240 | + |
| 241 | +\subsection{Require and Ensure} |
| 242 | +To specify conditions on the inputs and outputs of an algorithm, \verb|\Require| and \verb|\Ensure| can be used. |
| 243 | + |
| 244 | +\subsubsection*{Example} |
| 245 | +\begin{minipage}[t]{0.45\textwidth} |
| 246 | +\begin{verbatim} |
| 247 | +\begin{algorithmic}[1] |
| 248 | + \Require $x \in \{0,1\}$ |
| 249 | + \Ensure $y \in \{1,2\}$ |
| 250 | + \State $y \gets x+1$ |
| 251 | + \State \Return $y$ |
| 252 | +\end{algorithmic} |
| 253 | +\end{verbatim} |
| 254 | +\end{minipage} |
| 255 | +\hfill |
| 256 | +\begin{minipage}[t]{0.45\textwidth} |
| 257 | +\begin{algorithmic}[1] |
| 258 | +\Require $x \in \{0,1\}$ |
| 259 | +\Ensure $y \in \{1,2\}$ |
| 260 | +\State $y \gets x+1$ |
| 261 | +\State \Return $y$ |
| 262 | +\end{algorithmic} |
| 263 | +\end{minipage} |
43 | 264 |
|
44 | 265 | \end{document}
|
0 commit comments