# Apply background color in algorithm environment

I have not found a simple solution to have a background color in the algorithm environment. Can you help me put a background.

Here is the code that I have so far:

\documentclass{article}
\usepackage{amsmath, amsfonts, amssymb, amsthm, bm}
\usepackage{commath}
\usepackage{xcolor}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}

\begin{document}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\begin{algorithm}[H]
\DontPrintSemicolon
This is line one\\
This is line two numbered\\
This should be numbered\\
This should also be numbered
\caption{Unnumbered lines}
\end{algorithm}

\end{document}


This is how I do to center an algorithm with a gray background :

\begin{center}
\colorbox[gray]{0.95}{
\begin{minipage}{0.65\textwidth}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\begin{algorithm}[H]
\DontPrintSemicolon
This is line one\\
This is line two numbered\\
This should be numbered\\
This should also be numbered
\caption{Unnumbered lines}
\end{algorithm}
\end{minipage}}
\end{center}

• Thanks for your input. I was trying to modify your code to work with other colors like blue or \definecolor{mybluei}{RGB}{0,173,239}. When I tried to change the color, I could not compile the code. I changed the line \colorbox[gray]{0.95}{ to \colorbox[blue]{0.95}{ and I got the error ! Package xcolor Error: Underined color model blue. How do you go about changing the background color? Thanks! – Joe Apr 24 '16 at 0:43
• For posterity: the solution I used for the above comment by @Joe was to write \colorbox[rgb]{0.9, 0.9, 1} for a box that is lightly shaded blue. Parameters {0, 0, 0} give black and {1, 1, 1} give white, and you can interpolate between these. – GMB Dec 31 '20 at 21:23

This is PART I of my answer to the above comment of @Joe: (since I don't have enough reputation points)

Answer: Your problem is actually with regards to color models. Kindly look at the Color Models subsection in Wiki. I have slightly modified the code of @pzorba75 using the rgb model with color specification {0.74,0.83,1}.

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}

\begin{document}
\begin{center}
\colorbox[rgb]{0.74,0.83,1}{
\begin{minipage}{0.65\textwidth}
\SetAlgoLined
\SetNlSty{textbf}{}{:}
\begin{algorithm}[H]
\DontPrintSemicolon
This is line one\\
This is line two numbered\\
This should be numbered\\
This should also be numbered
\caption{Unnumbered lines}
\end{algorithm}
\end{minipage}}
\end{center}
\end{document}


The output looks like this:

Remark:

Well, of course, you can choose different color specifications. For some list to choose from, click this link.

UPDATE:

SUPPOSE YOU FOUND AN IMAGE WITH A COLOR YOU WANT FOR BACKGROUND BUT YOU DON'T KNOW THE COLOR SPECIFICATION

For example, if I want the light gray background color in the code blocks here at tex.stackexchange, the following steps using MS Paint will give me approximately the color:

1. Print Screen (PrtScr) the image and paste it to Microsoft Paint.
2. Click the Color Picker, the dropper-like image at the Tools section of Paint

and then click to the portion of the image where the desired color is located.

3. Now go to the Edit Color section and click it.

A pop-up menu should appear like the following and look at the Red-Green-Blue Section:

1. The Red-Green-Blue represents the color model RGB and in this example our desired image with the desired color has color specification {239,240,241}.
• Welcome to TeX.SX! Please always post compilable code snippets to answer the question (especially if you changed preamble and document). – TeXnician Oct 24 '17 at 5:26
• @TeXnician, I have edited by your requirements. Thankful for the comment. – venrey Oct 24 '17 at 6:03
• Suggestion to remove UPDATE part. This is off-topic for tex.se – hola Jun 6 '19 at 22:15

This is PART II of my answer to the above comment of @Joe: (Due to low reputation points, I can't post more than 8 links in the first part of my answer.)

We now illustrate the obtained RGB color specification {239,240,241} with the following code:

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}

\begin{document}
\begin{center}
\colorbox[RGB]{239,240,241}{
\begin{minipage}{.9\linewidth}
\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
\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{minipage}}
\end{center}
\end{document}


and it outputs the following: