4

I'd like to use algorithm2e package to write listing of both algorithms and heuristics. I would like to use \SetAlgorithmName in a way that it would use separate counters for heuristics and algorithms.

So, e.g. I do:

\begin{algorithm}
\end{algorithm}

\SetAlgorithmName{Heuristics}

\begin{algorithm}
\end{algorithm}

and I get:

Algorithm 1

Heuristic 1

and not: Algorithm 1

Heuristic 2

as currently.

2
  • Is this question still relevant. What about other uses of algorithm environment? Do you change the Algorithm name each time before?
    – user31729
    Jan 30, 2017 at 19:02
  • The use of \SetAlgorithmName is wrong!
    – user31729
    Feb 27, 2017 at 7:27

1 Answer 1

1

This is an approach that works if hyperref is used (and also if it is not used) and sets the counter name according to the content of \algorithmcfname and links correctly.

Just define a counter, say heuristics and a macro \heuristicsname which contains Heuristics. If the counter is named foo, \fooname must be defined as well.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{algorithm2e}

\usepackage{hyperref}

\newcommand{\heuristicsname}{Heuristics}
\newcounter{heuristics}

\makeatletter
\AtBeginDocument{%
\@ifpackageloaded{hyperref}{%
  \let\orighyper@refstepcounter\hyper@refstepcounter%
  \newcommand{\specialhyper@refstepcounter}[1]{%
    \orighyper@refstepcounter{\@specialcountername}%
    \renewcommand{\@currentHref}{\@specialcountername.\csname the\@specialcountername\endcsname}%
  }
}{%
  \providecommand{\autoref}[1]{\ref{#1}}
  \newcommand{\specialhyper@refstepcounter}[1]{%
  }
}

\newcommand{\specialcountername}[1]{%
  \def\@specialcountername{#1}
}

\AtBeginEnvironment{algorithm}{%
\specialcountername{heuristics}%
\edef\temp@@a{\csname\@specialcountername name\endcsname}
\edef\temp@@b{\algorithmcfname}
\ifx\temp@@a\temp@@b
\csletcs{c@algocf}{c@\@specialcountername}
\csletcs{thealgocf}{the\@specialcountername}
\let\hyper@refstepcounter\specialhyper@refstepcounter
\fi
}
}
\makeatother

\begin{document}
\listofalgorithms


See \ref{algo} and \ref{heu} or \autoref{heu}

\begin{algorithm}
  \KwData{foo}
  \caption{Foo Algorithm} \label{algo}
\end{algorithm}

\SetAlgorithmName{\heuristicsname}{}{}

\clearpage
\begin{algorithm}
  \KwData{foo}
  \caption{Foo Heuristics} \label{heu}
\end{algorithm}

\end{document}

enter image description here

Update with improved setup

Use \SetupForOtherCounter{foo}{Foo} to define the new counter name and a new name for the environment caption.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{algorithm2e}

\usepackage{hyperref}

\makeatletter
\newcommand{\SetupForOtherCounter}[2]{%
  \@ifundefined{c@#1}{%
    \newcounter{#1}%
  }{}%
  \@ifundefined{#1name}{%
    \expandafter\newcommand\csname #1name\endcsname{#2}
  }{%
    \expandafter\renewcommand\csname #1name\endcsname{#2}
  }%
}
\makeatother


\SetupForOtherCounter{heuristics}{Heuristics}

\makeatletter
\AtBeginDocument{%

\@ifpackageloaded{hyperref}{%
  \let\orighyper@refstepcounter\hyper@refstepcounter%
  \newcommand{\specialhyper@refstepcounter}[1]{%
    \orighyper@refstepcounter{\@specialcountername}%
    \renewcommand{\@currentHref}{\@specialcountername.\csname the\@specialcountername\endcsname}%
  }
}{%
  \providecommand{\autoref}[1]{\ref{#1}}
  \newcommand{\specialhyper@refstepcounter}[1]{%
  }
}


\newcommand{\specialcountername}[1]{%
  \def\@specialcountername{#1}
}

\AtBeginEnvironment{algorithm}{%
\specialcountername{heuristics}%
\edef\temp@@a{\csname\@specialcountername name\endcsname}
\edef\temp@@b{\algorithmcfname}
\ifx\temp@@a\temp@@b
\csletcs{c@algocf}{c@\@specialcountername}
\csletcs{thealgocf}{the\@specialcountername}
\let\hyper@refstepcounter\specialhyper@refstepcounter
\fi
}
}
\makeatother

\begin{document}
\listofalgorithms


See \ref{algo} and \ref{heu} or \autoref{heu}

\begin{algorithm}
  \KwData{foo}
  \caption{Foo Algorithm} \label{algo}
\end{algorithm}

\SetAlgorithmName{\heuristicsname}{}{}

\clearpage
\begin{algorithm}
  \KwData{foo}
  \caption{Foo Heuristics} \label{heu}
\end{algorithm}



\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .