2

Right now I have an algorithm and I wish to reference it throughout a body of text.

However, the way that the algorithm is being reference is awkward. See top figure. Using \autoref, the algorithm is reference simply as "algorithm 1". I wish instead to reference a table that contain algorithm 1, and refer to readers to look at the table instead.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       

\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}

\begin{document}
\section{Introduction}

{\LinesNumberedHidden
    \begin{algorithm}
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \SetAlgorithmName{Algorithm}{} 

        Initialize: $x^0$ = 0;
        \begin{enumerate}   
            \item Pour hot coal on $f(x)$
            \item Freeze coal until $\|f(x)\|_\infty < \epsilon$
        \end{enumerate}

        \caption{Meta-Coal Algorithm}
        \label{algo:Coal Meta-Heuristic}
\end{algorithm}}

Look at my beautiful algorithm in \autoref{algo:Coal Meta-Heuristic}

\end{document}

Instead, I want something like this:

enter image description here

Now I am referring the algorithm that is CONTAINED in a table, I feel referencing this table more natural this way. The table's name could go on top of the algorithm or at the bottom (as currently shown). The caption for that table could be called something like "Description of Meta Coal algorithm" or just "Meta Coal algorithm" (as currently shown).

However, if I try to place the algorithm block inside of a \begin{table} the entire thing fails to compile!

\begin{table}
{\LinesNumberedHidden
    \begin{algorithm}
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \SetAlgorithmName{Algorithm}{} 

        Initialize: $x^0$ = 0;
        \begin{enumerate}   
            \item Pour hot coal on $f(x)$
            \item Freeze coal until $\|f(x)\|_\infty < \epsilon$
        \end{enumerate}

        \caption{Meta-Coal Algorithm}
        \label{algo:Coal Meta-Heuristic}
\end{algorithm}}
\end{table}

enter image description here

Does anyone know if there is a way to accomplish what I want with minimal effort?

Fraïssé
  • 3,139
  • Well, you're trying to nest a float in a float... –  Feb 25 '17 at 23:52
  • Please don't use spaces in label names -- it is possible of course, but can break if further processing of the label name is required. –  Feb 26 '17 at 00:07
  • 1
    A shorter solution is \caption{Meta-Coal Algorithm}\refstepcounter{table} The label will then use the table counter information –  Feb 26 '17 at 00:39
  • @ChristianHupfer Thanks but I'm still interested on getting that Table caption working i.e. Table 1 - Description of MC algorithm. – Fraïssé Feb 26 '17 at 00:50
  • \usepackage{caption} and \captionof{table}{Description of ...} perhaps? –  Feb 26 '17 at 00:54
  • 1
    Why not writing ".. my beautiful algorithm1"? – Guido Feb 26 '17 at 01:02
  • 1
    @Guido: ... because that's too easy? ;-) –  Feb 26 '17 at 01:07
  • Its my master's wish – Fraïssé Feb 26 '17 at 01:47
  • @MachineLearningisnotGod: I have added the \captionof solution as well to my answer –  Feb 26 '17 at 02:31

2 Answers2

4

See an alternate solution with \captionof at the end of this post.

I suggest to use cleveref and its \label[othercountername]{...} feature and \Cref{...} or \cref{...} instead, i.e.

\label[table]{yourlabelname}

Nesting floats in floats is nasty and does not work anyway.

If the name of the referenced counter should appear in the link provided by \Cref or \cref use \usepackage[nameinlink]{cleveref}.

Please load cleveref after hyperref!

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       

\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
\section{Introduction}

{\LinesNumberedHidden
    \begin{algorithm}
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \SetAlgorithmName{Algorithm}{} 

        Initialize: $x^0$ = 0;
        \begin{enumerate}   
            \item Pour hot coal on $f(x)$
            \item Freeze coal until $\|f(x)\|_\infty < \epsilon$
        \end{enumerate}
        \caption{Meta-Coal Algorithm}
        \label[table]{algo:CoalMeta-Heuristic}
\end{algorithm}}

Look at my beautiful algorithm in \Cref{algo:CoalMeta-Heuristic}

\end{document}

enter image description here

Alternate solution with \captionof from caption package:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       

\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{caption}
\usepackage{hyperref}

\begin{document}
\section{Introduction}

{\LinesNumberedHidden
    \begin{algorithm}
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \SetAlgorithmName{Algorithm}{} 

        Initialize: $x^0$ = 0;
        \begin{enumerate}   
            \item Pour hot coal on $f(x)$
            \item Freeze coal until $\|f(x)\|_\infty < \epsilon$
        \end{enumerate}
        \captionof{table}{Meta-Coal Algorithm}
        \label{algo:CoalMeta-Heuristic}
\end{algorithm}}

Look at my beautiful algorithm in \autoref{algo:CoalMeta-Heuristic}

\end{document}
  • Please note that this might confuse readers anyway. The 'Algorithm' is still listed in the list of algorithms, but it is not listed in the LoT! –  Feb 26 '17 at 00:10
2

Use the H specifier so the algorithm is not more a floating object and can be put inside a table.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}

\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}

\begin{document}
\section{Introduction}

\begin{table}
{\LinesNumberedHidden
    \begin{algorithm}[H]
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \SetAlgorithmName{Algorithm}{}

        Initialize: $x^0$ = 0;
        \begin{enumerate}
            \item Pour hot coal on $f(x)$
            \item Freeze coal until $\|f(x)\|_\infty < \epsilon$
        \end{enumerate}
\caption{Meta-Coal Algorithm}
\end{algorithm}}
\caption{Meta-Coal Algorithm}
\label{algo:Coal Meta-Heuristic}
\end{table}

Look at my beautiful algorithm in \autoref{algo:Coal Meta-Heuristic}

\end{document} 

enter image description here

Arzigoglu
  • 1,348