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




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


gives me


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...)


\usetikzlibrary{babel} %conflicts...
% BibTex

% \usepackage{caption} %provides command \captionsetup

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

% Number equations according to section, see
% Better two-header arrows (longer...)
\usepackage{relsize} %mathlarger command
% Double Stroke. amsfont doesn't render numbers properly
    {\begin{trivlist}\item[]{\bfseries\sffamily Keywords:}\ }
% Used by qspace macro

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

\usepackage[noline, algoruled, nofillcomment, linesnumbered]{algorithm2e}
% Syntaxcolors etc./customization pertaining to 'algorithm2e'
% \definecolor{syntax_keywords}{RGB}{0, 152, 220}

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

\definecolor{dark_grey}{RGB}{128, 128, 128}

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

\definecolor{syntax_comment}{RGB}{128, 128, 128}

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





\newcommand{\Cland}{\&\& }

    \SetStartEndCondition{ \textbf{(}}{\textbf{)}}{\textbf{)}}
    \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}*/}

% Hack to quickly typeset functions

% Parameters as in C-for loop, same order
% Only use in algorithm env.
% 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
        \ForParams{\ArgSty{#1}}{\ArgSty{#2}}{\ArgSty{#3}}\textbf{)} {\large\{}\\%
        \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
% Close-counterpart to the above


\author{Alexander Rehbein}

    \ensuremath{\left({#1}_{i,j}\right)_{\substack{{#2}=1,\,\dots\,,\,{#3} \\ j={#4},\,\dots\,,\,{#5}}}}


\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}

\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}}$\;

  • 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


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




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


enter image description here


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

enter image description here





See Algorithm~\ref{alg:algorithm}.

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


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

  • 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 .