34

I am having trouble numbering and referencing algorithms.

I want to have the references labeled by the chapter, then within the chapter, such as:

First algorithm in chapter 1 - Algorithm 1.1

Second algorithm in chapter 1 - Algorithm 1.2

Here's a MWE of what I'm doing

\documentclass[11pt, letterpaper]{thesis}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}

\usepackage[chapter]{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\renewcommand{\thealgorithm}{\arabic{chapter}.\arabic{algorithm}} 

\begin{document} 

\chapter{First Chapter}

\begin{algorithm} 
  \begin{algorithmic}[1]
    \State Some code here 
  \end{algorithmic} 
  \label{alg:algorithm1}
\end{algorithm}

Algorithm \ref{alg:algorithm1} states blah blah blah. 

\end{document}

When I reference algorithm1, I get Algorithm I (where I is the roman numeral for 1) instead of Algorithm 1.1.

1 Answer 1

33

You need to use \caption before \label; otherwise, the string picked for the reference will be the last one where an anchor was set (typically the one of a sectional unit), in this case, the one from \chapter. Here's a modified version of your code showing the desired result:

\documentclass[11pt, letterpaper]{book}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}

\usepackage[chapter]{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\renewcommand{\thealgorithm}{\arabic{chapter}.\arabic{algorithm}} 

\begin{document} 

\chapter{First Chapter}

\begin{algorithm} 
  \begin{algorithmic}[1]
    \State Some code here 
  \end{algorithmic} 
  \caption{A test algorithm}
  \label{alg:algorithm1}
\end{algorithm}

Algorithm~\ref{alg:algorithm1} states blah blah blah and algorithm~\ref{alg:algorithm2} states blah blah blah. 

\begin{algorithm} 
  \begin{algorithmic}[1]
    \State Some code here 
  \end{algorithmic} 
  \caption{Another test algorithm}
  \label{alg:algorithm2}
\end{algorithm}

\end{document}

enter image description here

If you don't want to give explicit captions to your algorithms, you can use the \phantomcaption command from the caption package (this, however, could be confusing since you are referencing by number some objects which aren't really numbered):

\documentclass[11pt, letterpaper]{book}
\usepackage{caption}

\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}

\usepackage[chapter]{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}

\renewcommand{\thealgorithm}{\arabic{chapter}.\arabic{algorithm}} 

\begin{document} 

\chapter{First Chapter}

\begin{algorithm} 
  \begin{algorithmic}[1]
    \State Some code here 
  \end{algorithmic} 
  \phantomcaption
  \label{alg:algorithm1}
\end{algorithm}

Algorithm~\ref{alg:algorithm1} states blah blah blah and algorithm~\ref{alg:algorithm2} states blah blah blah. 

\begin{algorithm} 
  \begin{algorithmic}[1]
    \State Some code here 
  \end{algorithmic} 
  \phantomcaption
  \label{alg:algorithm2}
\end{algorithm}

\end{document}

enter image description here

2
  • Mistake 1 was a typo. In my actual document, \ref matched \label exactly. I'll edit my post so that they match. Commented Aug 6, 2012 at 3:07
  • @COM Ah, I see. I have then also edited my answer to reflect this. Commented Aug 6, 2012 at 14:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.