8

I don't understand why the reference number in the PDF is wrong.

\documentclass[12pt,addpoints]{exam}
\usepackage[section]{algorithm}
\usepackage{algorithmicx}

\usepackage{algpseudocode}

\begin{document}
\section{title}
Some text before the algortihm.
\begin{algorithm}   
    \caption {An algorithm.}
    \begin{algorithmic}[H]
        \Function{theAlgorithm}{} : 
        \State test
        \EndFunction\\
        \label{alg:Algo}
    \end{algorithmic}
\end{algorithm}
Some more text after the algorithm  \ref{alg:Algo}.
\end{document}

PDF:

pdf generated

I don't know why I get the wrong reference number. How can I obtain the same that is shown in the algorithm title?

1
  • 2
    A general comment: It's never a bad idea to place the \label instruction immediately after the corresponding \caption instruction. If you want, you can even place the \label instruction inside the argument of the \caption instruction.
    – Mico
    Dec 15, 2015 at 10:02

1 Answer 1

15

The error comes from the place where you write the label. You have put it inside the algorithmic environment and so it refers to the line within that environment. If you wish to reference the algorithm itself, place \label after the \caption (and not inside algorithmic).

enter image description here

\documentclass{article}
\usepackage[section]{algorithm}

\usepackage{algpseudocode}

\begin{document}
\section{title}
Some text before the algortihm.
\begin{algorithm}   
  \caption{An algorithm.}
  \label{alg:Algo}
  \begin{algorithmic}
    \Function{theAlgorithm}{} : 
    \State test
    \EndFunction\\
  \end{algorithmic}
\end{algorithm}
Some more text after the algorithm  \ref{alg:Algo}.
\end{document}
2
  • Ok I see, but it writes 1 and non 1,1! How can I have the same format?
    – Eric
    Dec 15, 2015 at 6:51
  • @Eric see my edit Dec 15, 2015 at 7:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .