1

I am trying to write a pseudo code in LaTeX, and want to list the algorithm in the \listofalgorithms. Using the following code, I can generate the pseudo code in pdf and also the algorithm is listed in the \listofalgorithms. However the linking does not work properly. When I click the line in the list of algorithm, it links me to the start of the document instead of linking to the algorithm.

The error generated while compiling and the code is written below. The algoritm is taken from the algorithmicx package user document.

\documentclass[a4paper,12pt]{report}
\usepackage{algorithm, algpseudocode}
\usepackage{hyperref}

\begin{document}
    \begin{algorithm}
    \caption{Euclid’s algorithm}\label{euclid}
    \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
    \State $a\gets b$
    \State $b\gets r$
    \State $r\gets a\bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}

    \listofalgorithms
 \end{document}

When I compile this, I get the following error:

./report.loa:1: Undefined control sequence. [...aces Euclid}}{4}{algorithm.\theHalgorithm }]

and in the report.loa file I have the following line written

\contentsline {algorithm}{\numberline {1}{\ignorespaces Euclid’s algorithm}}{4}{algorithm.\theHalgorithm }

I use latexmk version 4.43a and as an editor I use sublime with the LaTeXTools package.

closed as unclear what you're asking by Werner, user13907, Jesse, Svend Tveskæg, Stephen Dec 22 '15 at 8:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Welcome to TeX.SX! I get no error from this example. What TeX distribution are you using? – egreg Dec 17 '15 at 13:49
  • I use latexmk version 4.43a. And as an editor I use sublime with the LatexTools package – user5458351 Dec 17 '15 at 14:01
  • report.loa should read \contentsline {algorithm}{\numberline {1}{\ignorespaces Euclid's algorithm}}{1}{algorithm.1} (which it does when I compile the MWE with updatedTeX Live 2015). – Stephen Dec 22 '15 at 8:15
0

I finally find an answer to my question. It is listed in http://www.macfreek.nl/memory/LaTeX_package_conflicts#Hyperref_and_Algorithmic under Hyperref and Algorithm section. So I thought I share it with you.

As quoted from that link:

"If algorithm is loaded after hyperref, any \label{alg:myalgorithm} statements ends up faulty in the .aux file (it contains a spurious \theAlgorithm statement). This causes the above error if latex is run the second time (when it includes the faulty aux file). "

Apparently the error disappears when the order of the included packages are changed. Thus if you load the hyperref package before the algorithm, algpseudocode packages the code works as expected without any errors.

\documentclass[a4paper,12pt]{report}
% hyperref package need to be loaded before the algorithm package
\usepackage{hyperref}
\usepackage{algorithm, algpseudocode}

\begin{document}
    \begin{algorithm}
    \caption{Euclid’s algorithm}\label{euclid}
    \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}\Comment{We have the answer if r is 0}
    \State $a\gets b$
    \State $b\gets r$
    \State $r\gets a\bmod b$
    \EndWhile\label{euclidendwhile}
    \State \textbf{return} $b$\Comment{The gcd is b}
    \EndProcedure
    \end{algorithmic}
    \end{algorithm}

    \listofalgorithms
\end{document}
  • When compiled using an updated TeX Live 2015, it does not mater in which order the packages are loaded, it always works fine. – Stephen Dec 22 '15 at 8:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.