# title of a colored box

In an another post, beamer style text box, I've seen a box that I'm interested in making it. However, I would like to use it in a book, and I would like before the title to write: Definition (or Example) and then the number of the chapter and at the end the title.

For example: Definition 2.1:Good (when I'm on the second chapter, I'm giving the first definition here of the good)

What would be the code?

-
Have you read the manuals for the packages mdframed and tcolorbox? –  malin Jun 18 at 14:59
You can combine the answer in the post you linked with the tips about making your own "list of...whatever" given here –  JLDiaz Jun 18 at 15:03

The new list of definitions was created using the LaTeX's kernel \@starttoc and it behaves in the same way as the standard LoF and LoT, but uses a file with extension .ldf. The boxes were defined using mdframed:

\documentclass{book}
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{chngcntr}
\usepackage{lipsum}

\newcounter{exa}[chapter]
\counterwithin{exa}{chapter}

\makeatletter
\newcommand\listdefinitionname{List of Definitions}
\newcommand\listofdefinitions{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listdefinitionname}%
\@mkboth{\MakeUppercase\listdefinitionname}%
{\MakeUppercase\listdefinitionname}%
\@starttoc{ldf}%
\if@restonecol\twocolumn\fi
}
\makeatother

\mdfdefinestyle{mystyle}{%
linecolor=green!40!black,
outerlinewidth=1pt,%
frametitlerule=true,
frametitlefont=\sffamily\bfseries\color{white},%
frametitlerulewidth=1pt,frametitlerulecolor=green!40!black,%
frametitlebackgroundcolor=green!40!black,
backgroundcolor=green!5,
innertopmargin=\topskip,
roundcorner=5pt
}

\newmdenv[style=mystyle]{exa}

\newenvironment{example}[1]
{\stepcounter{exa}%
\begin{exa}[frametitle=Definition~\theexa: #1]}
{\end{exa}}

\begin{document}

\listofdefinitions

\chapter{Test Chapter}

\begin{example}{The Title}
\lipsum[1]
\end{example}\par\bigskip

\begin{example}{Another Title}
\lipsum[1]
\end{example}

\end{document}


An image of the new list of definitions:

An image of the look of the framed definitions:

The style and settings can be reused to define similar environments and lists:

\documentclass{book}
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{chngcntr}
\usepackage{lipsum}

\newcounter{exa}[chapter]
\newcounter{defi}[chapter]
\counterwithin{exa}{chapter}
\counterwithin{defi}{chapter}

\makeatletter
\newcommand\listdefinitionname{List of Definitions}
\newcommand\listofdefinitions{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listdefinitionname}%
\@mkboth{\MakeUppercase\listdefinitionname}%
{\MakeUppercase\listdefinitionname}%
\@starttoc{ldf}%
\if@restonecol\twocolumn\fi
}
\newcommand\listexamplename{List of Definitions}
\newcommand\listofexamples{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listexamplename}%
\@mkboth{\MakeUppercase\listexamplename}%
{\MakeUppercase\listexamplename}%
\@starttoc{lex}%
\if@restonecol\twocolumn\fi
}
\makeatother

\mdfdefinestyle{mystyle}{%
linecolor=green!40!black,
outerlinewidth=1pt,%
frametitlerule=true,
frametitlefont=\sffamily\bfseries\color{white},%
frametitlerulewidth=1pt,frametitlerulecolor=green!40!black,%
frametitlebackgroundcolor=green!40!black,
backgroundcolor=green!5,
innertopmargin=\topskip,
roundcorner=5pt
}

\newmdenv[style=mystyle]{exa}
\newmdenv[style=mystyle]{defi}

\newenvironment{example}[1]
{\stepcounter{exa}%
\begin{exa}[frametitle=Example~\theexa: #1]}
{\end{exa}}

\newenvironment{definition}[1]
{\stepcounter{defi}%
\begin{defi}[frametitle=Example~\thedefi: #1]}
{\end{defi}}

\begin{document}

\listofdefinitions
\listofexamples

\chapter{Test Chapter}

\begin{example}{The title of an example}
\lipsum[1]
\end{example}\par\bigskip

\begin{definition}{The title of a definition}
\lipsum[1]
\end{definition}

\end{document}

-
thank you both very much!! –  giannis Jun 18 at 15:58
If I would like to have another box entitled as Example, what should I change in the code? –  giannis Jun 22 at 10:10
@giannis please see my updated answer. –  Gonzalo Medina Jun 22 at 13:15
thank you very much. –  giannis Jun 22 at 16:17

A solution with tcolobox (exploiting Formatting new list of definitions with tocloft for the new table of contents):

\documentclass{book}
\usepackage{tcolorbox}
\usepackage{lipsum}
\usepackage{tocloft}
\usepackage{etoolbox}

\newcounter{good}
\setcounter{good}{0}

\newcommand{\listgoodname}{List of Goods}
\newlistof{goods}{good}{\listgoodname}

\newenvironment{good}[1]{
\stepcounter{good}
{\protect\numberline{\thechapter.\thegood}#1}\par%
\begin{tcolorbox}[colback=green!5,colframe=green!40!black,title={Definition \thechapter.\thegood\space #1}]
}{\end{tcolorbox}}

\makeatletter
\makeatother

\begin{document}

\tableofcontents
\listofgoods

\chapter{One}

\chapter{Two}
\section{First}

\lipsum[2]
\end{good}

\begin{good}{Good}
\lipsum[2]
\end{good}

\begin{good}{Good again}

\end{good}

\end{document}


The results (index):

and boxes:

-
If the whole passage of my box doesn't fit in a whole page, how can I manage to make it continue to the next page? –  giannis Jun 18 at 21:20
@giannis: For breakability you should modify the solution by \usepackage[breakable,skins]{tcolorbox} and add to the tcolorbox the options breakable (this will break the boxes but keep the partial boxes closed) or enhanced,breakable (this will break the boxes with opened borders). –  Thomas F. Sturm Jun 19 at 6:59
@ThomasF.Sturm I used the mdframed package, is it possible also in that package to break the box? –  giannis Jun 19 at 14:40
@giannis: with mdframed you're saved, since it automatically handles breaks: it is also stated in the abstract of the documentation. –  Claudio Fiandrino Jun 19 at 15:45
@ClaudioFiandrino I've read it, but in my document it doesn't breaks automatically. For the record I've used the code above (not yours-above that). –  giannis Jun 19 at 16:49
show 1 more comment