[Tex/LaTex] \SetAlgorithmName in algorithm2e package with a separate counter

algorithm2ecounters

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.

Best Answer

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}