3

I'm trying to generate a pdf file containing an algorithm, which will be included in the main document as a figure. I tried using standalone as follows:

% main.tex
\documentclass{standalone}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
$x \gets 0$\;
\If{$x < 10$}{
  $x \gets x + 1$\;
}
\end{algorithm}
\end{document}

Above example gives multiple errors when compiling and fails to generate a proper layout. I tried a few options for standalone (e.g. preview and varwidth) but failed to make it work. There is a similar question (using standalone with algorithmicx or algorithm2e) but it is rather old and does not have a proper answer. I already have my algorithms written in algorithm2e so I would rather not use a different algorithm package. I simply need a working example for this.

3 Answers 3

4

The algorithms are set in a floating environment, whereas standalone puts the contents in a box. This is incompatible.

However, if you give the algorithm the [H] option, it will no longer be floating, so you can use it. It will no longer have the possibility to have a caption, unless you use the capt-of or caption package, but you probably will put the captions in your main document.

Note that it will not be possible to have the algorithms cropped to their minimal horizontal space, because the algorithm package will put them in several layers of boxes, some of them with explicit width \hsize. But I guess you want all of them to be the same width, and you can just set \hsize to the desired width.

\documentclass[boxed,border=2pt]{standalone}
\usepackage{algorithm2e}
\begin{document}
\hsize=10cm
\setlength{\algomargin}{10pt}
  \begin{algorithm}[H]
    $x \gets 0$\; 
    \If{$x < 10$}{ $x \gets x + 1$\; }
  \end{algorithm}
\end{document}

enter image description here

But if you need the algorithms in another document, framed and with figure captions, why don't you just put them there and use the figure,boxed options?

\documentclass{article}
\usepackage[figure,boxed]{algorithm2e}
\begin{document}
\listoffigures
\section{Introduction}

This is my algorithm.

  \begin{algorithm}
    $x \gets 0$\; 
    \If{$x < 10$}{ $x \gets x + 1$\; }
    \caption{This is the algorithm}
  \end{algorithm}

This is the end of the document

\end{document}

enter image description here

This makes them float (it uses a real figure environment, so they may end up at a different location, as all floats. You can prevent that by using \usepackage{float}, and then \begin{algorithm}[H]. However, this may cause undesired white space on the page if the algorithm doesn't fit.

You can also remove the figure option; then the algorithm environment has a [H] option, that makes the algorithm non-floating. But then you must add a caption with the caption-of or caption package. In this case the caption will be placed outside the box, however.

\documentclass{article}
\usepackage[boxed]{algorithm2e}
\usepackage{capt-of}
\begin{document}
\listoffigures
\section{Introduction}

This is my algorithm.
  
  \SetAlgoSkip{medskip}
  \SetAlCapSkip{2ex}
  \begin{algorithm}[H]
    $x \gets 0$\; 
    \If{$x < 10$}{ $x \gets x + 1$\; }
  \captionof{figure}{This is the algorithm}
  \end{algorithm}

This is the end of the document

\end{document}

enter image description here

3

The algorithm environment from the alogrithm2e package seems to need outer par mode and seems to be implemented as/like a float. Floats (table, figure, ...) can not be catched inside boxes (\vbox, \mbox, \parbox, minipage, varwidth). That is why the algorithm environment does not work with my standalone class, with the varwidth option (paragraph mode) or without (restricted horizontal mode) nor with the preview or varwidth packages/environments inside an article class.

standalone or preview are simply not made for floating content in mind. Having a float in a tight document on its own does usually not make that much sense. In the case of a figure float it makes sense to put the figure content in a standalone document and then input it inside the figure environment in the main document. To simplify this standalone has also a float option which redefines common float environments to non-floats to make them incompatible.

Unfortunately both of this solutions do not work with algorithm. There seem to be no nofloat option or similar for it. Trying figure option of algorithm2e package together with the float option of standalone does not work either. You could ask the algorithm2e author to add such an option if you like ;-)


If you want your algorithm environments as a separate document, you could just use the article class instead of standalone while passing on the cropping feature. Then just use the standalone package in the main file to input that file.

% somealgo.tex
\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
$x \gets 0$\;
\If{$x < 10$}{
  $x \gets x + 1$\;
}
\end{algorithm}
\end{document}

% main.tex
\documentclass{article}
\usepackage{standalone}
\usepackage{algorithm2e}

\begin{document}

Text Text Text Text Text Text Text Text 

\input{somealgo}

Text Text Text Text Text Text Text 

\end{document}
1

The standalone class is incompatible with the algorithm environment of algorithm2e; it doesn't even work with the varwidth option of standalone, which helps in those cases where the document contents needs the text width.

There are several possibilities depending on the initial reason for using the standalone package.

  • If you just want to structure your document, \input the naked algorithm from the main file. No extra package required, this is plain (La)TeX.
    % main.tex
    \documentclass{article}
    \usepackage{algoritm2e}
    \begin{document}
    This is my algorithm:
    \input{myalgorithm}
    \end{document}
    
    % myalgorithm.tex
    \begin{algorithm}
    ...
    \end{algorithm}
  • If you want to be able to typeset the algorithm independently, e.g. while writing and testing the code, you can use the subfiles package. The structure would be as given below. You can run LaTeX on main.tex as well as on myalgorithm.tex, depending on whether you want to typeset the whole document or just the algorithm.
    % main.tex
    \documentclass{article}
    \usepackage{algorithm2e}
    \usepackage{subfiles}
    \begin{document}
    This is my algorithm:
    \subfile{myalgorithm}
    \end{document}
    
    % myalgorithm.tex
    \documentclass[main]{subfiles}% The pseudo-option main refers to main.tex; change it e.g. to thesis if the main file is named thesis.tex
    \begin{document}
    \begin{algorithm}
    ...
    \end{algorithm}
    \end{document}
  • If you need a feature unique to standalone, you probably have to switch to another algorithm package, which means that you have to rewrite your algorithm. The following combination of algpseudocode (from the algorithmicx bundle) with standalone plus varwidth option works. Note that using the algorithm package for adding a caption will again break the code.
    % myalgorithm.tex
    \documentclass[varwidth]{standalone}
    \usepackage{algpseudocode}
    \begin{document}
    \begin{algorithmic}
    \State $x \gets 0$
    \If{$x < 10$}
    \State $x \gets x + 1$
    \EndIf
    \end{algorithmic}
    \end{document}
6
  • Thank you for all alternative solutions. The format I should adhere to does not have anything separate for algorithms and requires us to add algorithms as framed figures. I tried wrapping the algorithm in a figure but there were some errors with algorithm2e. Other packages seem to work so maybe it is due to similar reasons as the issue here. That's why I thought about generating algorithms as standalone pdf files. I have now compiled the algorithm as article instead of standalone, and then used pdfcrop to remove the extra whitespace which is rather silly but it seems to work.
    – none
    Sep 30, 2021 at 14:36
  • @none Have you tried to load algorithm2e with the boxed option, i.e., \usepackage[boxed]{algorithm2e}?
    – gernot
    Sep 30, 2021 at 14:47
  • Not only the algorithm needs to be framed, but it should behave like a figure (i.e. it needs a figure caption and it should be listed in the list of figures). So I still need to wrap it in figure no matter I use the boxed option or not and unfortunately it fails to compile.
    – none
    Sep 30, 2021 at 15:04
  • @none It seems you have solved your current issue already with some hack. Next time, it will be more fruitful to post a question with the description of your real problem (boxed, captioned algorithm), not with a derived one resulting from a particular way you try to solve it.
    – gernot
    Sep 30, 2021 at 15:08
  • 1
    @none Of course there is a reason ;-) The packages were developed independently from each other, like most pairs of packages, and it seems that so far the issue wasn't pressing enough to invest the time to investigate it. So the reason may still be unknown. Thinking of it, it is in fact surprising how many packages actually do work together.
    – gernot
    Sep 30, 2021 at 20:21

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.