I am trying to color the background of the caption in algorithm (using algorithm2e).

I have modified the original command as follow directly in the .sty file.

However, as you can see on the image, the result is not convenient. I would like the whole "row" to be in gray (I put row into brackets as it is not a tabular).

  \global\sbox\algocf@capbox{\hskip\AlCapHSkip![enter image description here][2]

The original :

  \global\sbox\algocf@capbox{\hskip\AlCapHSkip% .5\algomargin%

enter image description here

The complete code :




\DontPrintSemicolon \SetAlgoVlined}

\caption{Caption of the algo}
\For{$k$ \KwFrom $n-1$ \KwTo $1$}{
\For{$j$ \KwFrom $k+1$ \KwTo $n$}{
$x_k=x_k -a_{kj} x_j$\;
$x_k=\displaystyle \frac{x_k}{a_{kk}}$

  • Please, do not make any modifications directly in standard .sty files. May 8, 2014 at 14:05
  • So how can I modify a package ? I mean generally speaking, for any package
    – hindi
    May 8, 2014 at 14:07
  • If the change is local to just one document, you can make the change in the .tex file as I did in my answer. If you really need to change a package, then make a copy of the original .sty file, save it with a different name and make the changes in the renamed file; of course, now you load the renamed package, not the original. May 8, 2014 at 14:09

1 Answer 1


Here's one possibility (the \parbox has to be inside the \colorbox and some vertical and horizontal spacing adjustments are required):



  \global\sbox\algocf@capbox{\colorbox{AlgCaptionColor}{\hskip\AlCapHSkip% .5\algomargin%
    \parbox[t]{\hsize}{\algocf@captiontext{\strut#1}{\strut#2\strut}}\hskip.6\algomargin}}% then caption is not centered


\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 and some additional text just for the exmaple}


enter image description here

You must log in to answer this question.

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