[Tex/LaTex] algorithm, algorithmic, algorithmicx, algorithm2e, algpseudocode = confused

algorithm2ealgorithmicalgorithmicxalgorithms

I am confused about the packages for writing simple "algorithms". There are too many options, and it is confusing when to use what. For example, if I use algpseudocode, then I can't add algorithmic because it is already included in the algorithm package.

Is there any definitive guidelines for a streamlined approach?
Any suggestion will be appreciated.

Best Answer

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:

  1. algcompatible (fully compatible with the algorithmic package),
  2. algpascal (aims to create a formatted pascal program, you can transform a pascal program into an algpascal algorithm description with some basic substitution rules).
  3. 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}
Related Question