1

I am using the algorithm2e package to typeset my algorithms. I want to be able to control the caption numbering style. More specifically, I want the caption number to be enclosed in curly braces and be blue, i.e. the caption should say

Algorithm {1} ...

with the '{1}' part in blue, instead of

Algorithm 1 ...

From what I've researched so far, it seems something among the lines of

\renewcommand{\thealgocf}{\string{\@arabic{\numexpr\value{algocf}\relax}\string}}

or

\renewcommand{\thealgocf}{\string{\@arabic\c@algocf\string}}

should do the trick (\{ and \} did not work, seemed like the escapes didn't work:

! TeX capacity exceeded, sorry [input stack size=5000].
\curr@fontshape ->\f@encoding 
                              /\f@family /\f@series /\f@shape 
l.337 \end{AlgoFunctionLike}

)

.

\string{ produces a and \string} a ̋ token (can't even put the latter into verbatim quotes...), so that doesn't work either - it says

Algorithm –1 ̋

I don't really know any tex... what is going on here, and (how) can I get what I want?

Oh, and

\renewcommand{\thealgocf}{\textcolor{blue}\string{\@arabic\c@algocf\string}}

gives me

\renewcommand{\thealgocf}{\textcolor{blue}\string{\@arabic\c@algocf\string}}

so color doesn't seem to work this way, either...

P.s.: I did not see any options for doing this in the algorithm2e documentation, there was something to set the separator after the number, and some option to set the general font style of the caption (forgot the command name), but nothing to change the style of the caption number...

P.p.s.: Here's a small working example (if you have the right packages...)

\documentclass[a4paper,11pt]{scrartcl}

\makeindex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% USED PACKAGES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[english]{babel}
\usepackage{tikz-cd}
\usetikzlibrary{babel} %conflicts...
\usepackage[utf8]{inputenc}
% BibTex
\usepackage{natbib}
\usepackage{makeidx}

% \usepackage{caption} %provides command \captionsetup

% More math typography
%\usepackage{urwchancal}
% Needed to get mathpzc
%% !!! Put this *above* the ams* packages, because urwchancal.sty
%% overwrites the mathcal command
\DeclareFontFamily{OT1}{pzc}{}
\DeclareFontShape{OT1}{pzc}{m}{it}{<-> s * [1.15] pzcmi7t}{}
\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}

\usepackage{amsmath}
% Number equations according to section, see
\numberwithin{equation}{section}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{enumitem}
% Better two-header arrows (longer...)
\usepackage{extpfeil}
\usepackage{mathtools}
\usepackage{relsize} %mathlarger command
\usepackage[english]{todonotes}
\usepackage{hyperref}
\numberwithin{equation}{section}
% Double Stroke. amsfont doesn't render numbers properly
\usepackage{dsfont}
\newenvironment{keywords}%
    {\begin{trivlist}\item[]{\bfseries\sffamily Keywords:}\ }
    {\end{trivlist}}
% Used by qspace macro
\usepackage{xfrac}
\usepackage{graphicx}

% Used for Bibtex
\usepackage{url}
% Custom packages
\usepackage{./latex-math/general_math}
% No use yet
% \usepackage{./latex-math/general_algebra}
% For defining environments as wrappers around old ones, see
\usepackage{environ}
% Used for colors for algorithm2e
\usepackage{xcolor}
\hypersetup{colorlinks=false, linkbordercolor={cyan}}

\usepackage[noline, algoruled, nofillcomment, linesnumbered]{algorithm2e}
% Syntaxcolors etc./customization pertaining to 'algorithm2e'
% \definecolor{syntax_keywords}{RGB}{0, 152, 220}
\newcommand\mykwfont[1]{\texttt{\color{brown}#1}}
\SetKwSty{mykwfont}

\usepackage{aligned-overset} % allow alignment markers + overset: \overset{bla}&{=}

\definecolor{dark_grey}{RGB}{128, 128, 128}
\newcommand\mynlfont[1]{\texttt{\color{dark_grey}#1}}
\SetNlSty{mynlfont}{}{}

\definecolor{purple_dark}{RGB}{125, 0, 125}
% \newcommand\myfuncfont[1]{\texttt{\color{syntax_function}#1}}

\definecolor{syntax_comment}{RGB}{128, 128, 128}
\newcommand\mycommfont[1]{\texttt{\color{blue}#1}}
\SetCommentSty{mycommfont}

% \definecolor{syntax_blockmarkers}{RGB}{255, 255, 255}
% \newcommand\mybmfont[1]{\texttt{\color{syntax_blockmarkers}#1}}
% \SetBlockMarkersSty{mybmfont}

\SetArgSty{text}
\SetProgSty{text}
\newcommand\myalcapnamefont[1]{\textbf{#1}}
\SetAlCapNameSty{myalcapnamefont}
\newcommand\myfuncstyfont[1]{\textbf{#1}}
\SetFuncSty{myfuncstyfont}
\SetFuncArgSty{text}

\setlength{\algomargin}{0.3em}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ALGORITHM2E STUFF
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\Parameters}{\textbf{Parameters}\\}
\newcommand{\FuncParam}[2]{{\ArgSty{#1}}:\hspace{1em}{\color{blue}#2}\\}

\newcommand{\Subroutines}{\textbf{Subroutines}\\}
\newcommand{\DeclareFunction}[1]{\SetKwFSimple{#1}\texttt{#1}()}
\newcommand{\Output}[1]{\textbf{Output}\\{\color{blue}#1}}
\newcommand{\KwReturn}[1]{\KwReturnHelper{#1\\\vspace{0.5em}}}
\newcommand{\SetKwFSimple}[1]{\SetKwFunction{#1}{#1}}
\newcommand{\KwMainFunctionCaption}[2]{\caption{#1}\SetKwFunction{KwMainFunc}{#1}}
\SetKw{continue}{continue}
\SetKw{KwInt}{int}
\newcommand{\CComment}[1]{{\DontPrintSemicolon\tcc{#1}}}
\newcommand{\Cland}{\&\& }

\NewEnviron{AlgoFunctionLike}{
    \IncMargin{1em}
    \SetInd{1em}{1em}
    \SetAlgoHangIndent{0.75em}
    \SetStartEndCondition{ \textbf{(}}{\textbf{)}}{\textbf{)}}
    \SetAlgoBlockMarkers{}{}%
    \SetKwProg{Fn}{}{{ \textcolor{black}\{}}{{\textcolor{black}\}}}
    \SetKwFor{For}{for}{{ \textcolor{black}\{}}{{\textcolor{black}\}}}%
    \SetKwIF{If}{ElseIf}{Else}{if}{{ \color{black}\{}} {{\color{black}\}} else if}{{ \textcolor{black}\}} {else} {\textcolor{ black}\{}}{{\textcolor{black}\}}}%
    \SetKwFor{While}{while}{{ \textcolor{black}\{}} {{\textcolor{black}\}}}%
    \SetKwRepeat{DoWhile}{do{ \textcolor{black}\{}}{{\textcolor{black}\}} while}%
    \SetKwComment{tcc}{\color{blue}/* }{ \color{blue}*/}
    \AlgoDisplayBlockMarkers
    \SetAlgoNoLine%
    \begin{algorithm}
        \BODY
    \end{algorithm}
}

% Hack to quickly typeset functions
\newcommand{\AlgoFunctionLikeFont}[1]{{\textcolor{blue}\{#1\}}}


% Parameters as in C-for loop, same order
\newcommand{\ForParams}[3]{{#1};\hspace{0.5em}{#2};\hspace{0.5em}{#3}}
% Only use in algorithm env.
\newcommand{\ForLoop}[4]{\For{\ForParams{#1}{#2}{#3}}{#4}}
% Parameters: As for ForParams
% Prints a for-loop beginning like for (bla1; bla2; bla3) {
% With curly braces and parens with \huge; currently looks like the internal
% typesetting for blocks
\newcommand{\BeginForLoop}[3]{%
    \KwSty{for}\hspace{0.3em}\textbf{(}%
        \ForParams{\ArgSty{#1}}{\ArgSty{#2}}{\ArgSty{#3}}\textbf{)} {\large\{}\\%
}
\newcommand{\BeginForLoopNoLineBreak}[3]{%
    \KwSty{for}\hspace{0.3em}\textbf{(}%
        \ForParams{\ArgSty{#1}}{\ArgSty{#2}}{\ArgSty{#3}}\textbf{)} {\large\{}%
}
% Open a code block, but only visually, i.e. no block w.r.t. algorithm2e will be opened
\newcommand{\BlockSty}[1]{{\large{}#1}}%
\newcommand{\OpenBlockVisual}{\BlockSty{\{}}%
% Close-counterpart to the above
\newcommand{\CloseBlockVisual}{\BlockSty{\}}}%
\newcommand{\QuickFunc}[2]{\FuncSty{{#1}(}\ArgSty{#2}\FuncSty{)}}%

\begin{document}
\selectlanguage{english}

\author{Alexander Rehbein}

\newcommand{\Gets}{\ensuremath{}\;\boldmath{\longleftarrow}\;}
\newcommand{\varmatrix}[5]{
    \ensuremath{\left({#1}_{i,j}\right)_{\substack{{#2}=1,\,\dots\,,\,{#3} \\ j={#4},\,\dots\,,\,{#5}}}}
}
\newcommand{\matring}[3]{\ensuremath{{#1}^{{#2}\times{#3}}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ALGO EXAMPLE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\ring}{\ensuremath{\mathpzc{R}}}

\begin{AlgoFunctionLike}\label{alg:MatrixMult}
\KwMainFunctionCaption{MatrixMult}{$A,\,B$}

\Parameters
\FuncParam{$A$}{Element of $\matring{\ring}{m}{n}$, $A = \varmatrix{a}{i}{m}{j}{n}$, $m$, $n$ positive integers, $\ring$
                a ring} 
\FuncParam{$B$}{Element of $\matring{\ring}{n}{o}$, $B = \varmatrix{b}{j}{n}{k}{o}$, $o$ positive integer}
\Output{$AB$}
\vspace{1em}

\Fn{\KwMainFunc{$A,\,B$}} {
    $C$ = $\varmatrix{c}{i}{m}{k}{o} \Gets 0$\;
    \ForLoop{row $\Gets 1$}{row $\leq m$}{++row} {
        \ForLoop{col $\Gets 1$}{col $\leq o$}{++col} {
            \ForLoop{sum\_index $\Gets 1$,\,$c_{\text{row}, \text{col}} \Gets 0$}
                    {sum\_index $\leq n$}{++sum\_index} {
                $c_{\text{row}, \text{col}} \Gets c_{\text{row}, \text{col}} + %
                    a_{\text{row}, \text{sum\_index}} \cdot b_{\text{sum\_index}, \text{col}}$\;
            }
        }
    }
    \Return{$C$}\;
}
\end{AlgoFunctionLike}
\bibliographystyle{unsrtnat}
\bibliography{sources}

\end{document}
9
  • 2
    Do you want to get “Algorithm {1}”? Please, add a minimal example of code to play with.
    – egreg
    Mar 5, 2020 at 18:05
  • @egreg I've added some code excerpts that should work. Yes, I want "Algorithm {1}" or "Algorithm {2}" etc., with the "{2}" part in blue. Or am I misunderstanding your question? Mar 5, 2020 at 18:16
  • @polynomial_donut: What should it look like when you use Algorithm \ref{alg}? Should that also turn into Algorithm {2}, or just Algorithm 2?
    – Werner
    Mar 5, 2020 at 18:18
  • @Werner The first one, but if that's not the case that'd be okay, too (currently using \newcommand{\algoref}[1]{\hbox{\texttt{\color{blue}\{\hspace{-0.06em}\textbf\small\ref{#1}\hspace{-0.06em}\}}}} to have it look the way I want it) Mar 5, 2020 at 18:20
  • 2
    @polynomial_donut Please, a small example starting from \documentclass and ending with \end{document}.
    – egreg
    Mar 5, 2020 at 18:20

2 Answers 2

3

Using an answer at How to change the order of algorithm number and name in algorithm2e? to help guide me.

\documentclass{article}
\usepackage{algorithm2e,xcolor}

\makeatletter
\renewcommand{\fnum@algocf}{\AlCapSty{\algorithmcfname\nobreakspace
  \color{blue}\{\AlCapFnt\thealgocf\}}}
\makeatother

\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
      }{
      go back to the beginning of current section\;
      }
    }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

enter image description here

1

The following updates the counter representation to print the algorithm number and braces in blue:

enter image description here

\documentclass{article}

\usepackage{algorithm2e,xcolor}

\renewcommand{\thealgocf}{\textcolor{blue}{\{\arabic{algocf}\}}}

\begin{document}

See Algorithm~\ref{alg:algorithm}.

\begin{algorithm}
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{%
      go to next section\;
      current section becomes this one\;
    }{%
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
  \label{alg:algorithm}
\end{algorithm}

\end{document}

Since the representation is changed, it also naturally follows that it will move to the representation of any \references as well. If you're using your own referencing macro (like \algoref say), then you could incorporate it as

\newcommand{\algoref}[1]{Algorithm~{\ttfamily\small\ref{#1}}}
4
  • Just copy pasted your renewcommand and got a ! Argument of \reserved@a has an extra }.. Mar 6, 2020 at 15:35
  • @polynomial_donut: No problem in my code. Can you provide more context so I can replicate your issue?
    – Werner
    Mar 6, 2020 at 17:17
  • The example in my question, for instance. Put your renewcommand line before the line \NewEnviron{AlgoFunctionLike}{. Breaks compilation with said error, uncomment and it works again. $ pdflatex --version pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Arch Linux) kpathsea version 6.3.1 Mar 6, 2020 at 17:30
  • $Id: algorithm2e.sty,v 5.2 2017/07/18 15:26:26 fioriochristophe Exp $ (from: /usr/share/texmf-dist/tex/latex/algorithm2e/algorithm2e.sty) Mar 6, 2020 at 17:31

You must log in to answer this question.

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