Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The following code works ... fine!

\documentclass{book}

% \usepackage{subfig}
% \usepackage{subcaption}

\usepackage[colorlinks]{hyperref}

\usepackage{algorithm}
\usepackage[noend]{algorithmic}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
We outline this in Algorithm~\ref{alg1}.
%%
\begin{algorithm}
\caption{Algo}
\label{alg1}
\begin{algorithmic}
  \STATE A
\end{algorithmic}
\end{algorithm}

\end{document}

But once I add either subfig or subcaption to it as hinted, I get the warning

name{algorithm.1} has been referenced but does not exist, replaced by a fixed one

and the link to the algorithm does not work. Note that the code does not make use of any feature of subfig, or subcaption. It must come from some macro redefinition in those packages. After searching around I found algorithmic-fix from our @AxelSommerfeldt, but that does not fix the warning. (Strangely, it is also not needed when subfig and subcaption are out.)

What do I overlook?

share|improve this question
    
have you tried loading hyperref after the other packages? I'd avoid subfig and use subcaption –  cmhughes Feb 1 '13 at 16:32
    
@cmhughes The hyperref manual said somewhere that algorithm should be loaded after hyperref. –  mafp Feb 1 '13 at 16:56
1  
I get no error with either package (not both, of course). –  egreg Feb 1 '13 at 17:21
    
I also get no error- have you seen How do I update my TeX distribution? –  cmhughes Feb 1 '13 at 18:22
    
Loading hyperref after algorithm is still the wrong load order. However, with up-to-date TeX systems it should compile fine anyway, but only because Heiko (Oberdiek) and I have implemented extra code in hyperref and caption to handle such wrong load orders. See also: latex-community.org/forum/viewtopic.php?f=46&t=20075 –  Axel Sommerfeldt Feb 2 '13 at 8:31
show 3 more comments

1 Answer

up vote 4 down vote accepted

This is a copy-paste-answer originating at http://www.latex-community.org/forum/viewtopic.php?f=46&t=20075:

by myself on Fri Apr 20th, 2012

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.

by myself on Thu Apr 26th, 2012

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:

\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]{%
    ... (Code removed)
  }%
\makeatother

\begin{document}

\tableofcontents

\chapter{One}

\begin{algorithm}
\caption{Iris Detection Algorithm}\label{alg:myaaa}
...
\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.

by myself on Wed Jan 9th, 2013

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.

NOTE These are only excerpts of http://www.latex-community.org/forum/viewtopic.php?f=46&t=20075 and I have only copied postings of mine, not the ones from other people. So if you are interested in the whole story (and how my cat is looking), follow the link.

share|improve this answer
    
Thanks, I should have found that myself. How does algorithmic-fix fit into this? Is this obsolete? –  mafp Feb 2 '13 at 19:40
2  
@mafp algorithm does (only) define the floating environment with the help of the float package, and when this happens in a wrong order, it will cause the "has been referenced but does not exist, replaced by a fixed one" warning, and this is what the answer of mine is about. algorithmic does define a (non-floating) environment for typesetting algorithms, and this package still has hyperref issues since the author of the package has not fixed them. So in short: No, algorithmic-fix is (unfortunately) not obsolete, it fixes (other) problems which (unfortunately) still exists. –  Axel Sommerfeldt Feb 2 '13 at 20:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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