1

The contents of the algorithms thinks that an algorithm that uses algorithm is one algorithm and an algorithm that uses algorithm2e is another algorithm, not the second of what was imported with the other package, but a different entity or something, which, in my opinion is not OK.

I would like this:

  1 algo 1..................... page of algo 1
1 algo 2..................... page of algo 2

to be changed to this:

1 algo 1..................... page of algo 1
2 algo 2..................... page of algo 2

Alignment and numbering were changed above.

Here is the minimal example, based on this question:

\documentclass{article}
\usepackage{algorithm} 
\usepackage{algorithmic}  
\usepackage[algo2e]{algorithm2e} 
\renewcommand{\listalgorithmname}{List of Algorithms}
\addtocontents{loa}{\protect\thispagestyle{empty}}

\begin{document}
    \listofalgorithms
    \smallskip
    \begin{algorithm}%>- from algorithm package
        test
        \caption{algo 1}
    \end{algorithm}

    \begin{algorithm2e}%>- from algorithm2e package
        test
        \caption{algo 2}
    \end{algorithm2e}
\end{document}

Output: enter image description here


Chronicle

I had my thesis ready. However, just last night we submitted a paper based on it. I must submit my thesis too and I would like to include some of the algorithms written to the paper into the thesis. For several reasons, I have come into a situation where someone will give a solution to my problem, or I will just leave the the thesis without the paper-algorithms, which of course lead to a heartbroken thesis. :)

The algorithms of the paper are written with algorithm2e, while the algorithms of the thesis are written with algorithm and algorithmic. I have to submit it first thing tomorrow morning, thus I have no time to re-write them (I am editing my thesis now in other sections), that's why I will either hear a solution from the community here, or will leave the algorithms of the paper outside of my thesis!

  • The purpose of the question is not clear. What is the problem here? – Carlos Viegas Apr 23 '15 at 18:11
  • Sorry @CarlosViegas, edited! – gsamaras Apr 23 '15 at 19:03
  • Let me ask you, why do you need to use different algorithm packages? Why not only algorithm package? – Carlos Viegas Apr 23 '15 at 19:06
  • Expected @CarlosViegas, see my edit. I did not write them for keeping the post short! – gsamaras Apr 23 '15 at 19:10
1

A quick solution is to use \setcounter inside each algorithm. Moreover, to get the algorithms aligned in the list of algorithms you need to redefine \listof and \l@algocf commands and set \@dottedtocline with the desired horizontal spacing. Here is the code:

\documentclass{article}
\usepackage{algorithm} 
\usepackage{algorithmic}  
\usepackage[algo2e]{algorithm2e} 
\renewcommand{\listalgorithmname}{List of Algorithms}
\addtocontents{loa}{\protect\thispagestyle{empty}}

% add this to align the list of algorithms
\makeatletter
  \renewcommand*{\listof}[2]{%
    \@ifundefined{ext@#1}{\float@error{#1}}{%
      \@namedef{l@#1}{\@dottedtocline{1}{1em}{2.3em}}% line of the list (change from 1em to the desired value)
      \float@listhead{#2}%
      \begingroup\setlength{\parskip}{\z@}%
        \@starttoc{\@nameuse{ext@#1}}%
      \endgroup}}
  \renewcommand*\l@algocf{\@dottedtocline{1}{1em}{2.3em}}% line of the list (change from 1em to the desired value)
\makeatother

\begin{document}
    \listofalgorithms
    \smallskip
    \begin{algorithm}%>- from algorithm package
      \setcounter{algorithm}{0}% set the counter for the 1st algorithm
        test
        \caption{algo 1}
    \end{algorithm}

    \begin{algorithm2e}%>- from algorithm2e package
      \setcounter{algocf}{1}% set the counter for the 2nd algorithm
        test
        \caption{algo 2}
    \end{algorithm2e}

    \begin{algorithm}%>- from algorithm package
      \setcounter{algorithm}{2}% set the counter for the 3rd algorithm
        test
        \caption{algo 3}
    \end{algorithm}

    \begin{algorithm2e}%>- from algorithm2e package
      \setcounter{algocf}{3}% set the counter for the 4th algorithm
        test
        \caption{algo 4}
    \end{algorithm2e}
\end{document}
  • Good Carlos. However, I can not accept it, because the first number at every row of a content is not aligned to each next one. For example, in my screenshot, first's item 1 is not aligned well with the first's item 1. If you do not understand me, please let me know. You should see when you run your example that 1, 2, 3 and 4 are not aligned harmonically, but it's like the indentation differs per package. – gsamaras Apr 23 '15 at 19:48
  • I understand you, but you asked for one thing (wrong numbering at LOA), now you want another (text alignment). – Carlos Viegas Apr 23 '15 at 20:10
  • My bad Carlos, but it was not intended, I thought it was obvious. Sorry, I will edit my question accordingly. However, I think that you are close to the solution, so an edit of yours might save the day after all! :) – gsamaras Apr 23 '15 at 20:15
  • It is not that obvious, since here you are reproducing your problem with a minimal code example. And this code differs from the final format of your thesis. So, if you don't ask, we can't guess this is a problem. Anyway, I'm thinking about the alignment problem... but for now I don't know a solution. – Carlos Viegas Apr 23 '15 at 20:25
  • I see Carlos, I will do better next time! Do not care about my thesis, work on the minimal example and if you can achieve in fixing this, then I will try to import it in my thesis. – gsamaras Apr 23 '15 at 20:27

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.