Numbering algorithms

2,783 views
Skip to first unread message

Ndebvuzamwai

unread,
Jul 7, 2011, 10:25:47 AM7/7/11
to LaTeX Users Group
I am using the following package to typeset my algorithms:

\usepackage{algorithmic2e} and my codes starts as:

\begin{algorithm}
\caption{Computing delta
\label{algo:delta}
.
.
.
.
\end{algorithm}

But when I typeset I get the numbering Algorithm 1 Algorithm 2
Algorithm 3........ Algorithm 10 etc across chapters in the document.

What I need is that the algorithms be numbered according the
subsections and reknew the numbering in the next section/subsection.
In other words I need Algorithm 1.1.2 in chapter 1 subsection 1,
Algorithm 3.4.1 in chapter 3 subsection 4 etc

How do i do this
Ndebvu

Peter Flynn

unread,
Jul 7, 2011, 12:15:13 PM7/7/11
to latexus...@googlegroups.com
On Thu, Jul 7, 2011 at 4:25 PM, Ndebvuzamwai <ekac...@gmail.com> wrote:
I am using the following package to typeset my algorithms:

\usepackage{algorithmic2e} and my codes starts as:

I don't find any package called that. There is a package called algorithm2e — did you mean that?
 
\begin{algorithm}
\caption{Computing delta
\label{algo:delta}
.
.
.
.
\end{algorithm}

But when I typeset I get the  numbering  Algorithm 1 Algorithm 2
Algorithm 3........ Algorithm 10 etc  across chapters in the document.

What I need is that the algorithms be numbered according the
subsections and reknew the numbering in the next section/subsection.
In other words I need Algorithm 1.1.2 in chapter 1 subsection 1,
Algorithm 3.4.1 in chapter 3 subsection 4 etc

The  algorithm2e package documentation says (p.4) that it has an option called algosection, which says:

 (default) algorithms are numbered within section numbers.

Have you tried that?

///Peter

Mico Loretan

unread,
Jul 7, 2011, 1:10:27 PM7/7/11
to LaTeX Users Group
Ndebvu:

First, which algorithm package are you using, algorithm2e or
algorithmic? I'm afraid I'm not familiar with a package called
algorithmic2e.

Second, assuming you're working with the algorithm2e package, your
needs should be met by inserting the following instructions in the
preamble of your document (after the \usepackage{algorithm2e}
statement}:

\usepackage{amsmath}
\numberwithin{algocf}{subsection}

This works because the amsmath package provides the nifty macro
\numberwithin and because the "number" of the current algorithm is
stored in a counter variable called "algocf".

Happy TeXing,

Mico

Ezekiel Kachisa

unread,
Jul 7, 2011, 2:51:58 PM7/7/11
to latexus...@googlegroups.com
Thanks for your quick response.

Sorry for the typo I meant algorithm2e.

Iam not sure if my package is in default BUT the only lines I have added in the preamble related to algorithms are:

\usepackage[ruled,vlined]{algorithm2e}
\usepackage{algorithmic}

I am a basic user of latex so i need a thorough advice.
ndebvu



--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

Mico Loretan

unread,
Jul 7, 2011, 3:16:46 PM7/7/11
to LaTeX Users Group
Greetings,

I'm confused: why are you loading both the algorithm2e and the
algorithmic packages in the preamble? I believe you should load one
package or the other, but not both.

Assuming you really want the algorithm2e package, you should delete
the \usepackage{algorithmic} statement and insert the following
instructions, immediately after the \usepackage[ruled,vlined]
{algorithm2e} statement:

\usepackage{amsmath}
\numberwithin{algocf}{subsection}

If, on the other hand, you decide that the algorithms should be
numbered by section rather than by subsection, the following
instruction will do (i.e., you won't need the two statements above):

\usepackage[ruled,vlined,algosection]{algorithm2e}

Sincerely, Mico



On Jul 7, 2:51 pm, Ezekiel Kachisa <ekach...@gmail.com> wrote:
> Thanks for your quick response.
>
> Sorry for the typo I meant algorithm2e.
>
> Iam not sure if my package is in default BUT the only lines I have added in
> the preamble related to algorithms are:
>
> \usepackage[ruled,vlined]{algorithm2e}
> \usepackage{algorithmic}
>
> I am a basic user of latex so i need a thorough advice.
> ndebvu
>
>
>
>
>
>
>
> On Thu, Jul 7, 2011 at 5:15 PM, Peter Flynn <anglebrac...@gmail.com> wrote:
> > On Thu, Jul 7, 2011 at 4:25 PM, Ndebvuzamwai <ekach...@gmail.com> wrote:
>
> >> I am using the following package to typeset my algorithms:
>
> >> \usepackage{algorithmic2e} and my codes starts as:
>
> > I don't find any package called that. There is a package called algorithm2e
> > — did you mean that?
>
> >> \begin{algorithm}
> >> \caption{Computing delta}
> >> \label{algo:delta}
> >> .
> >> .
> >> .
> >> .
> >> \end{algorithm}
>
> >> But when I typeset I get the  numbering  Algorithm 1 Algorithm 2
> >> Algorithm 3........ Algorithm 10 etc  across chapters in the document.
>
> >> What I need is that the algorithms be numbered according the
> >> subsections and reknew the numbering in the next section/subsection.
> >> In other words I need Algorithm 1.1.2 in chapter 1 subsection 1,
> >> Algorithm 3.4.1 in chapter 3 subsection 4 etc
>
> > The  algorithm2e package documentation says (p.4) that it has an option
> > called *algosection*, which says:
>
> > * (default) algorithms are numbered within section numbers.
> > *

Peter Flynn

unread,
Jul 7, 2011, 4:31:13 PM7/7/11
to latexus...@googlegroups.com

But I have already mentioned the class option that will do this. Have you tried to use it?

P

On 7 Jul 2011 20:53, "Ezekiel Kachisa" <ekac...@gmail.com> wrote:

Thanks for your quick response.

Sorry for the typo I meant algorithm2e.

Iam not sure if my package is in default BUT the only lines I have added in the preamble related to algorithms are:

\usepackage[ruled,vlined]{algorithm2e}
\usepackage{algorithmic}

I am a basic user of latex so i need a thorough advice.
ndebvu


On Thu, Jul 7, 2011 at 5:15 PM, Peter Flynn <angleb...@gmail.com> wrote:

>
> On Thu, Jul 7, 2011 at 4:25 PM, Ndebvuzamwai <ekac...@gmail.com> wrote:
>>

>> I am using the fo...


> --
> You received this message because you are subscribed to the Google Groups "LaTeX Users Group...



--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" ...

Mara

unread,
Jul 7, 2011, 9:48:53 PM7/7/11
to LaTeX Users Group
Dear Ndebvu,

I don't know whether or not you solved your problem. In case you still
have it. Please do the following:

First of all delete what ever package related to numbering the
algorithms.

If you are using thesis Template (preamble), then most probably you
would have .cls file, if so, try to open this file and add this
command,

\newtheorem{algorithm}{Algorithm}[section]

When you want to use it, write:
\being{algorithm}\label{...}
.
.
.
\end{algorithm}

try to run your .tex file,

The numbering of the algorithm will appear like:

Algorithm 1.2.1 %refer to chapter 1 section 2 algorithm 1

Algorithm 1.2.2 %refer to chapter 1 section 2 algorithm 2

and so on

if you write:

\newtheorem{algorithm}{Algorithm}[chapter]

Then the numbering of the algorithm will appear like:

Algorithm 1.1 %refer to chapter 1 algorithm 1

Algorithm 1.2 %refer to chapter 1 algorithm 2

regardless the section.

Now: If you are using the article class, then just add the following
command to your commands:

\newtheorem{algorithm}{Algorithm}[subsection]

then when you want to use it, write:

\being{algorithm}\label{...}
.
.
.
\end{algorithm}

then run your .tex file,

The numbering of the algorithm will appear like:

Algorithm 1.2.1 %refer to Section 1 subsection 2 algorithm 1

Algorithm 1.2.2 %refer to section 1 subsection 2 algorithm 2

and so on

if you write:

\newtheorem{algorithm}{Algorithm}[section]

Then the numbering of the algorithm will appear like:

Algorithm 1.1 %refer to section 1 algorithm 1

Algorithm 1.2 %refer to section 1 algorithm 2

regardless the subsection.

and so on...


Note: the word inside [...] control to which depth you want to reach.


I ran my latex files for all these cases, all of them have worked.
Also the I have tested them with cross-references, all work fine.

I hope I could help.
best regards
Mara

Ezekiel Kachisa

unread,
Jul 8, 2011, 5:17:48 AM7/8/11
to latexus...@googlegroups.com
Thank you all. Problem solved. The problem was on using two packages I guess. Your suggestions helped me a lot.
I had two different styles of algorithms in different chapters. I need to unify these.
Thanks again!
ezekiel


--
Reply all
Reply to author
Forward
0 new messages