4

I am using the algorithm2e package to generate pseudocode for a paper. The journal requires the format of "S1 Pseudocode", "S2 Pseudocode", etc. However, I am only able to generate the format as "Pseudocode S 1" based on the line \SetAlgorithmName{Pseudocode S}{}. Below is the minimum working example:

\documentclass[parskip=full]{bmcart}
\usepackage[lined]{algorithm2e}
\usepackage{geometry}

\begin{document}
\begin{algorithm}[H]

\SetAlgorithmName{Pseudocode S}{}

\caption{Pseudocode for my algorithm}
\end{algorithm}
\end{document}

How can I update the label to indicate "S1 Pseudocode" (instead of "Pseudocode S 1")? Presumably this would be changed with the \SetAlgorithmName command. Any advice greatly appreciated!

2 Answers 2

6

The algorithm2e does not naturally come with a definition allowing the counter to come before the caption.

You can, however edit the internal commands it uses to suit your needs:

\fnum@algocf is the macro that sets the caption name. You can renew it to output the counter first then the name thus:

\renewcommand{\fnum@algocf}{\AlCapSty{\AlCapFnt\thealgocf\nobreakspace\algorithmcfname}}%

Now the output would be 1 Psuedocode. To change it to S1 renew \thealgocf

\renewcommand{\thealgocf}{S\@arabic\c@algocf}%

A whole MWE

\documentclass[parskip=full]{article}
\usepackage[lined]{algorithm2e}
\usepackage{geometry}

\makeatletter
\renewcommand{\thealgocf}{S\@arabic\c@algocf}% and the way it is printed
\renewcommand{\fnum@algocf}{\AlCapSty{\AlCapFnt\thealgocf\nobreakspace\algorithmcfname}}%
\makeatother

\SetAlgorithmName{Pseudocode}{}{}

\begin{document}
\begin{algorithm}[H]

\caption{Pseudocode for my algorithm}
\end{algorithm}

How can I update the label to indicate "S1 Pseudocode" (instead of "Pseudocode S 1")? Presumably this would be changed with the \SetAlgorithmName command. Any advice greatly appreciated!
\end{document}

And the result

Result

comment: The original MWE was with a documentclass I do not have, changed to article

4

Small comment to the question & answer from Elad Den (not enough reputation to comment myself)

\SetAlgorithmName{Pseudocode}{}{}

The command \SetAlgorithmName requires three arguments. Missing the last one leads to not printing the caption name at all, in case you use multiple algorithm blocks.

see http://ctan.mirror.norbert-ruehl.de/macros/latex/contrib/algorithm2e/doc/algorithm2e.pdf

\SetAlgorithmName{algorithmname}{algorithmautorefname}{list of algorithms name} which redefines name of the algorithms and the sentence list of algorithms. Example: \SetAlgorithmName{Protocol}{List of protocols} if you prefer protocol than algorithm. Second argument is the name that \autoref, from hyperref package, will use.

3
  • It needs 2 arguments, one can be empty. If you use two as in the MWE of my answer and the OP then it will post the name. This is shown in my example and even your quoted example shows that...
    – Elad Den
    Commented Apr 30, 2020 at 13:14
  • Please do try the following example: \begin{document} \begin{algorithm}\SetAlgorithmName{Alg.}{}\caption{text}\end{algorithm} \begin{algorithm}\SetAlgorithmName{Alg.}{}{}\caption{text}\end{algorithm} \begin{algorithm}\caption{text}\end{algorithm} \end{document}
    – marc k
    Commented Apr 30, 2020 at 14:04
  • ah... I see... I think the real reason I never came across this is that I usually set the algorithm name in the preamble and never touch it since. But you are right, I'll correct the answer
    – Elad Den
    Commented May 3, 2020 at 5:49

You must log in to answer this question.

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