2

I am using the code from How may I remove \EndIf and \EndFor in algorithmicx?

The code, as is, works in article class. But in standalone it fails. I searched and found one needs to add [H] to algorithm when using standalone. Reference: standalone document with algorithm2e package

However, if you give the algorithm the [H] option, it will no longer be floating, so you can use it.

But that did not fix the problem.

Here is the code I am using

\documentclass[10pt,crop]{standalone}  %error
%\documentclass[10pt]{article}  %works
\usepackage{float}
\usepackage{algorithmicx}
\usepackage{algorithm} % http://ctan.org/pkg/algorithms
\usepackage{algpseudocode} % http://ctan.org/pkg/algorithmicx

\begin{document}

\begin{algorithm}[H] \begin{algorithmic}[1] \Procedure{A}{} \ForAll{$a\in A$} \If{$a\geq b$} \State\Return $a$ \EndIf \EndFor \EndProcedure \end{algorithmic} \end{algorithm}

\end{document}

Compiling with lualatex gives

(./first_order_series.aux)
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/ts1cmr.fd)

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ...

l.10 \begin{algorithm} [H] ?

Removing the [H] makes no difference. Compiling with article class, it works, no error

enter image description here

I wanted to use standalone to make a single pdf file which I can include as an image in the main document.

I also tried the workaround given in Problem with algorithmicx: perhaps a missing \item. What's wrong?

loading algpseudocode after algcompatible,

But it made no effect. The error is still there only when using standalone.

Is there a workaround?

TL 2022

Update

Here is a much simpler MWE which shows an error using algorithmic package

\documentclass[10pt,crop]{standalone}  %error
%\documentclass[10pt]{article}  %works
\usepackage{amsmath}
\usepackage{algorithmic}
\usepackage{float}

\begin{document} \begin{algorithmic}[H] \STATE $i\gets 10$ \end{algorithmic}

\end{document}

lualatex gives

(./first_order_series.aux)
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/ts1cmr.fd)

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ...

l.9 \STATE $i\gets 10$ ?

It looks like standalone is not compatible with all these algorithm packages?

Nasser
  • 20,220

1 Answers1

3

You should not use floats with standalone, but these are all vertical display environments so can not be used in a horizontal box like \mbox which is essentially what standalone provides. If you use the varwidth option it instead uses a \parbox so that you can use display material, and uses the varwidth package to choose a suitable width.

\documentclass[10pt,crop,varwidth]{standalone}  %error
%\documentclass[10pt]{article}  %works
\usepackage{float}
\usepackage{algorithmicx}
\usepackage{algorithm} % http://ctan.org/pkg/algorithms
\usepackage{algpseudocode} % http://ctan.org/pkg/algorithmicx

\begin{document}

\begin{algorithmic}[1] \Procedure{A}{} \ForAll{$a\in A$} \If{$a\geq b$} \State\Return $a$ \EndIf \EndFor \EndProcedure \end{algorithmic} \end{document}

enter image description here

David Carlisle
  • 757,742