0

I am using a .sty file from my university to prepare my dissertation. In this file, there is a float for "Algorithms" that I use to define

Algorithm 1, Algorithm 2, ...

My problem is that, no matter what the labels of my algorithms in the appendix section are, when I call them, their reference number is linked to algorithms in the main body, as opposed to the algorithms that they belong to.

After doing some research, I realized that there are parts of the .sty file that govern how the "List of Algorithms" are organized. This file creates a file with extension .lom that keeps all information about the algorithm list. In this file, the labels of appendix algorithms are linked to algorithms in the body! Please find below parts of the .sty file that talk about "List of Algorithms" rules. I am wondering if someone can help me with debugging these lines.

\def\listofalgorithms{%
\newpage
\addcontentsline{toc}{chapter}{LIST OF ALGORITHMS}
\@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn\fi
\hbox{ }
\twoinmar
\centerline{\large\bf LIST OF ALGORITHMS}
\vspace{0.7in}
\begin{doublespace}
\@starttoc{lom}\if@restonecol\twocolumn\fi
\addtocontents{lom}{\noindent\underline{\bf Algorithm}\hfill\rm\protect\newline}
\end{doublespace}
\pagestyle {empty}
}

\newcounter{algorithm}[chapter]
\def\thealgorithm{\@arabic\c@chapter.\@arabic\c@algorithm}
\def\fps@algorithm{tbp}
\def\ftype@algorithm{1}
\def\ext@algorithm{lom}
\def\fnum@algorithm{Algorithm\thealgorithm}
\def\algorithm{\@float{algorithm}}
\let\endalgorithm\end@float
\@namedef{algorithm*}{\@dblfloat{algorithm}}
\@namedef{endalgorithm*}{\end@dblfloat}
Ruben
  • 13,448
Salivan
  • 303
  • 2
    \appendix resets the chapter counter to one, but should change \thechapter to use \Alph instead of \arabic. I can't test anything myself right now (see http://tex.stackexchange.com/questions/54806/miktex-package-manager-service-unavailable) but you might try \renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}} – John Kormylo Jul 08 '15 at 22:35
  • Where do you want me to add \renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}} ? I added it to the preamble, but did not help. I have to say that I do not have any problems with my other floats like figures and tables. – Salivan Jul 08 '15 at 23:40
  • 1
    You should add John's code immediately after the start of the appendix part of your document. – Mico Jul 08 '15 at 23:43
  • No it did not work. Please be reminded that my issue is not numbering. It is how the labels are linked to algorithms. The numbers are correct, but when I click on them a wrong algorithm shows up. – Salivan Jul 08 '15 at 23:54
  • Please provide an MWE that generates the problem, ideally with a link to the .sty file that you're required to use. – Mico Jul 09 '15 at 00:00
  • 1
    So, you are using the hyperref package, and your problem is that the link produced by a \ref command, when clicked, leads to a different algorithm than the one you intend (to an algorithm in the main text rather than to an algorithm in the appendices). Is this correct? If so, try \newcommand*{\theHalgorithm}{\theHchapter.\arabic{algorithm}} at the end of the preamble. – GuM Jul 09 '15 at 00:09
  • Thanks Gustavo. It solved my problem.

    Also thank you Mico and John for your help. I was preparing MWE files that Gustavo solved my problem.

    – Salivan Jul 09 '15 at 00:37
  • Actually, the solution proposed by @JohnKormylo should work too, if you apply it in the preamble—or better, if you modify the .sty file your university gave you: say \def\thealgorithm{\thechapter.\arabic{algorithm}}, and also, I’d rather add, \def\ftype@algorithm{4} and \def\fnum@algorithm{Algorithm \thealgorithm} (note the space before \thealgorithm!). – GuM Jul 09 '15 at 00:47
  • To remove doubt, look at the aux file for the \newlabel commands. The last entry should be the \hypertarget name. – John Kormylo Jul 09 '15 at 03:28

1 Answers1

1

I created a partial MWE, but without access to the university .sty file I couldn't get \listofalgorithms to do anything. Using \label and \ref worked as expected. The only thing written to the lom file was the title line. You should search for anything involving \@writefile{lom} or at least \@writefile.

\documentclass{book}
\usepackage{lipsum}

\let\twoinmar=\relax

\makeatletter
\def\listofalgorithms{%
\newpage
\addcontentsline{toc}{chapter}{LIST OF ALGORITHMS}
\@restonecolfalse\if@twocolumn\@restonecoltrue\onecolumn\fi
\hbox{ }
\twoinmar
\centerline{\large\bf LIST OF ALGORITHMS}
\vspace{0.7in}
%\begin{doublespace}% also undefined
\@starttoc{lom}\if@restonecol\twocolumn\fi
\addtocontents{lom}{\noindent\underline{\bf Algorithm}\hfill\rm\protect\newline}
%\end{doublespace}
\pagestyle {empty}
}

\newcounter{algorithm}[chapter]
\def\thealgorithm{\@arabic\c@chapter.\@arabic\c@algorithm}
\def\fps@algorithm{tbp}
\def\ftype@algorithm{1}
\def\ext@algorithm{lom}
\def\fnum@algorithm{Algorithm\thealgorithm}
\def\algorithm{\@float{algorithm}}
\let\endalgorithm\end@float
\@namedef{algorithm*}{\@dblfloat{algorithm}}
\@namedef{endalgorithm*}{\end@dblfloat}
\makeatother

\usepackage{hyperref}

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

\begin{document}
\listofalgorithms

\chapter{dummy}

\ref{first}

\ref{second}

\begin{algorithm}
\label{first}
first algorithm
\end{algorithm}

\lipsum[1-4]

\appendix
\chapter{this space intentionally left blank}

\begin{algorithm}
\label{second}
second algorithm
\end{algorithm}

\lipsum[5-8]
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120