Generalnumberwithin

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
cb2001
Posts: 6
Joined: Mon Sep 15, 2008 10:42 pm

numberwithin

Post by cb2001 »

hi,

i have problem with the latex command \numberwithin. i use it in combination with the algorithm package to set the numbering of all algorithms to the chapter level:

Code: Select all

usepackage{algorithmic}
\usepackage[boxed]{algorithm}
\numberwithin{algorithm}{chapter}
...

Code: Select all

\begin{algorithm}                      % enter the algorithm environment
\label{alg:algo}            % and a label for \ref{} commands later in the document
\underline{Objective} \\[10pt] bla.\\[10pt]
\caption{some algorithm} % caption
\end{algorithm}
now if i refer to this algorithm somewhere else in the document using

Code: Select all

\ref{alg:algo}
, latex uses the section counter instead of the actual algorithm numbering.
that means for example if the i add the algorithm inside section 2.5.1.2, which obviously is in chapter 2, the algorithm box correctly displays "Algorithm 2.1" but the reference (Algorithm \ref{alg:algo}) to the algorithm is displayed as "Algorithm 2.5.1.2".

Can anyone please give me a hand to solve this?

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

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: numberwithin

Post by Stefan Kottwitz »

Hi cb2001,

welcome to the board!
Just use \label after \caption, not before.

Stefan
LaTeX.org admin
cb2001
Posts: 6
Joined: Mon Sep 15, 2008 10:42 pm

Re: numberwithin

Post by cb2001 »

wow, that was fast!
thanks for the quick reply, and thanks for the help ... works like a charm! :D
Post Reply