244

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.

2
298
+50

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}
9
  • 6
    A lovely, detailed answer! It sounds like the TL;DRTL;DR version is: "use algorithmicx"? Your discussion is very clear about why, and whether, to prefer algorithmicx to algorithmic, but doesn't offer much comparison to algorithm2e. Is that because you are less familiar with it, because it is well known to be inferior, or something else?
    – LSpice
    Mar 2 '15 at 0:27
  • 2
    Yes, you are right: I am not very familiar with algorithm2e. I think they should offer similar functionality, but I prefer the syntax of algorithmicx. I edited my answer to make it clear that it is only my (subjective) preference.
    – Augustin
    Mar 2 '15 at 16:13
  • 5
    Even if you load algorithmicx I understand you still have to load algorithm too? Aug 28 '16 at 20:37
  • 18
    Extensive answer! For a quick decision it may be helpful to include pictures the compiled outputs for each package
    – dopexxx
    Aug 23 '18 at 10:51
  • 5
    When I read the section about algorithmicx, I didn't immediately get that algpseudocode should also be loaded in order to use the package. Maybe I'm the only one who experienced that, otherwise it could maybe be added somewhere. Sep 26 '18 at 8:03
6
  • I found the recent algpseudocodex package to be a good step towards a unification of the variety of algorithm* packages.
  • That package combines nicely some features (e.g. genericity, templating) of algorithmicx and the lean layout of algorithm2e, that is, the vertical scope lines.

enter image description here (Example is taken from the manual.)

3
  • 1
    Hello. Thanks for sharing. I don't think that algorithm2e is hard to use: it uses a C-like syntax easy to learn. The main drawback of algorithm2e is that it uses floating environments...
    – projetmbc
    Apr 26 at 20:08
  • 1
    +1: I re-formatted your answer a bit, hope that's ok. Apr 26 at 20:23
  • @projetmbc Thx for mentioning. True, I modified my explanation. @Kuehner: Great, thx! I just removed the quoted forest as I am not referring to a LaTeX package.
    – Mario
    Apr 26 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.