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.

I'm trying to mix the caption, hyperref, and algorithm2e packages, but I am having some issues. Here is a MWE.

\documentclass[12pt]{book}
\usepackage{float}
\usepackage[footnotesize,bf,center,figurename=Fig]{caption}
\usepackage{amsmath}
\usepackage{xpatch}
\usepackage[vlined,ruled]{algorithm2e}
\xpretocmd{\algorithm}{\hsize=\linewidth}{}{}
\usepackage[colorlinks,citecolor=green!90!black,draft=false,pdfencoding=auto]{hyperref}
\usepackage{cleveref}
\begin{document}
\listoffigures
\newpage
\begin{algorithm}[H]
\DontPrintSemicolon
\renewcommand\figurename{Algorithm}
\captionof{figure}{FOO.\label{alg:foo}}
    \KwIn{}
\end{algorithm}
\begin{figure}[H]
~
\caption{Consistent numbering between figures and algorithms.}
\end{figure}
\newpage
\captionsetup{list=no}
\begin{algorithm}[H]
\DontPrintSemicolon
\renewcommand\figurename{Algorithm}
\captionof{figure}{BAR.\label{alg:bar}}
    \KwIn{}
\end{algorithm}
\captionsetup{list=yes}
\begin{algorithm}[H]
\DontPrintSemicolon
\renewcommand\figurename{Algorithm}
\captionof{figure}{FOOBAR.\label{alg:foobar}}
    \KwIn{}
\end{algorithm}
Ref: \ref{alg:foobar}.
\end{document}

There are two problems:

  1. algorithm BAR is in the list of figures, while it is not supposed to because of captionsetup.
  2. the reference to algorithm FOOBAR links to algorithm FOO, both in the list of figures and in page 3.

Thank you for looking.

share|improve this question
    
I extended the answer, based on your changed MWE –  Christian Hupfer Jul 2 at 13:06

1 Answer 1

up vote 1 down vote accepted

In case you should not know, but algorithm2e package has the same mechanism as figure and provides a \listofalgorithms command, so in this case, there is no need of using captionof at all. However, the BAR algorithm appears there, contrary to the request. In this case, one has to use the \TitleOfAlgo{Title} command to set the caption, but prevent the entry to the List of algorithms.

In order to print the algorithm number, the option titlenumbered has to be specified in the \usepackage[...]{algorithm2e} option list.

\documentclass[12pt]{book}
\usepackage{float}
\usepackage[footnotesize,bf,center,figurename=Fig]{caption}
\usepackage{amsmath}
\usepackage{xpatch}
\usepackage[titlenumbered,vlined,ruled]{algorithm2e}
\xpretocmd{\algorithm}{\hsize=\linewidth}{}{}
\usepackage[colorlinks,citecolor=green!90!black,draft=false,pdfencoding=auto]{hyperref}
\usepackage{cleveref}
\begin{document}
\listofalgorithms
\newpage

\begin{algorithm}[H]
\DontPrintSemicolon
\caption{FOO.}\label{alg:foo}
    \KwIn{}
\end{algorithm}
\newpage
%\captionsetup{list=no}
\begin{algorithm}[H]
\DontPrintSemicolon
\TitleOfAlgo{BAR.}\label{alg:bar}
\KwIn{}
\end{algorithm}
\newpage

\begin{algorithm}[H]
\DontPrintSemicolon
%\renewcommand\figurename{Algorithm}
\caption{FOOBAR}.\label{alg:foobar}
    \KwIn{}
\end{algorithm}
Ref: \ref{alg:foobar}.
\end{document}

Update

As the OP explicitly wants to have the algorithms to be listed in the List of Figures, the package \algorithm2e has to be configured for this usage by adding the figure option to the option list of \usepackage[...]{algorithm2e}. The MWE works 'out of the box' then.

\documentclass[12pt]{book}
\usepackage{float}
\usepackage[footnotesize,bf,center,figurename=Fig]{caption}
\usepackage{amsmath}
\usepackage{xpatch}
\usepackage[figure,titlenumbered,vlined,ruled]{algorithm2e}
\xpretocmd{\algorithm}{\hsize=\linewidth}{}{}
\usepackage[colorlinks,citecolor=green!90!black,draft=false,pdfencoding=auto]{hyperref}
\usepackage{cleveref}
\begin{document}
\listoffigures
\newpage
\begin{algorithm}[H]
\DontPrintSemicolon
\renewcommand\figurename{Algorithm}
\captionof{figure}{FOO.\label{alg:foo}}
    \KwIn{}
\end{algorithm}
\begin{figure}[H]
~
\caption{Consistent numbering between figures and algorithms.}
\end{figure}
\newpage
\captionsetup{list=no}
\begin{algorithm}[H]
\DontPrintSemicolon
\renewcommand\figurename{Algorithm}
\captionof{figure}{BAR.\label{alg:bar}}
    \KwIn{}
\end{algorithm}
\captionsetup{list=yes}
\begin{algorithm}[H]
\DontPrintSemicolon
\renewcommand\figurename{Algorithm}
\captionof{figure}{FOOBAR.\label{alg:foobar}}
    \KwIn{}
\end{algorithm}
Ref: \ref{alg:foobar}.
\end{document}

The hyperlinks within the LOF are correct also.

share|improve this answer
    
Thank you for your answer, however, I want my figures and my algorithms to be printed using the same list, in this case \listoffigures, and I want the numbering to be consistant (fig 1, alg 2, fig 3, ...). I might have stripped down my actual document too much to provide the MWE, sorry about that. Anyway, if you think this is possible with your approach, please let me know (I have edited my example). Thanks. –  BigDawg Jul 2 at 11:32
    
It might be possible, since the package has a figure option. However, in my opinion, you should keep a clear separation between figure and algorithm Toc –  Christian Hupfer Jul 2 at 12:04
    
Thank you for your edit, however, while the links are now good, the title of each algorithm is completely messed up (the caption is below the second line, instead of between the first and second line, and centered, instead of flushed to the left). Do you know why that is ? Thank you. –  BigDawg Jul 2 at 14:43
    
I believe, that the algorithm2e is not setup to work with the caption package. Perhaps, there is some setup to be done with caption –  Christian Hupfer Jul 2 at 15:09
    
@BigDawg: After struggling with algorithm2e code, I came to the conclusion, that the first version is definitely easier to use, not because it is a solution by me, but also due to a sense of style that algorithms should not appear in a list of figures, in point of view. Perhaps other users find a better solution. algorithm2e seems to screw up the caption settings completely. –  Christian Hupfer Jul 3 at 8:17

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.