From b4fc5f639874db951177ec539299d20908adb654 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Mon, 9 Jan 2006 00:44:40 +0000 Subject: doc 4 git-svn-id: svn://tug.org/texlive/trunk@80 c570f23f-e606-0410-a88d-b1316a301751 --- .../doc/latex/algorithmicx/algorithmicx.pdf | Bin 0 -> 201459 bytes .../doc/latex/algorithmicx/algorithmicx.tex | 1609 ++++++++++++++++++++ 2 files changed, 1609 insertions(+) create mode 100644 Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.pdf create mode 100644 Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.tex (limited to 'Master/texmf-dist/doc/latex/algorithmicx') diff --git a/Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.pdf b/Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.pdf new file mode 100644 index 00000000000..9e837c377c0 Binary files /dev/null and b/Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.pdf differ diff --git a/Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.tex b/Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.tex new file mode 100644 index 00000000000..c04febbb9e1 --- /dev/null +++ b/Master/texmf-dist/doc/latex/algorithmicx/algorithmicx.tex @@ -0,0 +1,1609 @@ +\documentclass{article} +\usepackage{algorithmicx} +\usepackage[ruled]{algorithm} +\usepackage[noset]{algpseudocode} +\usepackage[noset]{algpascal} +\usepackage[noset]{algc} + + + +\newcommand{\alg}{\texttt{algorithmicx}} +\newcommand{\old}{\texttt{algorithmic}} +\newcommand{\euk}{Euclid} +\newenvironment{alginc}[1][pseudocode]{\medskip\algsetlanguage{#1}\begin{algorithmic}[1]}{\end{algorithmic}\medskip} +\newenvironment{algi}[1][pseudocode]{\algsetlanguage{#1}\begin{algorithmic}[1]}{\end{algorithmic}} +\newcommand\ASTART{\bigskip\noindent\begin{minipage}[c]{0.5\linewidth}} +\newcommand\ACONTINUE{\end{minipage}\begin{minipage}[c]{0.5\linewidth}} +\newcommand\AENDSKIP{\end{minipage}\bigskip} +\newcommand\AEND{\end{minipage}} + + + +\title{The \alg\ package\footnote{This is the documentation for the version 1.1 +of the package. This package is released under LPPL.}} +\author{Sz\'asz J\'anos\\szaszjanos@users.sourceforge.net} + + + +\begin{document} +\maketitle +\begin{abstract} +The \alg\ package provides many possibilities to customize the layout of algorithms. +You can use one of the predefined layouts (\textbf{pseudocode}, \textbf{pascal} +and \textbf{c} and others), with or without modifications, or you can define a +completely new layout for your specific needs. +\end{abstract} +\tableofcontents + + + + +\section{Introduction} +All this begun in my last year at the university. The only thing that I knew of +\LaTeX\ was that it exists, and that it is ``good''. I started using it, but I needed to typeset some +algorithms. So I begun searching for a good algorithmic style, and I found the \old\ package. +It was a great joy for me, and I started to use it\dots\ +Well\dots\ Everything went nice, until I needed some block that wasn't defined in there. What to do? +I wasn't a \LaTeX\ guru, in fact I only knew the few basic macros. But there was no other way, so I opened +the style file, and I copied one existing block, renamed a few things, and voil\`a! This (and some other +small changes) where enough for me\dots + +One year later --- for one good soul --- I had to make some really big changes on the style. And there on +a sunny day came the idea. What if I would write some macros to let others create blocks automatically? +And so I did! Since then the style was completely rewritten\dots\ several times\dots + +I had fun writing it, may you have fun using it! I am still not a \LaTeX\ guru, so if you are, and you find +something really ugly in the style, please mail me! All ideas for improvements are welcome! + +Thanks go to Benedek Zsuzsa, Ionescu Clara, Sz\H ocs Zolt\'an, Cseke Botond, Kanoc +%Szotyori Zolt\'an Csaba +and many-many others. Without them I would have never started or continued \textbf{algorithmicx}. + + + + +\section{The predefined layouts} +The package \textbf{algorithmicx} itself doesn't define any algorithmic commands, but gives +a set of macros to define such a command set. So you may use \textbf{algorithmicx}, and define +the commands yourself, or use one of the predefined command sets. + +The following predefined command sets (layouts) are available: +\begin{description} +\item[algpseudocode] has the same look\footnote{almost :-)} as the one defined in the +\old\ package. The main difference is that while the \old\ package doesn't +allow you to modify predefined structures, or to create new ones, the \alg\ +package gives you full control over the definitions (ok, there are some +limitations --- you can not send mail with a, say, \verb:\For: command). +\item[algpascal] aims to create a formatted pascal program, it performs +automatic indentation (!), so you can transform a pascal program into an +\textbf{algpascal} algorithm description with some basic substitution rules. +\item[algc] yeah, just like the \textbf{algpascal}\dots\ but for C\dots\ +This layout is incomplete. +\end{description} + + + +\subsection{General informations} +Each algorithm begins with the \verb:\begin{algorithmic}[lines]: command, the +optional \verb:lines: controls the line numbering: $0$ means no line numbering, +$1$ means number every line, and $n$ means number lines $n$, $2n$, $3n$\dots\ until the +\verb:\end{algorithmic}: command, which ends the algorithm. + +\subsubsection{Simple lines} + +A simple line of text begins with \verb:\State:. This macro marks the begining of every +line. You don't need to use \verb:\State: before a command defined in the package, since +these commands use automatically a new line. + +To create a line that is not numbered, and not counted when numbering lines +(in case you choose to number lines), use the \verb:Statex: macro. This macro jumps into a new line, +the line gets no number, and any label will point to the previous numbered line. + +We will call \textit{statement\/}s the lines starting with \verb:\State:. The \verb:\Statex: +lines are not statements. + + + +\subsubsection{Placing comments in sources}\label{Putting comments in sources} + +Comments may be placed everywhere in the source using the \verb:\Comment: macro +(there are no limitations like those in the \old\ package), so feel the freedom! +If you would like to change the form in which comments are displayed, just +change the \verb:\algorithmiccomment: macro: + +\begin{verbatim} +\renewcommand{\algorithmiccomment}[1]{\hskip 3em$\rightarrow$ #1} +\end{verbatim} + +will result: +{ +\begin{alginc} +\renewcommand{\algorithmiccomment}[1]{\hskip 3em$\rightarrow$ #1} +\State $x\gets x+1$\Comment{Here is the new comment} +\end{alginc} +} + + + +\subsubsection{Labels and references} +Use the \verb:\label: macro, as usual, to label a line. When you use \verb:\ref: to reference +the line, the \verb:\ref: will be substituted with the corresponding line number. When using the +\textbf{algorithmicx} package together with the \textbf{algorithm} package, then you can label +both the algorithm and the line, and use the \verb:\algref: macro to reference a given line +from a given algorithm: + +\begin{verbatim} +\algref{}{} +\end{verbatim} + +\noindent\begin{minipage}[t]{0.5\linewidth} +\begin{verbatim} +The \textbf{while} in algorithm +\ref{euclid} ends in line +\ref{euclidendwhile}, so +\algref{euclid}{euclidendwhile} +is the line we seek. +\end{verbatim} +\end{minipage}\begin{minipage}[t]{0.5\linewidth} +The \textbf{while} in algorithm \ref{euclid} ends in line \ref{euclidendwhile}, +so \algref{euclid}{euclidendwhile} is the line we seek. +\end{minipage} + + + + +\subsection{The \textbf{algpseudocode} layout}\label{algpseudocode} +If you are familiar with the \old\ package, then you'll find it easy to +switch, and you have good chances to use previously written algorithms +without changing them (some comments needs to be placed to their right +place, but you will be happy to have them there). You only need to read the +section \ref{Putting comments in sources} and \ref{algpseudocode package options}. + +To use \textbf{algpseudocode}, simply load \verb:algpseudocode.sty:: + +\begin{verbatim} +\usepackage{algpseudocode} +\end{verbatim} + +You don't need to manually load the \textbf{algorithmicx} package, as this is done by +\textbf{algpseudocode}. + +Let's see for example the well known \textit{\euk's algorithm} (ok, an improved version): +{ +\begin{algorithm}[H] +\caption{\euk's algorithm}\label{euclid} +\begin{alginc} +\Procedure{\euk}{$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\label{euclidendwhile} + \State \Return $b$\Comment{The g.c.d. is b} +\EndProcedure +\end{alginc} +\end{algorithm} +} + +Created with the following source: +\begin{verbatim} +\begin{algorithm} +\caption{Euclid's algorithm}\label{euclid} +\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\label{euclidendwhile} + \State \textbf{return} $b$\Comment{The g.c.d. is b} +\EndProcedure +\end{algorithmic} +\end{algorithm} +\end{verbatim} + +The \verb:\State: stands at the beginning of each simple statement; the respective +statement is put in a new line, with the needed indentation. +The \verb:\Procedure: \dots\verb:\EndProcedure: and +\verb:\While: \dots\verb:\EndWhile: blocks (like any block defined in the +\textbf{algpseudocode} layout) automatically indent their content. +The indentation of the source doesn't matter, so + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\Repeat +\Comment{forever} +\State this\Until{you die.} +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\Repeat +\Comment{forever} +\State this\Until{you die.} +\end{algi} +} +\AENDSKIP + +But, generally, it is a good idea to keep the source indented, since you will find +errors much easier. And your tex file looks better! + +All examples and syntax descriptions will be shown as the previous +example --- the left side shows the \LaTeX\ input, and the right side +the algorithm, as it appears in your document. I'm cheating! Don't look +in the \verb:tex: file! Believe what the examples state! I'm using some +undocumented and dirty stuff to create all these examples. You may be more +confused after opening \verb:algorithmicx.tex: as you was before. + +In the case of syntax +descriptions the text between "$<$" and "$>$" is symbolic, so if you type +what you see on the left side, you will not get the algorithm on the +right side. But if you replace the text enclosed between "$<$" and "$>$" with a proper piece of +algorithm, then you will get what you probably want. The parts between +"$[$" and "$]$" are optional. + + +\subsubsection{The \textbf{for} block} +The \textbf{for} block has the following syntax: + +\ASTART +\begin{verbatim} +\For{} + +\EndFor +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\For{$<$text$>$} + \State $<$body$>$ +\EndFor +\end{algi} +} +\AEND + +\ASTART +\begin{verbatim} +\ForAll{} + +\EndFor +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\ForAll{$<$text$>$} + \State $<$body$>$ +\EndFor +\end{algi}} +\AENDSKIP + +\noindent Example: + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\State $sum\gets 0$ +\For{$i\gets 1, n$} + \State $sum\gets sum+i$ +\EndFor +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\State $sum\gets 0$ +\For{$i\gets 1, n$} + \State $sum\gets sum+i$ +\EndFor +\end{algi} +} +\AEND + +\subsubsection{The \textbf{while} block} +The \textbf{while} block has the form: + +\ASTART +\begin{verbatim} +\While{} + +\EndWhile +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\While{$<$text$>$} + \State $<$body$>$ +\EndWhile +\end{algi} +} +\AENDSKIP + +\noindent Example: + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\State $sum\gets 0$ +\State $i\gets 1$ +\While{$i\le n$} + \State $sum\gets sum+i$ + \State $i\gets i+1$ +\EndWhile +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\algsetlanguage{pseudocode} +\begin{algorithmic}[1] +\State $sum\gets 0$ +\State $i\gets 1$ +\While{$i\le n$} + \State $sum\gets sum+i$ + \State $i\gets i+1$ +\EndWhile +\end{algorithmic} +} +\AEND + +\subsubsection{The \textbf{repeat} block} +The \textbf{repeat} block has the form: + +\ASTART +\begin{verbatim} +\Repeat + +\Until{} +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\Repeat + \State $<$body$>$ +\Until{$<$text$>$} +\end{algi} +} +\AENDSKIP + +\noindent Example: + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\State $sum\gets 0$ +\State $i\gets 1$ +\Repeat + \State $sum\gets sum+i$ + \State $i\gets i+1$ +\Until{$i>n$} +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\State $sum\gets 0$ +\State $i\gets 1$ +\Repeat + \State $sum\gets sum+i$ + \State $i\gets i+1$ +\Until{$i>n$} +\end{algi} +} +\AEND + +\subsubsection{The \textbf{if} block} +The \textbf{if} block has the form: + +\ASTART +\begin{verbatim} +\If{} + +[ +\ElsIf{} + +... +] +[ +\Else + +] +\EndIf +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\If{$<$text$>$} + \State $<$body$>$ +\Statex \hskip-2\algorithmicindent[ +\ElsIf{$<$text$>$} + \State $<$body$>$ +\Statex \hskip-2\algorithmicindent... +\Statex \hskip-2\algorithmicindent] +\Statex \hskip-2\algorithmicindent[ +\Else + \State $<$body$>$ +\Statex \hskip-2\algorithmicindent] +\EndIf +\end{algi} +} +\AENDSKIP + +\noindent Example: + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\If{$quality\ge 9$} + \State $a\gets perfect$ +\ElsIf{$quality\ge 7$} + \State $a\gets good$ +\ElsIf{$quality\ge 5$} + \State $a\gets medium$ +\ElsIf{$quality\ge 3$} + \State $a\gets bad$ +\Else + \State $a\gets unusable$ +\EndIf +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\If{$quality\ge 9$} + \State $a\gets perfect$ +\ElsIf{$quality\ge 7$} + \State $a\gets good$ +\ElsIf{$quality\ge 5$} + \State $a\gets medium$ +\ElsIf{$quality\ge 3$} + \State $a\gets bad$ +\Else + \State $a\gets unusable$ +\EndIf +\end{algi} +} +\AEND + +\subsubsection{The \textbf{procedure} block} +The \textbf{procedure} block has the form: + +\ASTART +\begin{verbatim} +\Procedure{}{} + +\EndProcedure +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\Procedure{$<$name$>$}{$<$params$>$} + \State $<$body$>$ +\EndProcedure +\end{algi} +} +\AENDSKIP + +\noindent Example: See \euk's\ algorithm on page \pageref{euclid}. + +\subsubsection{The \textbf{function} block}The +\textbf{function} block has the same syntax as the \textbf{procedure} block: + +\ASTART +\begin{verbatim} +\Function{}{} + +\EndFunction +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\Function{$<$name$>$}{$<$params$>$} + \State $<$body$>$ +\EndFunction +\end{algi} +} +\AEND + +\subsubsection{The \textbf{loop} block} +The \textbf{loop} block has the form: + +\ASTART +\begin{verbatim} +\Loop + +\EndLoop +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\Loop + \State $<$body$>$ +\EndLoop +\end{algi} +} +\AEND + +\subsubsection{\textbf{Require} and \textbf{Ensure}} + +The starting conditions for the algorithm can be described with the \textbf{require} +instruction, and its result with the \textbf{ensure} instruction.: + +\ASTART +\begin{verbatim} +\Require something +\Ensure something +\end{verbatim} +\ACONTINUE +{ +\begin{algi} +\Require something +\Ensure something +\end{algi} +} +\AENDSKIP + +\noindent Example: + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\Require $x\ge5$ +\Ensure $x\le-5$ +\Statex +\While{$x>-5$} + \State $x\gets x-1$ +\EndWhile +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{alginc} +\Require $x\ge5$ +\Ensure $x\le-5$ +\Statex +\While{$x>-5$} + \State $x\gets x-1$ +\EndWhile +\end{alginc} +} +\AEND + + + + + +\subsubsection{Package options}\label{algpseudocode package options} + +The \texttt{algpseudocode} package supports the following options: + +\begin{description} +\item[compatible/noncompatible]\ \\If you would like to use old +algorithms, written with the \old\ package without (too much) +modification, then use the \textbf{compatible} option. This option +defines the uppercase version of the commands. Note that you still need +to remove the \verb:[...]: comments (these comments appeared due to some +limitations in the \old\ package, these limitations and comments are gone now). +The default \textbf{noncompatible} does not define the all uppercase +commands. +\item[noend/end]\ \\With \textbf{noend} specified all \textbf{end \dots} +lines are omitted. You get a somewhat smaller algorithm, and the odd +feeling, that something is missing\dots{} The \textbf{end} value is the +default, it means, that all \textbf{end \dots} lines are in their right +place. +\end{description} + + +\subsubsection{Changing command names} +One common thing for a pseudocode is to change the command names. Many people +use many different kind of pseudocode command names. In \textbf{algpseudocode} +all keywords are declared as \verb:\algorithmic:. You can change them +to output the text you need. For example, to obtain a hungarian translation, you write: + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\renewcommand\algorithmicwhile{\textbf{am\'\i g}} +\renewcommand\algorithmicdo{\textbf{ism\'eteld}} +\renewcommand\algorithmicend{\textbf{v\'ege}} +\begin{algorithmic}[1] +\State $x \gets 1$ +\While{$x < 10$} + \State $x \gets x - 1$ +\EndWhile +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algsetlanguage{pseudocode} +\begin{algorithmic}[1] +\renewcommand\algorithmicwhile{\textbf{am\'\i g}} +\renewcommand\algorithmicdo{\textbf{ism\'eteld}} +\renewcommand\algorithmicend{\textbf{v\'ege}} +\State $x \gets 1$ +\While{$x < 10$} + \State $x \gets x - 1$ +\EndWhile +\Statex +\end{algorithmic} +} +\end{minipage}\bigskip + +In some cases you may need to change even more (in the above example +\textbf{am\'\i g} and \textbf{v\'ege} should be interchanged in the \verb:\EndWhile: +text). Maybe the number of the parameters taken by some commands must be changed too. +This can be done with the command text customizing macros (see section +\ref{custom text}). Here I'll give only some examples of the most common usage: + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\renewcommand\algorithmicwhile{\textbf{am\'\i g}} +\renewcommand\algorithmicdo{\textbf{ism\'eteld}} +\renewcommand\algorithmicend{\textbf{v\'ege}} +\algrenewtext{EndWhile}{\algorithmicwhile\ \algorithmicend} +\begin{algorithmic}[1] +\State $x \gets 1$ +\While{$x < 10$} + \State $x \gets x - 1$ +\EndWhile +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algsetlanguage{pseudocode} +\begin{algorithmic}[1] +\renewcommand\algorithmicwhile{\textbf{am\'\i g}} +\renewcommand\algorithmicdo{\textbf{ism\'eteld}} +\renewcommand\algorithmicend{\textbf{v\'ege}} +\algrenewtext{EndWhile}{\algorithmicwhile\ \algorithmicend} +\State $x \gets 1$ +\While{$x < 10$} + \State $x \gets x - 1$ +\EndWhile +\Statex +\end{algorithmic} +} +\end{minipage} + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\newcommand\algorithmicto{\textbf{to}} +\algrenewtext{For}[3]% + {\algorithmicfor\ #1 \gets #2 \algorithmicto\ #3 \algorithmicdo} +\begin{algorithmic}[1] +\State $p \gets 1$ +\For{i}{1}{n} + \State $p \gets p * i$ +\EndFor +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algsetlanguage{pseudocode} +\begin{algorithmic}[1] +\newcommand\algorithmicto{\textbf{to}} +\algrenewtext{For}[3]% + {\algorithmicfor\ $#1 \gets #2$ \algorithmicto\ $#3$ \algorithmicdo} +\State $p \gets 1$ +\For{i}{1}{n} + \State $p \gets p * i$ +\EndFor +\Statex +\end{algorithmic} +} +\end{minipage}\bigskip + +You might want to create translation packages, which included after the \textbf{algpseudocode} +package translates the keywords to the language you need. + + + + + +\subsection{The \textbf{algpascal} layout} + + + + +The most important features of the \textbf{algpascal} layout is that +\textit{it performs automatically the end-of-block indentation}. In +section \ref{algorithmicx} you will see how to define such +automatically indented loops. Here is an example to demonstrate this +feature: + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\Begin +\State $sum:=0$; +\For{i=1}{n} + \State $sum:=sum+i$; +\State writeln($sum$); +\End. +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{algi}[pascal] +\Begin +\State $sum:=0$; +\For{i=1}{n} + \State $sum:=sum+i$; +\State writeln($sum$); +\End. +\end{algi} +} +\AENDSKIP + +Note, that the \verb:\For: is not closed explicitly, its end is +detected automatically. Again, the indentation in the source doesn't +affect the output. +In this layout every parameter passed to a command is put in +mathematical mode. + +\subsubsection{The \textbf{begin} \dots{} \textbf{end} block} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\Begin + +\End +\end{verbatim} +\ACONTINUE +{ +\begin{algi}[pascal] +\Begin + \State $<$body$>$ +\End +\end{algi} +} +\AENDSKIP + +The \verb:\Begin: \dots{} \verb:\End: block and the +\verb:\Repeat: \dots{} \verb:\Until: block are the only blocks in +the \textbf{algpascal} style (instead of \verb:\Begin: you may write +\verb:\Asm:). This means, that every other loop is ended automatically +after the following command (another loop, or a block). + + + +\subsubsection{The \textbf{for} loop} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\For{}{} + +\end{verbatim} +\ACONTINUE +{ +\begin{algi}[pascal] +\For{<$\relax$assign$\relax$>}{<$\relax$expr$\relax$>} + \State $<$command$>$ +\end{algi} +} +\AENDSKIP + +The \textbf{For} loop (as all other loops) ends after the following command (a block counts +also as a single command). + +\ASTART +\begin{verbatim} +\begin{algorithmic}[1] +\Begin + \State $sum:=0$; + \State $prod:=1$; + \For{i:=1}{10} + \Begin + \State $sum:=sum+i$; + \State $prod:=prod*i$; + \End +\End. +\end{algorithmic} +\end{verbatim} +\ACONTINUE +{ +\begin{alginc}[pascal] +\Begin + \State $sum:=0$; + \State $prod:=1$; + \For{i:=1}{10} + \Begin + \State $sum:=sum+i$; + \State $prod:=prod*i$; + \End +\End. +\end{alginc} +} +\AEND + +\subsubsection{The \textbf{while} loop} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\While{} + +\end{verbatim} +\ACONTINUE +{ +\begin{alginc}[pascal] +\While{<$\relax$expression$\relax$>} + \State $<$command$>$ +\end{alginc} +} +\AEND + + +\subsubsection{The \textbf{repeat}\dots\ \textbf{until} block} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\Repeat + +\Until{} +\end{verbatim} +\ACONTINUE +{ +\begin{alginc}[pascal] +\Repeat + \State $<$body$>$ +\Until{<$\relax$expression$\relax$>} +\end{alginc} +} +\AEND + +\subsubsection{The \textbf{if} command} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\If{} + +[ +\Else + +] +\end{verbatim} +\ACONTINUE +{ +\begin{alginc}[pascal] +\If{<$\relax$expression$\relax$>} + \State $<$command$>$ +\Statex \hskip-\algorithmicindent\hskip-\algorithmicindent[ +\Else + \State $<$command$>$ +\Statex \hskip-\algorithmicindent\hskip-\algorithmicindent] +\end{alginc} +} +\AENDSKIP + +Every \verb:\Else: matches the nearest \verb:\If:. + +\subsubsection{The \textbf{procedure} command} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\Procedure +\end{verbatim} +\ACONTINUE +{ +\begin{alginc}[pascal] +\Procedure $<$some text$>$ +\end{alginc} +} +\AENDSKIP + +\verb:\Procedure: just writes the ``procedure'' word on a new +line... You should put manually the \verb:\Begin:\dots\ \verb:\End: +block after it. + + +\subsubsection{The \textbf{function} command} + +\noindent\begin{minipage}[c]{0.5\linewidth} +\begin{verbatim} +\Function +\end{verbatim} +\ACONTINUE +{ +\begin{alginc}[pascal] +\Function $<$some text$>$ +\end{alginc} +} +\AENDSKIP + +Just like \textbf{Procedure}. + + + +\subsection{The \textbf{algc} layout} + +Sorry, the \textbf{algc} layout is incomplete. +The commands defined are: + +\begin{itemize} +\item\verb:\{:\dots\ \verb:\}: block +\item\verb:\For: with 3 params +\item\verb:\If: with 1 param +\item\verb:\Else: with no params +\item\verb:\While: with 1 param +\item\verb:\Do: with no params +\item\verb:\Function: with 3 params +\item\verb:\Return: with no params +\end{itemize} + + + + +\section{Custom algorithmic blocks}\label{algorithmicx} + + + +\subsection{Blocks and loops} + +Most of the environments defined in the standard layouts (and most probably +the ones you will define) are divided in two categories: + +\begin{description} +\item[Blocks] are the environments which contain an arbitrary number of +commands or nested blocks. Each block has a name, begins with a starting command +and ends with an ending command. The commands in a block are +indented by \verb:\algorithmicindent: (or another amount, as you will see later in this section). + +If your algorithm ends without closing all blocks, the \alg\ package gives +you a nice error. So be good, and close them all! + +Blocks are all the environments defined in the \verb:algpseudocode: +package, the \verb:\Begin: \dots \verb:\End: block in the +\verb:algpascal: package, and some other ones. +\item[Loops] (Let us call them loops\dots) The loops are environments +that include only one command, loop or block; a loop is closed +automatically after this command. So loops have no ending commands. If +your algorithm (or a block) ends before the single command of a loop, +then this is considered an empty command, and the loop is closed. Feel +free to leave open loops at the end of blocks! + +Most of the environments in the \verb:algpascal: and +\verb:algc: packages are loops. +\end{description} + +For some rare constructions you can create mixtures of the two +environments (see section \ref{setblock}). +Each block and loop may be continued with another one (like the \verb:If: +with \verb:Else:). + +\subsection{Defining blocks}\label{defblocks} + +There are several commands to define blocks. The difference is in what is defined +beyond the block. The macro \verb:\algblock: defines a new block with starting and +ending entity. + +\begin{verbatim} +\algblock[]{}{} +\end{verbatim} + +The defined commands have no parameters, and the text displayed by them is +\verb:\textbf{}: and \verb:\textbf{}:. You can change these texts later +(\ref{custom text}). + +With \verb:\algblockdefx: you can give the text to be output by the starting +and ending command and the number of parameters for these commands. In the text +reference with \#$n$ to the parameter number $n$. Observe that the text +is given in the form you define or redefine macros, and really, this is what happens. + +\begin{verbatim} +\algblockdefx[]{}{} + [][]{} + [][]{} +\end{verbatim} + +This defines a new block called \verb::, \verb:: opens the block, +\verb:: closes the block, +\verb:: displays \verb::, and has \verb:: parameters, +\verb:: displays \verb::, and has \verb:: parameters. +For both \verb:: and \verb::, if +\verb:: is given, then the first parameter is optional, and its default value +is \verb::. + +If you want to display different text (and to have a different number of parameters) +for \verb:: at the end of different blocks, then use +the \verb:\algblockx: macro. Note that it is not possible to display different starting texts, +since it is not possible to start different blocks with the same command. The \verb:: +defined with \verb:\algblockx: has the same behavior as if defined with \verb:\algblockdefx:. All ending commands +not defined with \verb:\algblockx: will display the same text, and the ones defined with this +macro will display the different texts you specified. + +\begin{verbatim} +\algblockx[]{}{} + [][]{} + [][]{} +\end{verbatim} + +If in the above definitions the \verb:: is missing, then the name of the starting command +is used as block name. If a block with the given name +already exists, these macros won't define a new block, instead of this the defined block will be used. +So both, the old and new starting and ending commands will open/close the block. +If \verb:: or \verb:: is empty, then +the definition does not define a new starting/ending command for the block, and then the +respective text must be missing from the definition. You may have more starting and ending commands +for one block. If the block name is missing, then a starting command must be given. + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\algblock[Name]{Start}{End} +\algblockdefx[NAME]{START}{END}% + [2][Unknown]{Start #1(#2)}% + {Ending} +\algblockdefx[NAME]{}{OTHEREND}% + [1]{Until (#1)} +\begin{algorithmic}[1] +\Start + \Start + \START[One]{x} + \END + \START{0} + \OTHEREND{\texttt{True}} + \End + \Start + \End +\End +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algblock[Name]{Start}{End} +\algblockdefx[NAME]{START}{END}% + [2][Unknown]{Start #1(#2)}% + {Ending} +\algblockdefx[NAME]{}{OTHEREND}% + [1]{Until (#1)} +\begin{algorithmic}[1] +\Start + \Start + \START[One]{x} + \END + \START{0} + \OTHEREND{\texttt{True}} + \End + \Start + \End +\End +\Statex +\end{algorithmic} +} +\end{minipage} + +\subsection{Defining loops} + +The loop defining macros are similar to the block defining macros. A loop has no ending command +and ends after the first statement, block or loop that follows the loop. +Since loops have no ending command, the macro \verb:\algloopx: would not have much sense. +The loop defining macros are: + +\begin{verbatim} +\algloop[]{} + +\algloopdefx[]{} + [][]{} +\end{verbatim} + +Both create a loop named \verb:: with the starting command \verb::. +The second also sets the number of parameters, and the text displayed by the starting command. + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\algloop{For} +\algloopdefx{If}[1]{\textbf{If} #1 \textbf{then}} +\algblock{Begin}{End} +\begin{algorithmic}[1] +\For + \Begin + \If{$a < b$} + \For + \Begin + \End + \Begin + \End + \End +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algloop{For} +\algloopdefx{If}% + [1]{\textbf{If} #1 \textbf{then}} +\algblock{Begin}{End} +\begin{algorithmic}[1] +\For + \Begin + \If{$a < b$} + \For + \Begin + \End + \Begin + \End + \End +\Statex +\end{algorithmic} +} +\end{minipage} + +\subsection{Continuing blocks and loops} + +For each block/loop you may give commands that close the block or loop and open another +block or loop. A good example for this is the \textbf{if}~\dots~\textbf{then}~\dots~\textbf{else} +construct. The new block or loop can be closed or continued, as any other blocks and loops. + +To create a continuing block use one of the following: + +\begin{verbatim} +\algcblock[]{}{}{} + +\algcblockdefx[]{}{}{} + [][]{} + [][]{} + +\algcblockx[]{}{}{} + [][]{} + [][]{} +\end{verbatim} + +All three macros define a new block named \verb::. If \verb:: is not given, +then \verb:: is used as the new block name. It is not allowed to have both +\verb:: missing, and \verb:: empty. The \verb:: command ends the +\verb:: block/loop and opens the \verb:: block. Since \verb:: may +end different blocks and loops, it can have different text +at the end of the different blocks/loops. If the \verb:: command doesn't find an +\verb:: to close, an error is reported. + +You can create continuing loops with the following macros: + +\begin{verbatim} +\algcloop[]{}{} + +\algcloopdefx[]{}{} + [][]{} + +\algcloopx[]{}{} + [][]{} +\end{verbatim} + +These macros create a continuing loop, the \verb:: closes the \verb:: +block/loop, and opens a \verb:: loop. + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\algblock{If}{EndIf} +\algcblock[If]{If}{ElsIf}{EndIf} +\algcblock{If}{Else}{EndIf} +\algcblockdefx[Strange]{If}{Eeee}{Oooo} + [1]{\textbf{Eeee} "#1"} + {\textbf{Wuuuups\dots}} +\begin{algorithmic}[1] +\If + \If + \ElsIf + \ElsIf + \If + \ElsIf + \Else + \EndIf + \EndIf + \If + \EndIf +\Eeee{Param} +\Oooo +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algblock{If}{EndIf} +\algcblock[If]{If}{ElsIf}{EndIf} +\algcblock{If}{Else}{EndIf} +\algcblockdefx[Strange]{If}{Eeee}{Oooo} + [1]{\textbf{Eeee} "#1"} + {\textbf{Wuuuups\dots}} +\begin{algorithmic}[1] +\If + \If + \ElsIf + \ElsIf + \If + \ElsIf + \Else + \EndIf + \EndIf + \If + \EndIf +\Eeee{Param} +\Oooo +\Statex +\end{algorithmic} +} +\end{minipage}\bigskip + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\algloop{If} +\algcloop{If}{Else} +\algblock{Begin}{End} +\begin{algorithmic}[1] +\If + \Begin + \End +\Else + \If + \Begin + \End +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algloop{If} +\algcloop{If}{Else} +\algblock{Begin}{End} +\begin{algorithmic}[1] +\If + \Begin + \End +\Else + \If + \Begin + \End +\Statex +\end{algorithmic} +} +\end{minipage}\bigskip + + + +\subsection{Even more customisation}\label{setblock} + +With the following macros you can give the indentation used by the new block (or loop), +and the number of statements after which the "block" is automatically closed. This value is $\infty$ +for blocks, 1 for loops, and 0 for statements. There is a special value, 65535, meaning that the +defined "block" does not end automatically, but if it is enclosed in a block, then the ending +command of the block closes this "block" as well. + +\begin{verbatim} +\algsetblock[]{}{} + {}{} + +\algsetblockdefx[]{}{} + {}{} + [][]{} + [][]{} + +\algsetblockx[]{}{} + {}{} + [][]{} + [][]{} + +\algcsetblock[]{}{}{} + {}{} + +\algcsetblockdefx[]{}{}{} + {}{} + [][]{} + [][]{} + +\algcsetblockx[]{}{}{} + {}{} + [][]{} + [][]{} +\end{verbatim} + +The \verb:: is the number of statements after that the block is closed. An empty +\verb:: field means $\infty$. The \verb:: gives the indentation of the block. +Leave this field empty for the default indentation. The rest of the parameters has the same +function as for the previous macros. + +\bigskip\noindent\begin{minipage}[b]{0.5\linewidth} +\begin{verbatim} +\algsetblock[Name]{Start}{Stop}{3}{1cm} +\algsetcblock[CName]{Name}{CStart}{CStop}{2}{2cm} +\begin{algorithmic}[1] +\Start + \State 1 + \State 2 + \State 3 +\State 4 +\Start + \State 1 +\Stop +\State 2 +\Start + \State 1 +\CStart + \State 1 + \State 2 +\State 3 +\Start + \State 1 +\CStart + \State 1 +\CStop +\end{algorithmic} +\end{verbatim} +\end{minipage} +\begin{minipage}[b]{0.5\linewidth} +{ +\algsetblock[Name]{Start}{Stop}{3}{1cm} +\algsetcblock[CName]{Name}{CStart}{CStop}{2}{2cm} +\begin{algorithmic}[1] +\Start + \State 1 + \State 2 + \State 3 +\State 4 +\Start + \State 1 +\Stop +\State 2 +\Start + \State 1 +\CStart + \State 1 + \State 2 +\State 3 +\Start + \State 1 +\CStart + \State 1 +\CStop +\Statex +\end{algorithmic} +} +\end{minipage}\bigskip + +The created environments behave as follows: + +\begin{itemize} +\item It starts with \verb:\Start:. The nested environments are +indented by 1 cm. +\item If it is followed by at least 3 environments (statements), then it closes +automatically after the third one. +\item If you put a \verb:\Stop: before the automatic closure, then this +\verb:\Stop: closes the environment. \verb:CStart: closes a block called \verb:Name: +and opens a new one called \verb:CName: and having an indentaion of 2 cm. +\item \verb:CName: can be closed with \verb:CStop: or it is closed automatically after +2 environments. +\end{itemize} + + + +\subsection{Parameters, custom text}\label{custom text} + +With \verb:\algrenewtext: you can change the number of parameters, and the text displayed by the +commands. With \verb:algnotext: you can make the whole output line disappear, but +it works only for ending commands, for beginning commands you will get an incorrect output. + +\begin{verbatim} +\algrenewcommand[]{} + [][]{} + +\algnotext[]{} +\end{verbatim} + +If \verb:: is missing, then the default text is changed, and if \verb:: is given, +then the text displayed at the end of \verb:: is changed. + +To make a command output the default text at the end of a block (say, you have changed the text +for this block), use \verb:\algdefaulttext:. + +\begin{verbatim} +\algdefaulttext[]{} +\end{verbatim} + +If the \verb:: is missing, the default text itself will be set to the default value +(this is \verb:\textbf{}:). + + + +\subsection{The base defining macro} + +All block and loop defining macros call the same macro. You may use this macro to gain a +better acces to what will be defined. This macro is \verb:\algdef:. + +\begin{verbatim} +\algdef{}... +\end{verbatim} + +Depending on the flags the macro can have many forms. + +\begin{center} +\begin{tabular}{|c|l|} +\hline +\textbf{Flag}&\textbf{Meaning}\\ +\hline +s&starting command, without text\\ +S&starting command with text\\ +c&continuing command, without text\\ +C&continuing command, with default text\\ +xC&continuing command, with block specific text\\ +\hline +e&ending command, without text\\ +E&continuing command, with default text\\ +xE&continuing command, with block specific text\\ +N&ending command, with default "no text"\\ +xN&ending command, with no text for this block\\ +\hline +b&block(default)\\ +l&loop\\ +L&loop closes after the given number of statements\\ +\hline +i&indentation specified\\ +\hline +\end{tabular} +\end{center} + +The \verb:: may be given for any combination of flags, and it is not allowed to have +\verb:: missing and \verb:: missing/empty. +For c, C, xC an old block is expected. For s, S, c, C, xC the \verb:: must be given. +For e, E, xE, N, xN the \verb:: must be given. For L the \verb:: must be given. +For i the \verb:: must be given. +For S, C, xC the starting text and related infos must be given. For E, xE the ending text must be given. +For each combination of flags give only the needed parameters, in the following order: + +\begin{verbatim} +\algdef{}[]{}{}{} + {}{} + [][]{} + [][]{} +\end{verbatim} + +The block and loop defining macros call \verb:\algdef: with the following flags: +\begin{center} +\begin{tabular}{|l|l|} +\hline +\textbf{Macro}&\textbf{Meaning}\\ +\hline +\verb:\algblock:&\verb:\algdef{se}:\\ +\hline +\verb:\algcblock:&\verb:\algdef{ce}:\\ +\hline +\verb:\algloop:&\verb:\algdef{sl}:\\ +\hline +\verb:\algcloop:&\verb:\algdef{cl}:\\ +\hline +\verb:\algsetblock:&\verb:\algdef{seLi}:\\ +\hline +\verb:\algsetcblock:&\verb:\algdef{ceLi}:\\ +\hline +\verb:\algblockx:&\verb:\algdef{SxE}:\\ +\hline +\verb:\algblockdefx:&\verb:\algdef{SE}:\\ +\hline +\verb:\algcblockx:&\verb:\algdef{CxE}:\\ +\hline +\verb:\algcblockdefx:&\verb:\algdef{CE}:\\ +\hline +\verb:\algsetblockx:&\verb:\algdef{SxELi}:\\ +\hline +\verb:\algsetblockdefx:&\verb:\algdef{SELi}:\\ +\hline +\verb:\algsetcblockx:&\verb:\algdef{CxELi}:\\ +\hline +\verb:\algsetcblockdefx:&\verb:\algdef{CELi}:\\ +\hline +\verb:\algloopdefx:&\verb:\algdef{Sl}:\\ +\hline +\verb:\algcloopx:&\verb:\algdef{Cxl}:\\ +\hline +\verb:\algcloopdefx:&\verb:\algdef{Cl}:\\ +\hline +\end{tabular} +\end{center} + + +\vfill +\section{Examples} +\subsection{A full pseudocode example} + + +\begin{verbatim} +\documentclass{article} +\usepackage{algorithm} +\usepackage{algpseudocode} +\begin{document} +\begin{algorithm} +\caption{The Bellman-Kalaba algorithm} +\begin{algorithmic}[1] +\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} + \ForAll {$v \in V(G)$} + \State $l(v) \leftarrow \infty$ + \EndFor + \State $l(u) \leftarrow 0$ + \Repeat + \For {$i \leftarrow 1, n$} + \State $min \leftarrow l(v_i)$ + \For {$j \leftarrow 1, n$} + \If {$min > e(v_i, v_j) + l(v_j)$} + \State $min \leftarrow e(v_i, v_j) + l(v_j)$ + \State $p(i) \leftarrow v_j$ + \EndIf + \EndFor + \State $l'(i) \leftarrow min$ + \EndFor + \State $changed \leftarrow l \not= l'$ + \State $l \leftarrow l'$ + \Until{$\neg changed$} +\EndProcedure +\Statex +\Procedure {FindPathBK}{$v$, $u$, $p$} + \If {$v = u$} + \State \textbf{Write} $v$ + \Else + \State $w \leftarrow v$ + \While {$w \not= u$} + \State \textbf{Write} $w$ + \State $w \leftarrow p(w)$ + \EndWhile + \EndIf +\EndProcedure +\end{algorithmic} +\end{algorithm} +\end{document} +\end{verbatim} +\eject + +{ +\algsetlanguage{pseudocode} +\begin{algorithm}[h] +\caption{The Bellman-Kalaba algorithm} +\begin{algorithmic}[1] +\Procedure {BellmanKalaba}{$G$, $u$, $l$, $p$} + \ForAll {$v \in V(G)$} + \State $l(v) \leftarrow \infty$ + \EndFor + \State $l(u) \leftarrow 0$ + \Repeat + \For {$i \leftarrow 1, n$} + \State $min \leftarrow l(v_i)$ + \For {$j \leftarrow 1, n$} + \If {$min > e(v_i, v_j) + l(v_j)$} + \State $min \leftarrow e(v_i, v_j) + l(v_j)$ + \State $p(i) \leftarrow v_j$ + \EndIf + \EndFor + \State $l'(i) \leftarrow min$ + \EndFor + \State $changed \leftarrow l \not= l'$ + \State $l \leftarrow l'$ + \Until{$\neg changed$} +\EndProcedure +\Statex +\Procedure {FindPathBK}{$v$, $u$, $p$} + \If {$v = u$} + \State \textbf{Write} $v$ + \Else + \State $w \leftarrow v$ + \While {$w \not= u$} + \State \textbf{Write} $w$ + \State $w \leftarrow p(w)$ + \EndWhile + \EndIf +\EndProcedure +\end{algorithmic} +\end{algorithm} +} +\eject + + + +\section{Bugs} + +At this time there are no known bugs. If you find a bug, please create +a small \LaTeX{} file to demonstrate this, and contact me on: + +\medskip +\textbf{szaszjanos@users.sourceforge.net} + + +\end{document} -- cgit v1.2.3