Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Have you read the manuals for the packages mdframed and tcolorbox? –  malin Jun 18 '13 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 '13 at 15:03
add comment

2 Answers

up vote 6 down vote accepted

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}
share|improve this answer
    
thank you both very much!! –  giannis Jun 18 '13 at 15:58
    
If I would like to have another box entitled as Example, what should I change in the code? –  giannis Jun 22 '13 at 10:10
    
@giannis please see my updated answer. –  Gonzalo Medina Jun 22 '13 at 13:15
    
thank you very much. –  giannis Jun 22 '13 at 16:17
add comment

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

share|improve this answer
    
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 '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 6:59
    
@ThomasF.Sturm I used the mdframed package, is it possible also in that package to break the box? –  giannis 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). –  giannis Jun 19 '13 at 16:49
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.