Graphics, Figures & TablesPackages algorithm2e and algpseudocode incompatible?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
kynan
Posts: 6
Joined: Sat Aug 21, 2010 1:20 pm

Packages algorithm2e and algpseudocode incompatible?

Post by kynan »

I want to typeset pseudocode in a floating listing using the packages "algorithm2e" and "algpseudocode". The reason I can't use "algorithm" is that I (have to) use a documentclass based on "memoir", which is incompatible with "algorithm".

Now the problem is that that "algorithm2e" and "algpseudocode" seem to be incompatible. Take the following minimal example (note that the document class doesn't matter, I just used article so that I can switch back and forth between "algorithm" and "algorithm2e"; it works just fine with "algorithm"):

Code: Select all

\documentclass{article}
\usepackage{algorithm2e}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Test algorithm}
\begin{algorithmic}[1]
\Procedure {Test}{}
  \For {$i = 1$ to $10$}
    \State print $i$
  \EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
I get the following errors using pdflatex:

Code: Select all

! Missing \endcsname inserted.
<to be read again> 
                   \ALG@currentblock@2 
l.11   \EndFor
              
? 
! Extra \endcsname.
\ALG@makenobeginrepeat ...LG@thisblock \endcsname 
                                                  \relax \def \ALG@thisblock...
l.11   \EndFor
              
? 
! Missing number, treated as zero.
<to be read again> 
                   \ALG@b@2@EndFor@0 
l.11   \EndFor
              
? 
! Missing \endcsname inserted.
<to be read again> 
                   \ALG@currentblock@2 
l.12 \EndProcedure
                  
? 
! Extra \endcsname.
\ALG@makenobeginrepeat ...LG@thisblock \endcsname 
                                                  \relax \def \ALG@thisblock...
l.12 \EndProcedure
                  
? 
! Missing number, treated as zero.
<to be read again> 
                   \ALG@b@2@EndProcedure@0 
l.12 \EndProcedure
                  
? 
! Missing number, treated as zero.
<to be read again> 
                   \ALG@currentblock@2 
l.45 \end{algorithmic}
                      
? 
! Missing = inserted for \ifnum.
<to be read again> 
                   \ALG@currentblock@2 
l.45 \end{algorithmic}
                      
? 
! Missing number, treated as zero.
<to be read again> 
                   \ALG@currentblock@2 
l.45 \end{algorithmic}
                      
? 
Can anybody help or knows a workaround? That would be greatly appreciated!

kynan
Last edited by kynan on Wed Aug 25, 2010 8:19 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Packages algorithm2e and algpseudocode incompatible?

Post by gmedina »

Hi,

you could use the caption package to define a new floating object:

Code: Select all

\documentclass{article}
\usepackage{algpseudocode,caption}

\DeclareCaptionType{algorithm}

\begin{document}

\begin{algorithm}[!ht]
\caption{Test algorithm}
\begin{algorithmic}[1]
\Procedure {Test}{}
  \For {$i = 1$ to $10$}
    \State print $i$
  \EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
kynan
Posts: 6
Joined: Sat Aug 21, 2010 1:20 pm

Packages algorithm2e and algpseudocode incompatible?

Post by kynan »

Thanks, that looks promising. However your example does not compile for me (I checked the manual and couldn't find out why possibly):

Code: Select all

! Undefined control sequence.
l.4 \DeclareCaptionType
                       {algorithm}
? 

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.4 \DeclareCaptionType{a
                         lgorithm}
? 

! LaTeX Error: Environment algorithm undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.8 \begin{algorithm}
                     [!ht]
? 

! LaTeX Error: \caption outside float.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.9 \caption{
             Test algorithm}
? 
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <7> on input line 12.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <5> on input line 12.

! LaTeX Error: \begin{document} ended by \end{algorithm}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.17 \end{algorithm}
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Packages algorithm2e and algpseudocode incompatible?

Post by sommerfee »

kynan wrote:! Undefined control sequence.
l.4 \DeclareCaptionType
Your TeX distribution seems to be at least three years old.

Updating your TeX distribution or installing an actual one should help.
kynan
Posts: 6
Joined: Sat Aug 21, 2010 1:20 pm

Re: Packages algorithm2e and algpseudocode incompatible?

Post by kynan »

Anticipated that, it's texlive on Ubuntu 9.10 ;) Thanks for the clarification!
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Packages algorithm2e and algpseudocode incompatible?

Post by sommerfee »

kynan wrote:it's texlive on Ubuntu 9.10 ;)
Then this shouldn't happen, AFAIK Ubuntu 9.10 is using TeXlive 2009.

Please take a look at the log file to see what version of the caption package you are using. It should be at least version 3.1f from 2007. (TeXlive 2009 was shipped with version 3.1m.)
kynan
Posts: 6
Joined: Sat Aug 21, 2010 1:20 pm

Packages algorithm2e and algpseudocode incompatible?

Post by kynan »

Unfortunately it does not, it uses TeXlive 2007. I just tried to upgrade to TeXlive 2009 from this PPA, but failed. Now my whole TeX distribution is broken :(

edit: This was the final incentive to upgrade to Lucid. Now it works, thanks! I only get a warning from the memoir class:

Code: Select all

Class memoir Warning: You are using the caption package with the memoir 
(memoir)              class. This may cause unexpected or inconsistent 
(memoir)              results if you use any of memoir's captioning facilities.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Packages algorithm2e and algpseudocode incompatible?

Post by localghost »

Now that the problem seems to be solved, please mark the topic accordingly as described in Section 3 of the Board Rules.


Best regards and welcome to the board
Thorsten
kynan
Posts: 6
Joined: Sat Aug 21, 2010 1:20 pm

Re: [solved]Packages algorithm2e and algpseudocode incompati

Post by kynan »

Hope it's OK like that. Or should I add the solution to the initial post?
Post Reply