## LaTeX forum ⇒ Math & Science ⇒ Algorithm reference

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
xixonga
Posts: 3
Joined: Wed Apr 18, 2012 5:52 pm

### Algorithm reference

Hi,

i'm having a hard time trying to make a reference for an algorithm...
im just doing it the standard way:

\begin{algorithm}\caption{something}\begin{algorithmic}blablabla\end{algorithmic}\label{alg:MYALG}\end{algorithm} see algorithm \ref{alg:MYALG}

this recognizes the algorithm, but when i click the link (on pdf), it always jump to the first page of the document.

best regards
Last edited by Stefan Kottwitz on Wed Apr 18, 2012 9:21 pm, edited 1 time in total.
Reason: code marked

Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am
Try writing the \label command right after \caption, that is,
\caption{something}\label{alg:MYALG}

If this doesn't work, you should provide a fully compilable minimal example showing the problem.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

xixonga
Posts: 3
Joined: Wed Apr 18, 2012 5:52 pm
it's the same...it recognizes the algortihm but it jumps to the wrong page.

this is the chapter where the algorithm is:
\chapter{title} % Write in your own chapter title\label{Chapter2}\lhead{Chapter 2. \emph{title}} % Write in your own chapter title to set the page header \section{sec}blabla \subsection{sub}blabla ~\ref{fig:eyeModel}. \begin{figure}[ht]	\centering		\includegraphics{./Figures/eyeModel.jpg}		\rule{35em}{0.5pt}	\caption{Eye anatomy}	\label{fig:eyeModel}\end{figure} \subsection{sub}blabla \begin{algorithm}{}\caption{Iris Detection Algorithm}\label{alg:myaaa}\begin{algorithmic}[1]\State ....\end{algorithmic}\end{algorithm} blabla on lines 8 and 24 of algorithm \ref{alg:myaaa})

and the main
\documentclass[a4paper, 12pt, oneside]{Thesis}  % Use the "Thesis" style, based on the ECS Thesis style by Steve Gunn\graphicspath{{Figures/}}  % Location of the graphics files (set up for graphics to be in PDF format) % Include any extra LaTeX packages required\usepackage{algorithm}\usepackage{algpseudocode}\usepackage[square, numbers, comma, sort&compress]{natbib}  % Use the "Natbib" style for the references in the Bibliography\usepackage{verbatim}  % Needed for the "comment" environment to make LaTeX comments\usepackage{subfigure}  \usepackage{vector}  % Allows "\bvec{}" and "\buvec{}" for "blackboard" style bold vectors in maths\hypersetup{urlcolor=blue, colorlinks=true}  % Colours hyperlinks in blue, but this can be distracting if there are many links. %% ---------------------------------------------------------------- \begin{document} %configs and text  \input{./Chapters/Chapter1} % Introduction \input{./Chapters/Chapter2} % Background Theory  \input{./Chapters/Chapter3} % Experimental Setup %\input{./Chapters/Chapter4} % Experiment 1 %\input{./Chapters/Chapter5} % Experiment 2 %\input{./Chapters/Chapter6} % Results and Discussion %\input{./Chapters/Chapter7} % Conclusion %% ----------------------------------------------------------------% Now begin the Appendices, including them as separate files \addtocontents{toc}{\vspace{2em}} % Add a gap in the Contents, for aesthetics \appendix % Cue to tell LaTeX that the following 'chapters' are Appendices \input{./Appendices/AppendixA}	% Appendix Title %\input{./Appendices/AppendixB} % Appendix Title %\input{./Appendices/AppendixC} % Appendix Title \addtocontents{toc}{\vspace{2em}}  % Add a gap in the Contents, for aesthetics\backmatter %% ----------------------------------------------------------------\label{Bibliography}\lhead{\emph{Bibliography}}  % Change the left side page header to "Bibliography"\bibliographystyle{unsrtnat}  % Use the "unsrtnat" BibTeX style for formatting the Bibliography\bibliography{Bibliography}  % The references (bibliography) information are stored in the file named "Bibliography.bib" \end{document}  % The End

and thats it...simplified.
any guess?

Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am
Consider the following code:
\documentclass[a4paper]{book}  \usepackage{algpseudocode}\usepackage{algorithm}\usepackage{hyperref} \begin{document} \tableofcontents \chapter{One} \begin{algorithm}\caption{Iris Detection Algorithm}\label{alg:myaaa}\begin{algorithmic}[1]    \State $a=b+c$    \If {$a>2$}    \State \ldots    \EndIf\end{algorithmic}\end{algorithm} \chapter{Two} See Algorithm~\ref{alg:myaaa}. \end{document}

It works as expected: the link to the algorithm in Chapter 2 leads to the corresponding page in Chapter 1, which is not the first page of the document. It shows, by the way, what a minimal example is. You should provide something similar: a short piece of code that any reader could copy in a tex file, then compile and then observe the problem. Include any relevant file, as the document class, which is not standard, and avoid everything which is not connected with your query (\input commands, headings, packages not required to show the problem...) If you don't do so, it is quite difficult to help.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

sommerfee
Posts: 495
Joined: Mon Apr 09, 2007 4:20 pm
Contact:
BTW: The correct load order of algorithm and hyperref is:

\usepackage{float}  % load float package first!...\usepackage{hyperref} % let hyperref patch the float package stuff...\usepackage{algorithm} % let algorithm use the patched version of the float package

See hyperref README for details.

xixonga
Posts: 3
Joined: Wed Apr 18, 2012 5:52 pm
sorry but it is a big template, i can't post all of the code. I can tell you that im using this template:

http://www.sunilpatel.co.uk/thesis-template/

Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am
I've isolated the problem, which comes from some conflict with the caption package. The original documentclass, Thesis.cls, is based on book.cls and loads caption and hyperref in the order given in the following code:
\documentclass[a4paper]{book}  \usepackage[centerlast,small,sc]{caption} % Loaded by Thesis.cls\usepackage{hyperref}                     % Loaded by Thesis.cls \usepackage{algpseudocode}\usepackage{algorithm} \begin{document} \tableofcontents \chapter{One} \begin{algorithm}\caption{Iris Detection Algorithm}\label{alg:myaaa}\begin{algorithmic}[1]    \State $a=b+c$    \If {$a>2$}    \State \ldots    \EndIf\end{algorithmic}\end{algorithm} \chapter{Two} See Algorithm~\ref{alg:myaaa}. \end{document}

If you comment out the loading of caption, the link works as expected.
Since sommerfee is the author of caption, I hope he can provide a fix.

By the way, I think that Thesis.cls is a bit outdated. For example, it loads the obsolete subfigure package.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.

sommerfee
Posts: 495
Joined: Mon Apr 09, 2007 4:20 pm
Contact:
Juanjo wrote:The original documentclass, Thesis.cls, is based on book.cls and loads caption and hyperref in the order given in the following code:

Loading hyperref in a document class is a really very bad idea since hyperref must be loaded after most packages to operate correctly.

So IMHO the only proper solution would be rewriting the document class, it should either not load the hyperref package or at least offer an option to suppress the loading of the hyperref package, so one can load it for oneself later on.

If you comment out the loading of caption, the link works as expected.

No, it's wrong in this case, too, since the hyper link jumps to the first line of the algorithm (=contents of the floating environment) and not to the top of the floating environment.

For having a really correct hyper link one need to include the float package before hyperref, as mentioned in the hyperref README:
\documentclass[a4paper]{book}  \usepackage[centerlast,small,sc]{caption} % Loaded by Thesis.cls\usepackage{float}\usepackage{hyperref}                     % Loaded by Thesis.cls \usepackage{algpseudocode}\usepackage{algorithm} \begin{document} \tableofcontents \chapter{One} \begin{algorithm}\caption{Iris Detection Algorithm}\label{alg:myaaa}\begin{algorithmic}[1]    \State $a=b+c$    \If {$a>2$}    \State \ldots    \EndIf\end{algorithmic}\end{algorithm} \chapter{Two} See Algorithm~\ref{alg:myaaa}. \end{document}

Since sommerfee is the author of caption, I hope he can provide a fix.

Fixing incorrect load orders of packages is a very nasty job. Beside the hyperref package, the caption package supports (and patches) 16 packages. Can you imagine how much work it would be dealing with incorrect package load orders additionally? So to make my life simple (means: needing only about 700 lines of code for this) I have to assume that the load order is correct.

And what should the caption package exactly do if it detects an incorrect load order? Correct all problems because of wrong hyperref load order? IMHO this would be a job of a package called "generic_hyperref_load_order_fix" [1], and not of the caption package.

[1] Although writing such package looks like a good idea if would be impossible to do so.

sommerfee
Posts: 495
Joined: Mon Apr 09, 2007 4:20 pm
Contact:
I have examined this case a little further. Why is the (wrong) behavior with caption package different to the (wrong, too) behavior without caption package?

If hyperref is loaded after the float package, it patches two macros offered (or patched) by the float package: \@caption and \float@makebox. If hyperref is loaded before the float package, these two patches are missing.

If the caption package is loaded additionally, the caption package patches \@caption, too. (And the code offered by the caption package is adapted to both, float and hyperref package.) So when the load order is caption-hyperref-float, only one patch is missing in this case, the one hyperref does for \float@makebox:

\documentclass[a4paper]{book}  \usepackage[centerlast,small,sc]{caption} % Loaded by Thesis.cls\usepackage{hyperref}                     % Loaded by Thesis.cls \usepackage{algpseudocode}\usepackage{algorithm} \makeatletter% Patch of \float@makebox, taken from hyperref.sty [2012/02/28 v6.82p]%% DO NOT TRY THIS AT HOME, USE CORRECT LOAD ORDER OF PACKAGES INSTEAD!   \let\HyOrg@float@makebox\float@makebox   \renewcommand{\float@makebox}[1]{%     \HyOrg@float@makebox{%       #1\relax       \ifx\Hy@float@currentHref\@undefined       \else         \expandafter\hyper@@anchor\expandafter{%           \Hy@float@currentHref         }{\relax}%         \global\let\Hy@float@currentHref\@undefined       \fi     }%   }%\makeatother \begin{document} \tableofcontents \chapter{One} \begin{algorithm}\caption{Iris Detection Algorithm}\label{alg:myaaa}\begin{algorithmic}[1]    \State $a=b+c$    \If {$a>2$}    \State \ldots    \EndIf\end{algorithmic}\end{algorithm} \chapter{Two} See Algorithm~\ref{alg:myaaa}. \end{document}

So without caption package we have two missing patches (usually done by the hyperref package if the package load order would be correct), with caption package we have one missing patch. This leads to the different behavior, wrong in both cases, but wrong in a different way.

sommerfee
Posts: 495
Joined: Mon Apr 09, 2007 4:20 pm
Contact:
Update:

Heiko Oberdiek (Maintainer of the hyperref package) and me decided to implement a workaround for this incorrect load order hyperref-float within our packages since this is a very common mistake.

So when using at least version 6.82q of the hyperref package and version 3.3 of the caption package the wrong load order should not make any harm anymore.

Please note that this will not correct other incorrect load orders regarding the hyperref package (see README provided by hyperref), just the only one "hyperref before float", and it will only correct it if the caption package is used additionally.