Creating a Problem Environment

I'm trying to learn how to make new environments, namely I want to make a "problem environment" for examples of problems, and the algorithms that solve them. Optimally I would like it to use a typewriter-like font, and have some kind of shaded box around the problem kind of like the following from Algorithms by Vazirani:

I don't really know how to approach this problem, currently I'm just looking into the \newenvironment command, so any input would be greatly appreciated. Optimally I'm looking for an environment that has a space for the spec, the algorithm, and the runtime analysis.

\documentclass{11pt}{article}
\begin{document}
\newenvironment{problem}[1][ ]{
\begin{tcolorbox}
\newcommand\given[1]{\fontfamily{cmtt}\selectfont\textbf{Given}: ##1\\ \\}
\newcommand{\goal}[1]{\fontfamily{cmtt}\selectfont\textbf{Goal}: ##1\\ \\}
\newcommand\discussion[1]{\fontfamily{cmtt}\selectfont\textbf{Discussion}: ##1\\ \\}
\newcommand\algorithm[1]{\fontfamily{cmtt}\selectfont\textbf{Algorithm}: ##1\\ \\}
\fontfamily{cmtt}\selectfont\textbf{Problem}: #1 \\
}{\end{tcolorbox}\par}
\begin{problem}
\given{}
\goal{}
\discussion{}
\algorithm{}
\end{problem}
\end{document}

• For the general question, see my answer on this topic. The only way to learn it is to play around and post a much more specific question when you get stuck. That is, you need an MWE. – cfr Jan 21 '15 at 4:15
• I'll work on getting one up. Do you know where to look for coloring, or anything? – user41692 Jan 21 '15 at 4:16
• However, if what you really want is a solution to the specific problem, there are plenty of ready-made solutions available. Just search the site. Or look at the related questions on the right. Again, your question needs to be more specific: what have you tried? That is, you need an MWE. – cfr Jan 21 '15 at 4:16
• There are a whole bunch of questions on this e.g. try searching for tcolorbox. (I'd suggest searching CTAN but you can't right now - it isn't working.) – cfr Jan 21 '15 at 4:17
• mdframed if your environment needs to allow page breaks. – cfr Jan 21 '15 at 4:18

But I took your MWE and changed it in a way you may want to have it. I kept the code as near to your MWE as possible. Especially, many font settings are superfluous, but I kept them.

• I would not add a \newcommand inside an environment if there is no specific reason for it (e.g. dependency of environment parameters). So, I moved these commands outside.

• The options for a tcolorbox are given by a key-value list. I made some settings which look like in the picture you provided.

The code is:

\documentclass[11pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}

\newcommand\given[1]{\par\noindent\fontfamily{cmtt}\selectfont\textbf{\sffamily Given}: #1\par\bigskip}
\newcommand{\goal}[1]{\par\noindent\fontfamily{cmtt}\selectfont\textbf{\sffamily Goal}: #1\par\bigskip}
\newcommand\discussion[1]{\par\noindent\fontfamily{cmtt}\selectfont\textbf{\sffamily Discussion}: #1\par\bigskip}
\newcommand\algorithm[1]{\par\noindent\fontfamily{cmtt}\selectfont\textbf{\sffamily Algorithm}: #1\par\bigskip}

\newenvironment{problem}[1][]{%
\begin{tcolorbox}[enhanced jigsaw,
breakable,
sharp corners,
beforeafter skip=10pt,
size=fbox,
boxrule=0pt,
frame hidden,
colback=black!10
]
\fontfamily{cmtt}\selectfont\textbf{\sffamily Problem}: #1 \par\bigskip
}{\end{tcolorbox}\par}

\begin{document}
\lipsum[5]

\begin{problem}[My Problem]
\given{\lipsum[1]}
\goal{\lipsum[2]}
\discussion{\lipsum[3]}
\algorithm{\lipsum[4]}
\end{problem}

\lipsum[6]
\end{document}


• I removed superfluous font settings.

• \newenvironment is replaced by \newtcolorbox which is shorter and provided some exta goodies if needed.

• The problem title line is put into a title with own coloring.

The code is:

\documentclass[11pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}

\newcommand\given[1]{\par\noindent\textbf{\sffamily Given}: #1\par\bigskip}
\newcommand{\goal}[1]{\par\noindent\textbf{\sffamily Goal}: #1\par\bigskip}
\newcommand\discussion[1]{\par\noindent\textbf{\sffamily Discussion}: #1\par\bigskip}
\newcommand\algorithm[1]{\par\noindent\textbf{\sffamily Algorithm}: #1\par\bigskip}

\newtcolorbox{problem}[1][]{enhanced jigsaw,
breakable,
sharp corners,
beforeafter skip=10pt,
size=fbox,
boxrule=0pt,
frame hidden,
colback=blue!10,
fontupper=\fontfamily{cmtt}\selectfont,
fonttitle=\sffamily,
colbacktitle=blue!50!black,
title={\textbf{\sffamily Problem}: #1},
top=3pt
}

\begin{document}
\lipsum[5]

\begin{problem}[My Problem]
\given{\lipsum[1]}
\goal{\lipsum[2]}
\discussion{\lipsum[3]}
\algorithm{\lipsum[4]}
\end{problem}

\lipsum[6]
\end{document}


• It's giving me an unknown option error for many... – user41692 Jan 21 '15 at 8:10
• @Anthony I guess your installation is outdated. You should update the tcolorbox package to a recent version. – Thomas F. Sturm Jan 21 '15 at 8:12
• @Anthony The log file will tell you the version number. There should be something like Package: tcolorbox 2015/01/14 version 3.40 text color boxes – Thomas F. Sturm Jan 21 '15 at 8:13