7

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?

Also how could we add a list of definitions after the table of contents?

Y_gr
  • 3,322

2 Answers2

6

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}%
    \addcontentsline{ldf}{figure}{#1}%
    \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:

enter image description here

An image of the look of the framed definitions:

enter image description here

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}%
    \addcontentsline{lex}{figure}{#1}%
    \begin{exa}[frametitle=Example~\theexa: #1]}
  {\end{exa}}

\newenvironment{definition}[1]
  {\stepcounter{defi}%
    \addcontentsline{ldf}{figure}{#1}%
    \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}
Gonzalo Medina
  • 505,128
5

A solution with tcolorbox (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}
\addcontentsline{good}{figure}
    {\protect\numberline{\thechapter.\thegood}#1}\par%
\begin{tcolorbox}[colback=green!5,colframe=green!40!black,title={Definition \thechapter.\thegood\space #1}]
}{\end{tcolorbox}}

\makeatletter
\preto\chapter{\addtocontents{good}{\protect\addvspace{10\p@}}}%
\makeatother

\begin{document}

\tableofcontents
\listofgoods

\chapter{One}

\chapter{Two}
\section{First}

\begin{good}{A nice heading}
\lipsum[2]
\end{good}

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

\begin{good}{Good again}

\end{good}

\end{document}

The results (index):

enter image description here

and boxes:

enter image description here

  • 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? – Y_gr Jun 18 '13 at 21:20
  • 1
    @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 '13 at 06:59
  • @ThomasF.Sturm I used the mdframed package, is it possible also in that package to break the box? – Y_gr Jun 19 '13 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 '13 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). – Y_gr Jun 19 '13 at 16:49
  • @giannis: The good news is that both mdframed and tcolorbox can be used for your purpose. mdframed uses breakable boxes as default and with tcolorbox you can switch breakability on and off. You should ask a new question with your code which does not work - it certainly can be fixed. – Thomas F. Sturm Jun 20 '13 at 15:09