Here are the answers to your questions.
Why no \algorithmicstate
?
Perhaps an oversight by the package writer? Perhaps never intended to exist. Either way, you can create your own version of the command using:
\algnewcommand{\algorithmicstate}{\textbf{State:}}
\newcommand{\Dosth}{\textbf{Do Something }}
% Need space here, why?
This is answered here: \newcommand and spacing
Why does \def\ForEach{\ForAll}
work but \def\ForEach{\algorithmicforall}
not work?
Because \ForAll
is a more complicated macro than \algorithmicforall
. It is defined using the following command:
\algdef{S}[FOR]{ForAll}[1]{\algorithmicforall\ #1\ \algorithmicdo}
The \algdef
command does a lot of things, and one of the things it does with a for loop is to set up an expectation for the end of the for, i.e. the \EndFor
.
When you define \ForEach
to be ForAll
, you don't change the definition of \ForAll
you simply cause \ForEach
to act like ForAll
. But if you change ForEach
into \algorithmicforall
you fail to create the corresponding expectation for an end for, and the code fails when it encounters \EndFor
.
TL;DR version:
- algorithm - float wrapper for algorithms.
- algorithmic - first algorithm typesetting environment.
- algorithmicx - second algorithm typesetting environment.
- algpseudocode - layout for
algorithmicx
.
- algorithm2e - third algorithm typesetting environment.
I use algorithmicx
with algpseudocode
since they are superior to algorithmic
. I think algorithmicx
offers the same functionality compared to algorithm2e
, but I find its syntax clearer than the one provided by algorithm2e
.
Detailed version
algorithm
Float wrapper for algorithms. It is similar to block commands table
or figure
, which you wrap around your table/figure to give it a number and to prevent it being split over two pages. The documentation says:
When placed within the text without being encapsulated in a floating
environment algorithmic
environments may be split over a page
boundary, greatly detracting from their appearance. In addition, it is
useful to have algorithms numbered for reference and for lists of
algorithms to be appended to the list of contents. The algorithm
environment is meant to address these concerns by providing a floating
environment for algorithms.
Example:
\begin{algorithm}
\caption{Algorithm caption}
\label{alg:algorithm-label}
\begin{algorithmic}
... Your pseudocode ...
\end{algorithmic}
\end{algorithm}
algorithmic
This is the environment in which you write your pseudocode. You have predefined commands for common structures such as if
, while
, procedure
. All the commands are capitalized, e.g. \IF{cond} ... \ELSE ...
. The documentation1 says:
The algorithmic
environment provides an environment for describing
algorithms and the algorithm
environment provides a “float” wrapper
for algorithms (implemented using algorithmic
or some other method at
the users’s option). The reason for two environments being provided is
to allow the user maximum flexibility.
Example:
\begin{algorithmic}
\IF{some condition is true}
\STATE do some processing
\ELSIF{some other condition is true}
\STATE do some different processing
\ELSE
\STATE do the default actions
\ENDIF
\end{algorithmic}
algorithmicx
This package is like algorithmic
upgraded. It enables you to define custom commands, which is something algorithmic
can't do. So if you don't want to write your (crazy) custom commands, you will be fine with algorithmic
. You use algorithmicx
the same way you use algorithmic
, only the syntax and details are slightly different. See the example below for details. The documentation says:
The package algorithmicx itself doesn’t define any algorithmic
commands, but gives a set of macros to define such a command set. You
may use only algorithmicx, and define the commands yourself, or you
may use one of the predefined command sets
Example:
\begin{algorithm}
\caption{Euclid’s algorithm}
\label{euclid}
\begin{algorithmic}[1] % The number tells where the line numbering should start
\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 gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
algpseudocode
This is just a layout for algorithmicx
which tries to be as simillar as possible to algorithmic
. There are also other layouts, such as:
algcompatible
(fully compatible with the algorithmic package),
algpascal
(aims to create a formatted pascal program, you can transform a pascal program into an algpascal
algorithm description with some basic substitution rules).
algc
(just like the algpascal, but for c. This layout is incomplete).
The documentation says:
If you are familiar with the algorithmic package, then you’ll find it
easy to switch. You can use the old algorithms with the
algcompatible
layout, but please use the algpseudocode
layout for
new algorithms. To use algpseudocode
, simply use
\usepackage{algpseudocode}
. You don’t need to manually load the
algorithmicx
package, as this is done by algpseudocode
.
See the example for algorithmicx
, it uses the algpseudocode
layout.
algorithm2e
This is another algorithm environment just like algorithmic
or algorithmicx
. The documentation says:
Algorithm2e is an environment for writing algorithms in LaTeX2e. An
algorithm
is defined as floating object like figures. It provides
macros that allow you to create different sorts of key words, thus a
set of predefined key words is given. You can also change the
typography of the keywords.
Example:
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
Best Answer
You are using
That means you get no end signs in your printed algorithm. That is -- while writing your algorithm -- very problematic, because you can not proof if you added all closing end statements for your opened
for
,if
etc.But -- if printed ends or not -- you have to write in the tex code for your algorithm the closing ends, otherwise TeX throws error messages, because it counts opening and closing statements and throws an error if there is a missmatch!
So I recommend to delete/comment
[noend]
when calling the package.Because you did not describe what your algorithm should do I can only guess what you are doing. I tried to guess well, but please check my code and correct the place for closing ends if needed!
At last I suggest to use an editor showing you small lines to be able to localize corresponding open and end markings in your algorithm (see red arrows in the following image):
Now at last I recommend to use indents to mark correspondings open and closing signs as shown in the following assumed code for your algorithm:
If the algorithm is complicated you can run into problems with the "dangling else". To make clear where the else belongs just add an comment with the corresponding If clause as I showed in the algorithm code marked with
<========
.All that helps you to have an overview of your algorithm.
Now supposing I guessed right you have the final tex code
resulting in the following printed algorithm:
The advantage now is you can simply proof and see where the Ifs, For, Whiles etc. ends in the alporithm.
If you now (after the final proof everything is okay) add
[noend]
again you get:If that algorithm is better to read you have to decide by your own, I always prefer the print with corresponding end statements -- but that is my personal opinion!
There are other packages for generating algorithm (see ctan) you can try, then you can get printed lines in the algorithm like in the screenshot of my editor. Could it make easier to locate corresponding start and end marks ...