LaTeX forum ⇒ Math & ScienceAlgorithm 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

Postby xixonga » Wed Apr 18, 2012 9:05 pm

Hi,

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

  1. \begin{algorithm}
  2. \caption{something}
  3. \begin{algorithmic}
  4. blablabla
  5. \end{algorithmic}
  6. \label{alg:MYALG}
  7. \end{algorithm}
  8.  
  9. 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.

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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Wed Apr 18, 2012 9:37 pm

Try writing the \label command right after \caption, that is,
  1. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

xixonga
Posts: 3
Joined: Wed Apr 18, 2012 5:52 pm

Postby xixonga » Thu Apr 19, 2012 1:49 pm

it's the same...it recognizes the algortihm but it jumps to the wrong page.

this is the chapter where the algorithm is:
  1. \chapter{title} % Write in your own chapter title
  2. \label{Chapter2}
  3. \lhead{Chapter 2. \emph{title}} % Write in your own chapter title to set the page header
  4.  
  5. \section{sec}
  6. blabla
  7.  
  8. \subsection{sub}
  9. blabla ~\ref{fig:eyeModel}.
  10.  
  11. \begin{figure}[ht]
  12. \centering
  13. \includegraphics{./Figures/eyeModel.jpg}
  14. \rule{35em}{0.5pt}
  15. \caption{Eye anatomy}
  16. \label{fig:eyeModel}
  17. \end{figure}
  18.  
  19. \subsection{sub}
  20. blabla
  21.  
  22. \begin{algorithm}{}
  23. \caption{Iris Detection Algorithm}
  24. \label{alg:myaaa}
  25. \begin{algorithmic}[1]
  26. \State ....
  27. \end{algorithmic}
  28. \end{algorithm}
  29.  
  30. blabla on lines 8 and 24 of algorithm \ref{alg:myaaa})



and the main
  1. \documentclass[a4paper, 12pt, oneside]{Thesis} % Use the "Thesis" style, based on the ECS Thesis style by Steve Gunn
  2. \graphicspath{{Figures/}} % Location of the graphics files (set up for graphics to be in PDF format)
  3.  
  4. % Include any extra LaTeX packages required
  5. \usepackage{algorithm}
  6. \usepackage{algpseudocode}
  7. \usepackage[square, numbers, comma, sort&compress]{natbib} % Use the "Natbib" style for the references in the Bibliography
  8. \usepackage{verbatim} % Needed for the "comment" environment to make LaTeX comments
  9. \usepackage{subfigure}
  10.  
  11.  
  12. \usepackage{vector} % Allows "\bvec{}" and "\buvec{}" for "blackboard" style bold vectors in maths
  13. \hypersetup{urlcolor=blue, colorlinks=true} % Colours hyperlinks in blue, but this can be distracting if there are many links.
  14.  
  15. %% ----------------------------------------------------------------
  16.  
  17. \begin{document}
  18.  
  19. %configs and text
  20.  
  21.  
  22. \input{./Chapters/Chapter1} % Introduction
  23.  
  24. \input{./Chapters/Chapter2} % Background Theory
  25.  
  26. \input{./Chapters/Chapter3} % Experimental Setup
  27.  
  28. %\input{./Chapters/Chapter4} % Experiment 1
  29.  
  30. %\input{./Chapters/Chapter5} % Experiment 2
  31.  
  32. %\input{./Chapters/Chapter6} % Results and Discussion
  33.  
  34. %\input{./Chapters/Chapter7} % Conclusion
  35.  
  36. %% ----------------------------------------------------------------
  37. % Now begin the Appendices, including them as separate files
  38.  
  39. \addtocontents{toc}{\vspace{2em}} % Add a gap in the Contents, for aesthetics
  40.  
  41. \appendix % Cue to tell LaTeX that the following 'chapters' are Appendices
  42.  
  43. \input{./Appendices/AppendixA} % Appendix Title
  44.  
  45. %\input{./Appendices/AppendixB} % Appendix Title
  46.  
  47. %\input{./Appendices/AppendixC} % Appendix Title
  48.  
  49. \addtocontents{toc}{\vspace{2em}} % Add a gap in the Contents, for aesthetics
  50. \backmatter
  51.  
  52. %% ----------------------------------------------------------------
  53. \label{Bibliography}
  54. \lhead{\emph{Bibliography}} % Change the left side page header to "Bibliography"
  55. \bibliographystyle{unsrtnat} % Use the "unsrtnat" BibTeX style for formatting the Bibliography
  56. \bibliography{Bibliography} % The references (bibliography) information are stored in the file named "Bibliography.bib"
  57.  
  58. \end{document} % The End



and thats it...simplified.
any guess?

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Thu Apr 19, 2012 3:34 pm

Consider the following code:
  1. \documentclass[a4paper]{book}
  2.  
  3. \usepackage{algpseudocode}
  4. \usepackage{algorithm}
  5. \usepackage{hyperref}
  6.  
  7. \begin{document}
  8.  
  9. \tableofcontents
  10.  
  11. \chapter{One}
  12.  
  13. \begin{algorithm}
  14. \caption{Iris Detection Algorithm}
  15. \label{alg:myaaa}
  16. \begin{algorithmic}[1]
  17. \State $a=b+c$
  18. \If {$a>2$}
  19. \State \ldots
  20. \EndIf
  21. \end{algorithmic}
  22. \end{algorithm}
  23.  
  24. \chapter{Two}
  25.  
  26. See Algorithm~\ref{alg:myaaa}.
  27.  
  28. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby sommerfee » Fri Apr 20, 2012 6:36 am

BTW: The correct load order of algorithm and hyperref is:

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


See hyperref README for details.

Link:
BBcode:
HTML:
Hide post links
Show post links

xixonga
Posts: 3
Joined: Wed Apr 18, 2012 5:52 pm

Postby xixonga » Tue Apr 24, 2012 6:40 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/

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Tue Apr 24, 2012 9:20 pm

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:
  1. \documentclass[a4paper]{book}
  2.  
  3. \usepackage[centerlast,small,sc]{caption} % Loaded by Thesis.cls
  4. \usepackage{hyperref} % Loaded by Thesis.cls
  5.  
  6. \usepackage{algpseudocode}
  7. \usepackage{algorithm}
  8.  
  9. \begin{document}
  10.  
  11. \tableofcontents
  12.  
  13. \chapter{One}
  14.  
  15. \begin{algorithm}
  16. \caption{Iris Detection Algorithm}\label{alg:myaaa}
  17. \begin{algorithmic}[1]
  18. \State $a=b+c$
  19. \If {$a>2$}
  20. \State \ldots
  21. \EndIf
  22. \end{algorithmic}
  23. \end{algorithm}
  24.  
  25. \chapter{Two}
  26.  
  27. See Algorithm~\ref{alg:myaaa}.
  28.  
  29. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby sommerfee » Thu Apr 26, 2012 7:40 am

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:
  1. \documentclass[a4paper]{book}
  2.  
  3. \usepackage[centerlast,small,sc]{caption} % Loaded by Thesis.cls
  4. \usepackage{float}
  5. \usepackage{hyperref} % Loaded by Thesis.cls
  6.  
  7. \usepackage{algpseudocode}
  8. \usepackage{algorithm}
  9.  
  10. \begin{document}
  11.  
  12. \tableofcontents
  13.  
  14. \chapter{One}
  15.  
  16. \begin{algorithm}
  17. \caption{Iris Detection Algorithm}\label{alg:myaaa}
  18. \begin{algorithmic}[1]
  19. \State $a=b+c$
  20. \If {$a>2$}
  21. \State \ldots
  22. \EndIf
  23. \end{algorithmic}
  24. \end{algorithm}
  25.  
  26. \chapter{Two}
  27.  
  28. See Algorithm~\ref{alg:myaaa}.
  29.  
  30. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby sommerfee » Thu Apr 26, 2012 8:05 am

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?

The answer:

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:

  1. \documentclass[a4paper]{book}
  2.  
  3. \usepackage[centerlast,small,sc]{caption} % Loaded by Thesis.cls
  4. \usepackage{hyperref} % Loaded by Thesis.cls
  5.  
  6. \usepackage{algpseudocode}
  7. \usepackage{algorithm}
  8.  
  9. \makeatletter
  10. % Patch of \float@makebox, taken from hyperref.sty [2012/02/28 v6.82p]
  11. %% DO NOT TRY THIS AT HOME, USE CORRECT LOAD ORDER OF PACKAGES INSTEAD!
  12. \let\HyOrg@float@makebox\float@makebox
  13. \renewcommand{\float@makebox}[1]{%
  14. \HyOrg@float@makebox{%
  15. #1\relax
  16. \ifx\Hy@float@currentHref\@undefined
  17. \else
  18. \expandafter\hyper@@anchor\expandafter{%
  19. \Hy@float@currentHref
  20. }{\relax}%
  21. \global\let\Hy@float@currentHref\@undefined
  22. \fi
  23. }%
  24. }%
  25. \makeatother
  26.  
  27. \begin{document}
  28.  
  29. \tableofcontents
  30.  
  31. \chapter{One}
  32.  
  33. \begin{algorithm}
  34. \caption{Iris Detection Algorithm}\label{alg:myaaa}
  35. \begin{algorithmic}[1]
  36. \State $a=b+c$
  37. \If {$a>2$}
  38. \State \ldots
  39. \EndIf
  40. \end{algorithmic}
  41. \end{algorithm}
  42.  
  43. \chapter{Two}
  44.  
  45. See Algorithm~\ref{alg:myaaa}.
  46.  
  47. \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.

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby sommerfee » Wed Jan 09, 2013 11:25 pm

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.

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Math & Science”

Who is online

Users browsing this forum: Baidu [Spider] and 4 guests